Question

Question 1: Given the initial-value problem 12-21 0 <1 <1, y(0) = 1, 12+10 with exact solution v(t) = 2t +1 t2 + 1 a. Use EulQuestion2: Use the Modified Euler method to approximate the solutions to the above IVP with h=0.1, and compare the results to

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


% % Matlab code for solving ode using Euler Improved Euler and RK4 clear all close all %function for which Solution have to d fprintf(\tFor h=82.4f at t=%2.2f value of y=%f.\n ,h, tend, y_euler(end)) fprintf(\t Error E = %f.\n\n,error) end Answeri% Number of steps n=(t_eval-t)/h; t_imp (1)=t; y_imp (1)=y; for i=1:n %improved Euler steps ml=double(f(t,y)); m2=double(f( (For h=0.1000 at t=1.00 value of y=1.579669. Error E = 0.079669. Solution Using Improved Euler Method For h=0.1000 at t=0.40 v

%%Matlab code for solving ode using Euler Improved Euler and RK4
clear all
close all

%function for which Solution have to do
fun=@(t,y) (2-2.*t.*y)./(t.^2+1);
%initial guess
tinit=0; yinit=1;

%Answering Question 1.
syms y(t)
eqn = diff(y,t) == (2-2.*t.*y)./(t.^2+1);
cond = y(0) == yinit;
ySol(t) = dsolve(eqn,cond);

%displaying result
fprintf('Exact solution phi(t)=')
disp(ySol)

%Answering Question 1.

%all t values
tt=[0.4 1.0];
%All step size
h=0.1;

fprintf('\nSolution Using Euler Method\n')
%loop for all step size and tend
    for jj=1:length(tt)
        tend=tt(jj);
      
        [t_euler,y_euler]=Euler(fun,tinit,yinit,tend,h);
        y_ext=double(ySol(tend));
      
        error = abs(y_ext-y_euler(end));
        fprintf('\tFor h=%2.4f at t=%2.2f value of y=%f.\n ',h,t_euler(end),y_euler(end))
        fprintf('\t Error E = %f.\n\n',error)
    end

%Answering Question 2.

fprintf('\nSolution Using Improved Euler Method\n')
%loop for all step size and tend
    for jj=1:length(tt)
        tend=tt(jj);
      
        [t_euler,y_euler]=ImpEuler(fun,tinit,yinit,tend,h);
        y_ext=double(ySol(tend));
      
        error = abs(y_ext-y_euler(end));
        fprintf('\tFor h=%2.4f at t=%2.2f value of y=%f.\n ',h,tend,y_euler(end))
        fprintf('\t Error E = %f.\n\n',error)
    end      

%Answering Question 4.

fprintf('\nSolution Using RK4 Method\n')
%All step size
%loop for all step size and tend
    for jj=1:length(tt)
        tend=tt(jj);
      
        [t_euler,y_euler]=RK4(fun,tinit,yinit,tend,h);
        y_ext=double(ySol(tend));
      
        error = abs(y_ext-y_euler(end));
        fprintf('\tFor h=%2.4f at t=%2.2f value of y=%f.\n ',h,tend,y_euler(end))
        fprintf('\t Error E = %f.\n\n',error)
    end      

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Matlab function for Euler Method

function [t_euler,y_euler]=Euler(f,tinit,yinit,tend,h)
    %Euler method
    % h amount of intervals
    t=tinit;         % initial t
    y=yinit;         % initial y
    t_eval=tend;     % 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
        %Eular Steps
        m=double(f(t,y));
        t=t+h;
        y=y+h*m;
        t_euler(i+1)=t;
        y_euler(i+1)=y;
    end
  
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Matlab function for Improved Euler Method

function [t_imp,y_imp]=ImpEuler(f,tinit,yinit,tend,h)
    %Euler method
    % h amount of intervals
    t=tinit;         % initial t
    y=yinit;         % initial y
    t_eval=tend;     % at what point we have to evaluate
    n=(t_eval-t)/h; % Number of steps
    t_imp(1)=t;
    y_imp(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_imp(i+1)=y;
       t_imp(i+1)=t;
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Matlab function for Runge Kutta Method

function [t_rk,y_rk]=RK4(f,tinit,yinit,tend,h)
    %Euler method
    % h amount of intervals
    t=tinit;         % initial t
    y=yinit;         % initial y
    t_eval=tend;     % 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+h/2),(y+k1/2)));
       k3=h*double(f((t+h/2),(y+k2/2)));
       k4=h*double(f((t+h),(y+k3)));
       dy=(1/6)*(k1+2*k2+2*k3+k4);
       t=t+h;
       y=y+dy;
       t_rk(i+1)=t;
       y_rk(i+1)=y;
    end
end

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

Add a comment
Know the answer?
Add Answer to:
Question 1: Given the initial-value problem 12-21 0 <1 <1, y(0) = 1, 12+10 with exact...
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