Question

Solve the following initial value problem over the interval from t= 0 to 1 where y(O) = 1 using the following methods with a

Display all methods listed below in ONE GRAPH:

1. Analytical method

2. Euler's method

3. Heun's method without iteration

4. Ralston's method

5. Fourth-order RK method

Metlab preferred

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


SMatlab code for Euler, Mid Point, RK2 and RK4 method clear all close all %Function for which solution have to do f-L(t,y) (1RK4 method tprint f ( \nS olution for RK4 method using step size %2.2f.\n,h) fprint f ( Initial condition y(0)=1 at t=0.\neqn -diff (y,t)(1+4*t)sqrt(y)i cond y (0)1; ySol(t) = dsolve (eqn, cond); fprintf( Exact solution for given ode is y(t)- disSolution for Ralston method using step size 0.25 initial condition y(0)=1 at t=0. at t 0.25 value of y(0.25)-1.404508 at t-0.

%%Matlab code for Euler, Mid Point, RK2 and RK4 method
clear all
close all
%Function for which solution have to do
f=@(t,y) (1+4.*t).*sqrt(y);


    h=0.25; % amount of intervals
    fprintf('\nSolution for Euler method using step size %2.2f.\n',h)
    fprintf('Initial condition y(0)=1 at t=0.\n')
    %Euler method
    %%%%%%%%%%%%%%%        
    t=0;             % initial t
    y=1;             % initial y
    t_eval=1;        % at what point we have to evaluate
    n=(t_eval-t)/h; % Number of steps
    t2(1)=t;
    y2(1)=y;
    for i=1:n
        %Eular Steps
        m=double(f(t,y));
        t=t+h;
        y=y+h*m;
        t2(i+1)=t;
        y2(i+1)=y;
        fprintf('\t at t=%2.2f value of y(%2.2f)=%f\n',t2(i+1),t2(i+1),y2(i+1))
    end

  
    %Huen method
    %%%%%%%%%%%%%%%
    fprintf('\nSolution for Heun method using step size %2.2f.\n',h)
    fprintf('Initial condition y(0)=1 at t=0.\n')
    t=0;             % initial t
    y=1;             % initial y
    t_eval=1;        % at what point we have to evaluate
    n=(t_eval-t)/h; % Number of steps
    t3(1)=t;
    y3(1)=y;
    for i=1:n
    %Huen steps
       m1=double(f(t,y));
       m2=double(f((t+h),(y+h*m1)));
       y=y+double(h*((m1+m2)/2));
       t=t+h;
       y3(i+1)=y;
       t3(i+1)=t;
       fprintf('\t at t=%2.2f value of y(%2.2f)=%f\n',t3(i+1),t3(i+1),y3(i+1))
    end

  
    %RK4 method
    %%%%%%%%%%%%%%%
    fprintf('\nSolution for RK4 method using step size %2.2f.\n',h)
    fprintf('Initial condition y(0)=1 at t=0.\n')
    t=0;             % initial t
    y=1;             % initial y
    t_eval=1;        % at what point we have to evaluate
    n=(t_eval-t)/h; % Number of steps
    t4(1)=t;
    y4(1)=y;
    for i=1:n
    %RK4 Steps
       k1=h*double(f(t,y));
       k2=h*double(f((t+h/2),(y+k1/2)));
       k3=h*double(f((t+h/2),(y+k2/2)));
       k4=h*double(f((t+h),(y+k3)));
       dx=(1/6)*(k1+2*k2+2*k3+k4);
       t=t+h;
       y=y+dx;
       t4(i+1)=t;
       y4(i+1)=y;
       fprintf('\t at t=%2.2f value of y(%2.2f)=%f\n',t4(i+1),t4(i+1),y4(i+1))
    end
  
    %Ralston method
    %%%%%%%%%%%%%%%
    fprintf('\nSolution for Ralston method using step size %2.2f.\n',h)
    fprintf('Initial condition y(0)=1 at t=0.\n')
    t=0;             % initial t
    y=1;             % initial y
    t_eval=1;        % at what point we have to evaluate
    n=(t_eval-t)/h; % Number of steps
    t5(1)=t;
    y5(1)=y;
    for i=1:n
    %RK2 Steps
       k1=h*double(f(t,y));
       k2=h*double(f((t+h),(y+k1)));
     
       dx=(1/2)*(k1+k2);
       t=t+h;
       y=y+dx;
       t5(i+1)=t;
       y5(i+1)=y;
       fprintf('\t at t=%2.2f value of y(%2.2f)=%f\n',t5(i+1),t5(i+1),y5(i+1))
    end

  
     %%Exact solution
     syms y(t)
        eqn = diff(y,t) == (1+4*t)*sqrt(y);
        cond = y(0) == 1;
        ySol(t) = dsolve(eqn,cond);
        fprintf('Exact solution for given ode is y(t)=')
        disp(ySol)
        yy_ext(t)=(t*(2*t + 1) + 2)^2/4;
        fprintf('Initial condition y(0)=1 at t=0.\n')
        for ii=1:length(t4)
            y6(ii)=double(yy_ext(t4(ii)));
            fprintf('\t at t=%2.2f value of y(%2.2f)=%f\n',t4(ii),t4(ii),y6(ii))
        end

%%Plotting solution using Euler method
figure(1)
hold on
plot(t2,y2,'Linewidth',2)
plot(t3,y3,'Linewidth',2)
plot(t4,y4,'Linewidth',2)
plot(t5,y5,'linewidth',2)
plot(t4,y6,'linewidth',2)

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

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

Add a comment
Know the answer?
Add Answer to:
Display all methods listed below in ONE GRAPH: 1. Analytical method 2. Euler's method 3. Heun's method without iteration 4. Ralston's method 5. Fourth-order RK method Metlab preferred Sol...
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