Question

PROBLEMS 22.1 Solve the following initial value problem over the interval from 0to2 where yo) 1.Display all your results on t
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 h-0.5; % amRK4 method ,,88움움88움움움을움움움 h=0.5; % amount of intervals fprintf( \nSolution for RK 4 method using step size %2.2f.\n,h) fpr%%Exact solution syms y(t) cond y(0)1; ySolt)dsolve (eqn, cond) fprintf ( Exact solution for given ode is y(t) disp(ysol) yy_Solution for RK4 method using step size 0.50. Initial condition y(0)-1 at t o. at t-0.50 value of y(0.50)=0 . 601570 at t-1.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) y.*t.^2-1.1.*y;


    h=0.5; % 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=2;        % 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

  
    %Euler method
    %%%%%%%%%%%%%%%
    h=0.25;
    fprintf('\nSolution for Euler 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=2;        % 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
    %Euler steps
       m=double(f(t,y));
       y=y+h*m;
       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
    %%%%%%%%%%%%%%%
    h=0.5; % amount of intervals
    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=2;        % 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
  
    %Midpoint method
    h=0.5;
    %%%%%%%%%%%%%%%
    fprintf('\nSolution for Midpoint 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=2;        % 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
    %Midpoint 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) == y*t^2-1.1*y;
        cond = y(0) == 1;
        ySol(t) = dsolve(eqn,cond);
        fprintf('Exact solution for given ode is y(t)=')
        disp(ySol)
        yy_ext(t)=ySol;
        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','Euler Method','RK4 Method','Midpoint Method','Exact solution','Location','northwest')
grid on

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

Add a comment
Know the answer?
Add Answer to:
PROBLEMS 22.1 Solve the following initial value problem over the interval from 0to2 where yo) 1.Display all your results on the same graph. dy=vr2-1.ly dt (a) Analytically. (b) Using Euler'...
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