Question

Consider the following initial value problem:

0.3t

1. Use Euler's explicit scheme to solve the above initial value problem with time step h= 0.5. Express all the computed results with a precision of three decimal places.

2. The analytical or exact solution is 43 1.2t 70 _0.3t--e compute the absolute error at each ti value. Express all the computed results with a precision of three decimal places.

3. Write a matlab function that solves the above (IVP) using (RK2.M) for arbitrary time-step h.

0.3t
43 1.2t 70 _0.3t--e
1 0
Add a comment Improve this question Transcribed image text
Answer #1

Matlab code for solving 1st order ode using RK4 method and its plot clear all close all 酱88,,888888움움88움움88888888888888888888fprint f ( \n\tThe solution at t(3.2f) is %f Exact n,_v vall (end), y ext(end)) %Rk2 solution h-0.5; amount of intervals % it-0.00 y-3.000000 t-0.50y-4.072295 t=1.00 y=4.322880 t-1.50 y-4.169569 The Exact solution at t (1.50) is 4.169569 Y(t) vs. t

%Matlab code for solving 1st order ode using RK4 method and its plot
clear all
close all

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%function for ode
f=@(t,y) -1.2.*y+7.*exp(-0.3.*t);

fprintf('For the function dy/dt= -1.2.*y+7.*exp(-0.3.*t) \n')
%All values for solving the equation

%Euler Method for h=0.5
h=0.5;           % amount of intervals
t=0;             % initial t
y=3;           % initial y

t_eval=1.5;        % at what point we have to evaluate
n=(t_eval-t)/h; % Number of steps
t_val1(1)=t;
y_val1(1)=y;

fprintf('y(t) vs. t tabular form Euler Method\n')
fprintf('\tt=%2.2f \t y=%f\n',t,y)
for i=1:n
  
    %Euler STEPS
  

   dy=h*double(f(t,y));


   t=t+h;
   y=y+dy;
   t_val1(i+1)=t;
   y_val1(i+1)=y;
   fprintf('\tt=%2.2f \t y=%f\n',t,y)

          
end
fprintf('\n\tThe solution using Euler Method for h=%.2f at t(%.2f) is %f\n',h,t_val1(end),y_val1(end))

%function for exact solution
f_ext=@(t) (70/9).*exp(-0.3.*t)-(43/9).*exp(-1.2.*t);

fprintf('Exact solution\n')
for j=1:length(t_val1)
    y_ext(j)=f_ext(t_val1(j));
    fprintf('\tt=%2.2f \t y=%f\n',t_val1(j),y_ext(j))
end

fprintf('\n\tThe Exact solution at t(%.2f) is %f\n',t_val1(end),y_ext(end))

%Rk2 solution
h=0.5;           % amount of intervals
t_in=0;             % initial t
y_in=3;           % initial y

[t_rk,y_rk]= RK2(f,t_in,y_in,t_eval,h);
fprintf('y(t) vs. t tabular form RK4 Method\n')

for j=1:length(t_rk)
  
    fprintf('\tt=%2.2f \t y=%f\n',t_rk(j),y_rk(j))
end

fprintf('\n\tThe RK2 solution for h=%f at t(%.2f) is %f\n',h,t_rk(end),y_rk(end))

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%RK2 function for solving
function [t_rk,y_rk]= RK2(f,t_in,y_in,t_eval,h)
    %h amount of intervals
    %t_in initial t
    %y_in initial y
    %t_eval at what point we have to evaluate
    n=(t_eval-t_in)/h; % Number of steps
    t_rk(1)=t_in;
    y_rk(1)=y_in;
  
    for i=1:n
    %RK2 Steps
       k1=h*double(f(t_in,y_in));
       k2=h*double(f((t_in+h/2),(y_in+k1/2)));
     
       t_in=t_in+h;
       y_in=y_in+k2;
       t_rk(i+1)=t_in;
       y_rk(i+1)=y_in;
    
    end
  
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Add a comment
Know the answer?
Add Answer to:
Consider the following initial value problem: 1. Use Euler's explicit scheme to solve the above initial value probl...
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