I just need a Matlab code that would help me do this problem. 2. Consider the in
ID: 3210089 • Letter: I
Question
I just need a Matlab code that would help me do this problem.2. Consider the initial value problem dy dt =-y3, y(0) = 1, oct 2 with the exact solution y()TE Use Euler method to approximate the solution with h = 2 × 10-k, k = 1, 2, 5. (a) For each value of k, compute the exact "maximum error." (b) For each value of k, compute the estimated "maximum error" using the error estimator based on Richardson extrapolation. (c) In a table, display the value of h, the exact max. error, and the estimated max. error. (d) Comment about the efficiency of the error estimator.
Explanation / Answer
clc;
clear all;
close all;
format short
%%%%% a)
f=@(t,y)-y^3;
y(1)=1;
fprintf(' k h Exact Max error ');
for k=1:5
h=2*10^(-k);
t=0:h:2;
y_act=1./((2*t+1).^0.5);
for n=1:length(t)-1
y(n+1)=y(n)+h*feval(f,t(n),y(n));
end
err=max(abs(y_act-y));
fprintf(' %d %f %f ',k,h,err);
end
OUTPUT:
k h Exact Max error
1 0.200000 0.047756
2 0.020000 0.003754
3 0.002000 0.000369
4 0.000200 0.000037
5 0.000020 0.000004