function euler1

%  error, as function of number of points used, for euler's method
%  y' = f(t,y)  with   y(0) = y0

global lambda

lambda=10;
y0=0.01;
tmax=1;

clf
% get(gcf)
set(gcf,'Position', [1 1078 573 267])

% exact solution at tmax
a0=(1-y0)/y0;
exact=1/(1+a0*exp(-lambda*tmax));

m=1;
for im=1:10
    m=m*4;
    points(im)=m;
    t=linspace(0,tmax,m+1);
    k=t(2)-t(1);
    y_euler=euler(t,y0,k,m+1);
    error(im)=abs(exact-y_euler(m+1));
end

loglog(points,error,'--ob','MarkerSize',8,'LineWidth',1.3)
hold on
grid on
xlabel('M')
ylabel('Error')
% axis([1 1.01e3 1e-16 1]);
set(gca,'ytick',[1e-8 1e-6 1e-4 1e-2 1])
set(gca,'YMinorGrid','off')
set(gca,'FontSize',16,'FontWeight','bold')


% euler method
function y=euler(t,y0,k,n)
y=zeros(n,1);
y(1)=y0;
for i=2:n
    y(i)=y(i-1)+k*f(t(i-1),y(i-1));
end


% right-hand side of DE
function z=f(t,y)
global lambda
z=lambda*y*(1-y);












