Question

25.5 Solve from 0 to 3 with h = 0.1 using (a) Heun (without corrector) and (b) Ralstons 2nd-order RK method: dy = y sin3 (1)
0 0
Add a comment Improve this question Transcribed image text
Answer #1


Matlab code or Improved Euler and Ralston RK2 method clear all close all %Function for which solution have to do f-2(t,y) y-*plot (t_euler,y_euler, Linewidth, 2) plot (t_rk,y rk, Linewidth,2) xlabel( t) ylabel( y(t) title( Solution plot y(t) vs

%%Matlab code for Improved Euler and Ralston RK2 method
clear all
close all
%Function for which solution have to do
f=@(t,y) y.*(sin(t)).^3;
  
    %Improved Euler method
    h=0.1;           % step size
    t=0;             % initial t
    y=1;             % initial y
    t_eval=3;        % at what point we have to evaluate
    n=(t_eval-t)/h; % Number of steps
    t_euler(1)=t;
    y_euler(1)=y;
    for i=1:n
    %improved Euler steps
       m1=double(f(t,y));
       m2=double(f((t+h),(y+h*m1)));
       y=y+double(h*((m1+m2)/2));
       t=t+h;
       y_euler(i+1)=y;
       t_euler(i+1)=t;
    end
  
    fprintf('\n\tThe solution using improved Euler Method for h=%.2f at x(%.1f) is %f\n',h,t_euler(end),y_euler(end))
  
    %RK4 method
    t=0;             % initial t
    y=1;             % initial y
    t_eval=3;        % at what point we have to evaluate
    n=(t_eval-t)/h; % Number of steps
    t_rk(1)=t;
    y_rk(1)=y;
    for i=1:n
    %RK4 Steps
       k1=h*double(f(t,y));
       k2=h*double(f((t+(2*h/3)),(y+(2*k1/3))));
     
       dy=(1/4)*(k1+3*k2);
       t=t+h;
       y=y+dy;
       t_rk(i+1)=t;
       y_rk(i+1)=y;
    end
    fprintf('\n\tThe solution using Rlaston Runge Kutta 2 for h=%.2f at x(%.1f) is %f\n',h,t_rk(end),y_rk(end))


%%Plotting solution using Euler method
figure(1)
hold on
plot(t_euler,y_euler,'Linewidth',2)
plot(t_rk,y_rk,'Linewidth',2)


xlabel('t')
ylabel('y(t)')
title('Solution plot y(t) vs. t')
legend('Euler Heun Method','Ralston RK2 Method','Location','northwest')
grid on

%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%

Add a comment
Know the answer?
Add Answer to:
25.5 Solve from 0 to 3 with h = 0.1 using (a) Heun (without corrector) and (b) Ralston's 2nd-orde...
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