Hi,

I was reading some notes on-line on linear multi-step methods and saw this graph:

I thought it would be good MATLAB-practice to replicate the graph and gave it a go.

To start off with, I did not include Adams-Bashfort, and just used Euler's to solve the equation

. What I end up with is quite far from what i wanted, namely:

Not very linear... The code that includes the loglog plot is;

Code:

yprime = @(t,y) y;
tspan = [0 1];
y0 = 1;
h = logspace(-3,0);
n = length(h);
absErrorEuler = zeros(n,1);
for i = 1:n
[teuler,yeuler] = euler(yprime,tspan,y0,h(i));
absErrorEuler(i) = abs(yeuler(end)-exp(1));
end
figure(2), clf,hold on
set(gca,'XDir','reverse')
loglog(h,absErrorEuler,'kd')
xlabel('h'), ylabel('error at t=1')

The implementation of Euler's is;

Code:

function [t,y] = euler(yprime, tspan, y0, h)
t0 = tspan(1); tfinal = tspan(end);
% set up the t values at which we will approximate the solution
t=[t0:h:tfinal];
% include tfinal even if h does not evenly divide tfinal-t0
if t(end)~=tfinal, t = [t tfinal]; end
% execute Euler's method
y = [y0 zeros(length(y0),length(t)-1)];
for j=1:length(t)-1
y(:,j+1) = y(:,j) + h*feval(yprime,t(j),y(:,j));
end

Before dealing with the details of how to put

on the x and y axes, I need to deal with the fact that the plot should show a linear relationship.

Any suggestions are welcome.

Thanks.