please write matlab code for this problem Consider the IVP y\' = 3y, y(0) = 2 0
ID: 3110739 • Letter: P
Question
please write matlab code for this problem
Consider the IVP y' = 3y, y(0) = 2 0 lessthanorequalto t lessthanorequalto 0.5 (a) Determine the Improved Euler's approximation for N = 50, N = 500 and N = 5000. Fill in the following table with the values of the approximations, errors and ratios of consecutive errors at t = 0.5. One of the values has already been entered based on the computations we did above. Recall that the exact solution to the ODE is y = 2e^3t. Include the table in your report, as well as the MATLAB commands used to find the entries. (b) Examine the last column. How does the ratio of the errors relate to the number of steps used? Your answer to this question should confirm the fact that Improved Euler's method is of " order h^2 ", that is, every time the stepsize is decreased by a factor k, the error is reduced (approximately) by a factor of k^2.Explanation / Answer
MATLAB Code for Part (a):
clear
clc
%% N = 5
y = 2;
N = 5;
h = 0.5/N;
for i=1:N
m1 = 3*y;
m2 = 3*(y + h*m1);
y = y + 0.5*h*(m1 + m2);
end
disp('For N = 5');
fprintf('Approximation: %f ', y);
fprintf('Error: %f ', abs(y - 2*exp(3*0.5)));
fprintf('Actual/Approximation: %f ', 2*exp(3*0.5)/y);
%% N = 50
y = 2;
N = 50;
h = 0.5/N;
for i=1:N
m1 = 3*y;
m2 = 3*(y + h*m1);
y = y + 0.5*h*(m1 + m2);
end
disp('For N = 50');
fprintf('Approximation: %f ', y);
fprintf('Error: %f ', abs(y - 2*exp(3*0.5)));
fprintf('Actual/Approximation: %f ', 2*exp(3*0.5)/y);
%% N = 500
y = 2;
N = 500;
h = 0.5/N;
for i=1:N
m1 = 3*y;
m2 = 3*(y + h*m1);
y = y + 0.5*h*(m1 + m2);
end
disp('For N = 500');
fprintf('Approximation: %f ', y);
fprintf('Error: %f ', abs(y - 2*exp(3*0.5)));
fprintf('Actual/Approximation: %f ', 2*exp(3*0.5)/y);
%% N = 5000
y = 2;
N = 5000;
h = 0.5/N;
for i=1:N
m1 = 3*y;
m2 = 3*(y + h*m1);
y = y + 0.5*h*(m1 + m2);
end
disp('For N = 5000');
fprintf('Approximation: %f ', y);
fprintf('Error: %f ', abs(y - 2*exp(3*0.5)));
fprintf('Actual/Approximation: %f ', 2*exp(3*0.5)/y);
Output:
For N = 5
Approximation: 8.803217
Error: 0.160161
Actual/Approximation: 1.018193
For N = 50
Approximation: 8.961406
Error: 0.001972
Actual/Approximation: 1.000220
For N = 500
Approximation: 8.963358
Error: 0.000020
Actual/Approximation: 1.000002
For N = 5000
Approximation: 8.963378
Error: 0.000000
Actual/Approximation: 1.000000
>>
Part (b):
The errors for N=5, 50, 500, 5000:
e1 = 0.160161,
e2 = 0.001972,
e3 = 0.000020,
e4 = 0.000000.
If your observe, e2 = 0.01*e1, i.e., e2 is approximately equal to (k^2)*e1, where k = 10, as the number of steps in second column (50) = 10 times of the number of steps in column one (5).
This is true for the remaining ones too.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.