%%Matlab code for Euler's forward and Rk4 method
clear all
close all
%---------------------%
%All h values
hh=[0.1,0.01];
for it=1:2
%Program for Euler
%function for Euler equation solution
f=@(x,y) -1.2*y+7.*exp(-0.3*x);
%Euler steps for h and x_end
%Initial values
x0=0;
y0=3;
%step size
h=hh(it);
%x end value
xend=2;
xn=x0:h:xend;
% Euler steps
y_result1(1)=y0;
x_result1(1)=x0;
%Loop for Euler Steps
for i=1:length(xn)-1
x_result1(i+1)=
x_result1(i)+h;
y_result1(i+1)=y_result1(i)+h*double(f(x_result1(i),y_result1(i)));
end
%printing the result for Euler
fprintf('\tThe approximate solution using Euler
method at x=%d for h=%0.2f is %f\n',xend,h,y_result1(end))
%---------------------%
%Program for RK4
%function for RK4 equation solution
f=@(x,y) -1.2*y+7.*exp(-0.3*x);
%RK4 steps for h and x_end
%Initial values
x0=0;
y0=3;
%x end value
xend=2;
xn=x0:h:xend;
% RK4 steps
y_result2(1)=y0;
x_result2(1)=x0;
%Runge Kutta 4
iterations
n=(xend-x0)/h;
for i=1:n
k0=h*f(x_result2(i),y_result2(i));
k1=h*f(x_result2(i)+(1/2)*h,y_result2(i)+(1/2)*k0);
k2=h*f(x_result2(i)+(1/2)*h,y_result2(i)+(1/2)*k1);
k3=h*f(x_result2(i)+h,y_result2(i)+k2);
x_result2(i+1)=x_result2(i)+h;
y_result2(i+1)=double(y_result2(i)+(1/6)*(k0+2*k1+2*k2+k3));
end
%printing the result for RK4
fprintf('\tThe approximate solution using RK4
method at x=%d for h=%0.2f is %f\n',xend,h,y_result2(end))
%---------------------%
%Finding exact solution using dsolve
syms y(x)
eqn=diff(y,x)==-1.2*y+7.*exp(-0.3*x);
cond=y(0)==3;
y_result3(x)=dsolve(eqn,cond);
%printing exact solution
fprintf('\tThe exact solution using dsolve at
x=%d is %f\n',xend,y_result3(xend))
fprintf('The expression for Exact
solution\n')
disp(y_result3(x))
figure(it)
subplot(2,1,1)
hold on
plot(x_result1,double(y_result3(x_result1)))
plot(x_result1,y_result1)
legend('Exact solution','Euler Solution')
title(sprintf('Euler and Exact solution for
h=%2.3f',h))
xlabel('x')
ylabel('y')
subplot(2,1,2)
hold on
plot(x_result1,double(y_result3(x_result1)))
plot(x_result2,y_result2)
legend('Exact solution','RK4 Solution')
title(sprintf('RK4 and Exact solution for
h=%2.3f',h))
xlabel('x')
ylabel('y')
if it==1
%Printing the result for
table
fprintf('Table for
h=0.1\n')
fprintf('\n\tx,\ty(EM),\ty(RK),\tExact(Y),\tError-EM,\tError-RK,\n')
for
ii=1:length(y_result1)
ext_sl=y_result3(x_result1(ii));
err1=double((abs(y_result1(ii)-ext_sl)/ext_sl)*100);
err2=double((abs(y_result2(ii)-ext_sl)/ext_sl)*100);
fprintf('\t%2.2f,\t%2.5f,\t%2.5f,\t%2.5f,\t%2.2f,\t%2.5f,\t\n',...
x_result1(ii),y_result1(ii),y_result2(ii),ext_sl,err1,err2)
end
end
end
%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%
Runge-Kutta method R-K method is given by the following algorithm. o y(xo)- given k2-f(xi5.yi tk,...
-1.2y 7e-03* from x tox 2.0 with the 2) Use Euler's method to solve the ODE initial condition y3 at x0 dx a) Solve by hand using h b) Write a MATLAB program in a script file that solves the equation using h-0.1 and find y(1.5). c) Use the program from part (b) to solve the equation using h= 0.01 and h = 0.001 and findy(1,5). 0.5 and find y(2). d) The exact solution to the IVP is given by...
please show all steps and equations used, please write
neatly.
Problem 16. Given the Runge-Kutta method for the initial value problem y' = f(t,y) for a
use
matlab
Assignment: 1) Write a function program that implements the 4th Order Runge Kutta Method. The program must plot each of the k values for each iteration (one plot per k value), and the approximated solution (approximated solution curve). Use the subplot command. There should be a total of five plots. If a function program found on the internet was used, then please cite the source. Show the original program and then show the program after any modifications. Submission...
Need help with this MATLAB problem:
Using the fourth order Runge-Kutta method (KK4 to solve a first order initial value problem NOTE: This assignment is to be completed using MATLAB, and your final results including the corresponding M- iles shonma ac Given the first order initial value problem with h-time step size (i.e. ti = to + ih), then the following formula computes an approximate solution to (): i vit), where y(ti) - true value (ezact solution), (t)-f(t, v), vto)...
Numerical Methods
Consider the following IVP dy=0.01(70-y)(50-y), with y(0)-0 (a) [10 marks Use the Runge-Kutta method of order four to obtain an approximate solution to the ODE at the points t-0.5 and t1 with a step sizeh 0.5. b) [8 marks Find the exact solution analytically. (c) 7 marks] Use MATLAB to plot the graph of the true and approximate solutions in one figure over the interval [.201. Display graphically the true errors after each steps of calculations.
Consider the...
Given (dy/dx)=(3x^3+6xy^2-x)/(2y) with y=0.707 at x= 0, h=0.1 obtain a solution by the fourth order Runge-Kutta method for a range x=0 to 0.5
1 st s2, y(1)1 The exact solution is given by yo) - = . 1+Int Write a MATLAB code to approximate the solution of the IVP using Midpoint (RK2) and Modified Euler methods when h [0.5 0.1 0.0s 0.01 0.005 0.001]. A) Find the vector w mid and w mod that approximates the solution of the IVP for different values of h. B) Plot the step-size h versus the relative error of both in the same figure using the LOGLOG...