Question: USING MATLAB ONLY! Please make my code work, I currently have this; x0 = 0; %initial t y0 = 1; %initial y H = 0.1;

 USING MATLAB ONLY! Please make my code work, I currently have

USING MATLAB ONLY!

Please make my code work, I currently have this;

x0 = 0; %initial t

y0 = 1; %initial y

H = 0.1; %step size

N = 20;

y = y0 ;

for i = 0:N

x= x0+i*H;

xx(i+1) = x;

y_Eu(i+1) = y;

y_acc(i+1) = sqrt(((xx(i+1))^2.)+1);

y = Euler(@f_function,x,y,H);

end

%Imp Euler

y = y0;

for i = 0:N

x= x0+i*H;

xx(i+1) = x;

y_Imp(i+1) = y;

y = Imp_Euler(@f_function,x,y,H);

end

%Runge Kutta

y = y0;

for i = 0:N

x= x0+i*H;

xx(i+1) = x;

y_RK(i+1) = y;

y = Runge_Kutta(@f_function,x,y,H);

end

plot (xx,y_acc,xx,y_Eu,'r--',xx,y_Imp,'g-',xx,y_RK,'k-.');

legend('accurate', 'Euler', 'imp_Euler','Runge_Kutta') ;

function y_f = f_function(x,y)

y_f = x/y;

end

Here are the code sources

Euler.m

%Euler.m

function yint = Euler(f_function, x,y,H)

yint = y+H*f_function(x,y) ; end

Imp_Euler.m

% Imp_Euler.m

function yint = Imp_Euler(f_function,x,y,H)

k1 = f_function(x,y) ; k2 = f_function(x+H,y+k1*H) ; yint = y+H*(k1+k2)/2.0 ; end

Runge_Kutta.m

% Runge_Kutta.m

function yint = Runge_Kutta(f_function,x,y,H)

k1 = f_function(x,y) ; k2 = f_function(x+0.5*H,y+0.5*k1*H) ; k3 = f_function(x+0.5*H,y+0.5*k2*H) ; k4 = f_function(x+H,y+k3*H) ;

%fprintf('k1= %12.6f ',k1); %fprintf('k2= %12.6f ',k2); %fprintf('k3= %12.6f ',k3); %fprintf('k4= %12.6f ',k4);

yint = y+H*(k1+2*k2+2*k3+k4)/6.0 ; end

Solve the following ODE within 0s ts 2 dy t dt y (0) = 1 The exact solution is y = V t2 + 1. (a) Solve the equation using the Euler method, then improved Euler method, and then Runge Kutta method (use Eulerm, Imp Euwer. m and Runar Kutta. m). Try different time step At = 0.1 and 0.2. For each time step, calculate the relative error and then plot the relative error vs. time. Plot three graphs in one single plot, each corresponding to a different method

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!