In: Advanced Math
y′ = t, y(0) = 1, solution: y(t) = 1+t2/2
y′ = 2(t + 1)y, y(0) = 1, solution: y(t) = et2+2t
y′ = 5t4y, y(0) = 1, solution: y(t) = et5
y′ = t3/y2, y(0) = 1, solution: y(t) = (3t4/4 + 1)1/3
For the IVPs above, make a log-log plot of the error of Backward Euler and Implicit Trapezoidal Method, at t = 1 as a function of hwithh=0.1×2−k for0≤k≤5.
MATLAB Code (Backward Euler):
close all
clear
clc
k = 0:1:5;
H = 0.1 * 2.^(-k);
y_exact = @(t) 1 + t^2 / 2;
y(1) = 1; % Initial condition
ti = 0; tf = 1; % Interval of t
yf_vec = []; % Vector for storing y(t = 1) for different step
sizes
for i = 1:length(H)
h = H(i);
t = ti:h:tf;
for j = 1:length(t)-1
syms y_next
sol = solve(y_next == y(j) + h*(t(j+1))); % Solve foy y_next
sol = sol(1);
y(j+1) = double(sol); % Backward Euler Update
end
yf_vec = [yf_vec, y(end)]; % Store the value of y(t = 1)
end
figure, loglog(H, abs(y_exact(1) - yf_vec), 'o-'), xlabel('h'),
ylabel('Error')
title(sprintf('Log-Log plot of Error of Backward Euler''s Method at
t = 1\nODE: y'' = t'))
y_exact = @(t) exp(t^2 + 2*t);
y(1) = 1; % Initial condition
ti = 0; tf = 1; % Interval of t
yf_vec = []; % Vector for storing y(t = 1) for different step
sizes
for i = 1:length(H)
h = H(i);
t = ti:h:tf;
for j = 1:length(t)-1
syms y_next
sol = solve(y_next == y(j) + h*(2*(t(j+1) + 1)*y_next)); % Solve
foy y_next
sol = sol(1);
y(j+1) = double(sol); % Backward Euler Update
end
yf_vec = [yf_vec, y(end)]; % Store the value of y(t = 1)
end
figure, loglog(H, abs(y_exact(1) - yf_vec), 'o-'), xlabel('h'),
ylabel('Error')
title(sprintf('Log-Log plot of Error of Backward Euler''s Method at
t = 1\nODE: y'' = 2(t + 1)y'))
y_exact = @(t) exp(t^5);
y(1) = 1; % Initial condition
ti = 0; tf = 1; % Interval of t
yf_vec = []; % Vector for storing y(t = 1) for different step
sizes
for i = 1:length(H)
h = H(i);
t = ti:h:tf;
for j = 1:length(t)-1
syms y_next
sol = solve(y_next == y(j) + h*(5*t(j+1)^4*y_next)); % Solve foy
y_next
sol = sol(1);
y(j+1) = double(sol); % Backward Euler Update
end
yf_vec = [yf_vec, y(end)]; % Store the value of y(t = 1)
end
figure, loglog(H, abs(y_exact(1) - yf_vec), 'o-'), xlabel('h'),
ylabel('Error')
title(sprintf('Log-Log plot of Error of Backward Euler''s Method at
t = 1\nODE: y'' = 5t^4y'))
y_exact = @(t) (3 * t^4 / 4 + 1)^(1/3);
y(1) = 1; % Initial condition
ti = 0; tf = 1; % Interval of t
yf_vec = []; % Vector for storing y(t = 1) for different step
sizes
for i = 1:length(H)
h = H(i);
t = ti:h:tf;
for j = 1:length(t)-1
syms y_next
sol = solve(y_next == y(j) + h*(t(j+1)^3/y_next^2)); % Solve foy
y_next
sol = sol(1);
y(j+1) = double(sol); % Backward Euler Update
end
yf_vec = [yf_vec, y(end)]; % Store the value of y(t = 1)
end
figure, loglog(H, abs(y_exact(1) - yf_vec), 'o-'), xlabel('h'),
ylabel('Error')
title(sprintf('Log-Log plot of Error of Backward Euler''s Method at
t = 1\nODE: y'' = t^3/y^2'))
Plots:
MATLAB Code (Implicit Trapz.):
close all
clear
clc
k = 0:1:5;
H = 0.1 * 2.^(-k);
y_exact = @(t) 1 + t^2 / 2;
y(1) = 1; % Initial condition
ti = 0; tf = 1; % Interval of t
yf_vec = []; % Vector for storing y(t = 1) for different step
sizes
for i = 1:length(H)
h = H(i);
t = ti:h:tf;
for j = 1:length(t)-1
syms y_next
sol = solve(y_next == y(j) + 0.5*h*(t(j) + t(j+1))); % Solve foy
y_next
sol = sol(1);
y(j+1) = double(sol); % Implicit Trapz. Update
end
yf_vec = [yf_vec, y(end)]; % Store the value of y(t = 1)
end
figure, loglog(H, abs(y_exact(1) - yf_vec), 'o-'), xlabel('h'),
ylabel('Error')
title(sprintf('Log-Log plot of Error of Implicit Trapz. Method at t
= 1\nODE: y'' = t'))
y_exact = @(t) exp(t^2 + 2*t);
y(1) = 1; % Initial condition
ti = 0; tf = 1; % Interval of t
yf_vec = []; % Vector for storing y(t = 1) for different step
sizes
for i = 1:length(H)
h = H(i);
t = ti:h:tf;
for j = 1:length(t)-1
syms y_next
sol = solve(y_next == y(j) + 0.5*h*(2*(t(j) + 1)*y(j) + 2*(t(j+1) +
1)*y_next)); % Solve foy y_next
sol = sol(1);
y(j+1) = double(sol); % Implicit Trapz. Update
end
yf_vec = [yf_vec, y(end)]; % Store the value of y(t = 1)
end
figure, loglog(H, abs(y_exact(1) - yf_vec), 'o-'), xlabel('h'),
ylabel('Error')
title(sprintf('Log-Log plot of Error of Implicit Trapz. Method at t
= 1\nODE: y'' = 2(t + 1)y'))
y_exact = @(t) exp(t^5);
y(1) = 1; % Initial condition
ti = 0; tf = 1; % Interval of t
yf_vec = []; % Vector for storing y(t = 1) for different step
sizes
for i = 1:length(H)
h = H(i);
t = ti:h:tf;
for j = 1:length(t)-1
syms y_next
sol = solve(y_next == y(j) + 0.5*h*(5*t(j)^4*y(j) +
5*t(j+1)^4*y_next)); % Solve foy y_next
sol = sol(1);
y(j+1) = double(sol); % Implicit Trapz. Update
end
yf_vec = [yf_vec, y(end)]; % Store the value of y(t = 1)
end
figure, loglog(H, abs(y_exact(1) - yf_vec), 'o-'), xlabel('h'),
ylabel('Error')
title(sprintf('Log-Log plot of Error of Implicit Trapz. Method at t
= 1\nODE: y'' = 5t^4y'))
y_exact = @(t) (3 * t^4 / 4 + 1)^(1/3);
y(1) = 1; % Initial condition
ti = 0; tf = 1; % Interval of t
yf_vec = []; % Vector for storing y(t = 1) for different step
sizes
for i = 1:length(H)
h = H(i);
t = ti:h:tf;
for j = 1:length(t)-1
syms y_next
sol = solve(y_next == y(j) + 0.5*h*(t(j)^3/y(j)^2 +
t(j+1)^3/y_next^2)); % Solve foy y_next
sol = sol(1);
y(j+1) = double(sol); % Implicit Trapz. Update
end
yf_vec = [yf_vec, y(end)]; % Store the value of y(t = 1)
end
figure, loglog(H, abs(y_exact(1) - yf_vec), 'o-'), xlabel('h'),
ylabel('Error')
title(sprintf('Log-Log plot of Error of Implicit Trapz. Method at t
= 1\nODE: y'' = t^3/y^2'))
Plots: