% CLASS11DEC  Do a first Euler method example, on a problem for which
%             we know the exact answer.

f = @(t,x) 1.025 * x;        % RHS of ODE

t = 0;  x = 1;               % initial values

tf = 3;                      % desired final time for prediction

n = 10                       % number of steps
h = (tf - t) / n;            % number and length of steps

% you may now do this next line 10 times:
%    x = x+h*f(t,x), t=t+h

format long g
exact = exp(1.025 * 3)

for j=1:n
  x = x + h * f(t,x);         % Euler's method
  t = t+h;
end

x
error = abs(x-exact)

% now quickly with n=100000=10^5 steps:
t = 0;  x = 1;                      % reset to initial condition
n = 100000, h = (tf - t)/n;
for j=1:n, x = x + h * f(t,x); end  % Euler's
x, error = abs(x-exact), relerror= error/exact
