f = @(t,x) -x;
x_exact = exp(-1);
M = 4;
error = zeros(1,M);
for N = 1:4
dt = 10^(-N);
t = 0:dt:1;
x = zeros(size(t));
x(1) = 1;
for i = 1:length(t)-1
x(i+1) = x(i)+dt*f(t(i),x(i));
end
error(N) = abs(x(end)-x_exact);
end
plot(10.^(-(1:M)),error,’.-Line 21 plot(10.^(-(1:M)),error,’.-‘)
‘)
fitlm(-(1:M),log10(error))
xlabel(‘\Delta t’)
ylabel(‘Error’)