In: Advanced Math
Please solve all parts
Consider the following IVP
y′ = 2t − y + 1, y(0) = 1.
Find an approximation of y(1) using
(a) the Euler’s method with N = 4,
(b) the Euler’s method with N = 8,
(c) the improved Euler’s method with N = 4,
(d) the improved Euler’s method with N = 8.
Solve the IVP, find y(1), and compare the accuracy |y(1)−yN | of the approximations.
Approximating y(1) using two methods with 4 different n values will take much time by hand. So I am providing MATLAB CODE for the same with all output.
part a
%%%%%%%%%%%% MATLAB CODE %%%%%%%%%%
% Euler Method
clear all
clc;
format short
f=@(t,y)(2*t-y+1); % dy/dt=f(t,y)=y-t^2
n=4;
a=0;
b=1;
h=(b-a)/n;
t = a:h:b;
y = zeros(1,length(t));
y(1)=1; % index has been taken as i instead of 0
for n=1:(length(t)-1)
y(n+1) = y(n)+h*f(t(n),y(n));
end
t_y=[t' y'] % Solution of ODE at t=0 to 1
yN=y(n+1) % Solution by Euler method
y1=1.7358 % Analytical solution
Accuracy=abs(y1-yN)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
OUTPUT
t_y =
0 1.0000
0.2500 1.0000
0.5000 1.1250
0.7500 1.3438
1.0000 1.6328
yN = 1.6328
y1 = 1.7358
Accuracy = 0.1030
Part (b)
%%%%%%%%%%%%%%% MATLAB CODE %%%%%%%%%%%%%%%%%
% Euler Method
clear all
clc;
format short
f=@(t,y)(2*t-y+1); % dy/dt=f(t,y)=y-t^2
n=8;
a=0;
b=1;
h=(b-a)/n;
t = a:h:b;
y = zeros(1,length(t));
y(1)=1; % index has been taken as i instead of 0
for n=1:(length(t)-1)
y(n+1) = y(n)+h*f(t(n),y(n));
end
t_y=[t' y'] % Solution of ODE at t=0 to 1
yN=y(n+1) % Solution by Euler method
y1=1.7358 % Analytical solution
Accuracy=abs(y1-yN)
%%%%%%%%%%%%%%%%%%%%%%%%%
OUTPUT
t_y =
0 1.0000
0.1250 1.0000
0.2500 1.0313
0.3750 1.0898
0.5000 1.1724
0.6250 1.2758
0.7500 1.3976
0.8750 1.5354
1.0000 1.6872
yN = 1.6872
y1 = 1.7358
Accuracy = 0.0486
Part (c)
%%%%%%%%%%%%%%%%%%%%%%%% MATLAB CODE %%%%%%%%%
% Modified (Improved) Euler method
clear all
clc;
format short
f=@(t,y)(2*t-y+1); % dy/dt=f(t,y)=y-t^2
n=4;
a=0;
b=1;
h=(b-a)/n;
t = a:h:b;
y = zeros(1,length(t));
y(1)=1; % index has been taken as i instead of 0
for n=1:(length(t)-1)
y(n+1) = y(n)+h*f(t(n)+0.5*h,y(n)+0.5*h*f(t(n),y(n)));
end
t_y=[t' y'] % Solution of ODE at t=0 to 1
yN=y(n+1) % Solution by Modified Euler method
y1=1.7358 % Analytical solution
Accuracy=abs(y1-yN)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
OUTPUT
t_y =
0 1.0000
0.2500 1.0625
0.5000 1.2207
0.7500 1.4537
1.0000 1.7451
yN = 1.7451
y1 = 1.7358
Accuracy = 0.0093
Part(d)
%%%%%%%%%%%%%%% MATLAB CODE
% Improved Euler Method
clear all
clc;
format short
f=@(t,y)(2*t-y+1); % dy/dt=f(t,y)=y-t^2
n=8;
a=0;
b=1;
h=(b-a)/n;
t = a:h:b;
y = zeros(1,length(t));
y(1)=1; % index has been taken as i instead of 0
for n=1:(length(t)-1)
y(n+1) = y(n)+h*f(t(n)+0.5*h,y(n)+0.5*h*f(t(n),y(n)));
end
t_y=[t' y'] % Solution of ODE at t=0 to 1
yN=y(n+1) % Solution by Euler method
y1=1.7358 % Analytical solution
Accuracy=abs(y1-yN)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
OUTPUT
t_y =
0 1.0000
0.1250 1.0156
0.2500 1.0587
0.3750 1.1261
0.5000 1.2148
0.6250 1.3224
0.7500 1.4468
0.8750 1.5858
1.0000 1.7379
yN = 1.7379
y1 = 1.7358
Accuracy = 0.0021