Question

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 usin

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Matlab code for 2nd order Runge Kutta and Modified Euler clear all close all %function for solving ODE using rk2 %function fonumber of steps n-(t_max-t_in)/h; УЗ (1 )-yE; tici for i-l:n improved Eular steps ml-double (fun(t,yH)); m2-double (fun ( (t+n- (t max-t in)/h; tic; for i-l:n %RK4 STEPS kl-h*double (fun (t, yR)); k2-h double (fun ((tth/2) , (yR+kl/2))); k3-h doubleyt) vs t plot using RK2 method 1.2 RK2 for step length 0.500 RK2 forstep length 0.100 1.18 RK2 for step length O.050 -RK2 forLoglog plot of stepsize vs. rel error 0.02 Midpoint RK2 Modified Euler 0018 0016 0014 0012 001 0.008 0.006 0.004 0.002 0.05 0ytt) vs t plot using rk4 method 1.2 rk4 solution for step length 0.500 rk4 solution for step length 0.100 rk4 solution for st

%%Matlab code for 2nd order Runge Kutta and Modified Euler
clear all
close all
%function for solving ODE using rk2
fun=@(t,y) (y./t)-(y./t).^2;
%function for exact solution
fun_ext=@(t,y) t./(1+log(t));
%all step size
hh=[0.5 0.1 0.05 0.01 0.005 0.001];

for ii=1:length(hh)
  
    t(1)=1;yA(1)=1;   %initial conditions
    t_in=t(1);        %Initial t
    t_max=2;          %Final t
    %Runge Kutta 2 iterations
    h=hh(ii);
    %number of steps
    n=(t_max-t_in)/h;
    tic;
    for i=1:n
     
        k1=h*fun(t(i),yA(i));
        k2=h*fun(t(i)+(1/2)*h,yA(i)+(1/2)*k1);
      
        t(i+1)=t_in+i*h;
        yA(i+1)=double(yA(i)+k2);
    end
    time_mid(ii)=toc;
    %plotting the solution using RK2
    figure(1)
    hold on
    plot(t,yA)
    legendinfo{ii}=sprintf('RK2 for step length %2.3f',h);
    RE_mid(ii)=norm(yA-fun_ext(t));

    clear t; clear yA;
end
    xlabel('t')
    ylabel('Y(t)')
    title('y(t) vs t plot using RK2 method')
    legend(legendinfo,'location','northwest')
  

for ii=1:length(hh)
  
    t=1;
    yH=1;   %initial conditions
    t_in=1;        %Initial t
    t_max=2;       %Final t
    %Modified Euler step size
    h=hh(ii);
    %number of steps
    n=(t_max-t_in)/h;
    t3(1)=1;
    y3(1)=yH;
    tic;
    for i=1:n
    %improved Eular steps
       m1=double(fun(t,yH));
       m2=double(fun((t+h),(yH+h*m1)));
       yH=yH+double(h*((m1+m2)/2));
       t=t+h;
       y3(i+1)=yH;
       t3(i+1)=t;
    end
    time_mod(ii)=toc;
    %plotting the solution using Modified Euler
    figure(2)
    hold on
    plot(t3,y3)
    legendinfo{ii}=sprintf('Improved Euler for step length %2.3f',h);
    RE_mod(ii)=norm(y3-fun_ext(t3));

    clear t3; clear y3;
end
    xlabel('t')
    ylabel('Y(t)')
    title('y(t) vs t plot using improved Euler method')
    legend(legendinfo,'location','northwest')

    figure(3)
hold on
loglog(hh,RE_mid,'-+',hh,RE_mod,'-o')
legend('Midpoint=RK2','Modified Euler')
xlabel('h')
ylabel('Relative error')
title('Loglog plot of stepsize vs. rel error')

figure(4)
hold on
loglog(hh,time_mid,'-+',hh,time_mod,'-o')
legend('Midpoint=RK2','Modified Euler')
xlabel('h')
ylabel('time')
title('Loglog plot of stepsize vs. time')

for ii=1:length(hh)
  
    t=1;
    yR=1;   %initial conditions
    t_in=1;        %Initial t
    t_max=2;       %Final t
    %Modified Euler step size
    h=hh(ii);
    %number of steps
    n=(t_max-t_in)/h;
    t4(1)=1;
    y4(1)=yR;
    tic;

    for i=1:n
    %RK4 STEPS
       k1=h*double(fun(t,yR));
       k2=h*double(fun((t+h/2),(yR+k1/2)));
       k3=h*double(fun((t+h/2),(yR+k2/2)));
       k4=h*double(fun((t+h),(yR+k3)));
       dy=(1/6)*(k1+2*k2+2*k3+k4);
       t=t+h;
       yR=yR+dy;
       t4(i+1)=t;
       y4(i+1)=yR;

    end
    time_rk4(ii)=toc;
    %plotting the solution using Modified Euler
    figure(5)
    hold on
    plot(t4,y4)
    legendinfo{ii}=sprintf('rk4 solution for step length %2.3f',h);
    RE_rk4(ii)=norm(y4-fun_ext(t4));
    clear t4; clear y4;
end

    xlabel('t')
    ylabel('Y(t)')
    title('y(t) vs t plot using rk4 method')
    legend(legendinfo,'location','northwest')
%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%

Add a comment
Know the answer?
Add Answer to:
1 st s2, y(1)1 The exact solution is given by yo) - = . 1+Int Write a MATLAB code to approximate ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT