In: Advanced Math
1. Consider the initial value problem y′ =1+y/t, y(1)=3 for1≤t≤2.
• Show that y(t) = t ln t + 3t is the solution to the initial value problem.
• Write a program that implements Euler’s method and the 4th order Runke-Kutta method for the above initial value problem. Use your program to solve with h = 0.1 for Euler’s and h = 0.2 for R-K.
• Include a printout of your code and a printout of the results at each time step.
• Use Taylor’s method to compute w1 with h = 0.1 for orders 1 through 4.
MATLAB Program for EULER METHOD h=0.1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Solving ODE (IVP) using Euler method
clc; % Clears the screen
clear all;
format long
h=0.1 % step size
t = 1:h:2;
y(1) = 3; % initial condition
F_ty = @(t,y)(1+y/t); % Function f(t,y)
for n=1:(length(t)-1)
y(n+1)=y(n)+h*F_ty(t(n),y(n));
end
solution =[t' y'] % to disply all t and corresponding y
values
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
OUTPUT
solution =
1.000000000000000 3.000000000000000
1.100000000000000 3.400000000000000
1.200000000000000 3.809090909090909
1.300000000000000 4.226515151515152
1.400000000000000 4.651631701631701
1.500000000000000 5.083891108891109
1.600000000000000 5.522817182817183
1.700000000000000 5.967993256743257
1.800000000000000 6.419051683610507
1.900000000000000 6.875665666033314
2.000000000000000 7.337542806350856
MATLAB Program for RK METHOD h=0.2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Runge-Kutta 4th order method
clear all
format short
clc;
f=@(t,y)(1+y/t); % y'=f(t,y)
y(1)=3; % index has been taken as i instead of 0
h=0.2;
a=1;b=2; % Range of t a<=t<=b
t=a:h:b;
N=(b-a)/h;
for n=1:(length(t)-1)
k1 = h*f(t(n),y(n));
k2 = h*f(t(n)+0.5*h,y(n)+0.5*k1);
k3 = h*f((t(n)+0.5*h),(y(n)+0.5*k2));
k4 = h*f((t(n)+h),(y(n)+k3));
y(n+1) = y(n) + (1/6)*(k1+2*k2+2*k3+k4) ;
end
solution =[t' y'] % to disply all t and corresponding y
values
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
OUTPUT
solution =
1.0000 3.0000
1.2000 3.8188
1.4000 4.6710
1.6000 5.5520
1.8000 6.4580
2.0000 7.3863