%% Forward Euler for ODE
%
% We solve the ordinary differential equation
%
% $$ \frac{\mathrm{d}y}{\mathrm{d}t} = -ky(t) $$
%
% with initial conditions $y(0)=C$, $C=10$, on the interval $(0, T]$, $T=5$,
% and concentration $k=1$ using forward Euler with timestep size
% $\frac{T}{N}$, $N=10$.
%
% We note, that this equation has the known analytical solution 
%
% $$ y(t) = C \mathrm{e}^{-kt}, $$
%
% so we can compare to the numerical result.

N = 10;  % interval [0,T] discretized with N+1 points
C = 10;  % value C (0)
T = 5;   % maximum time
k = 1;   % concentration
h = T/N; % the step size h, we consider equi-distance division

nC = C*ones(N+1,1);  % numerical solution
aC = C*ones(N+1,1);  % analytical solution

for i=1:N
    nC(i+1) = (1-k*h)*nC(i);
    aC(i+1) = C * exp(-k*h*i);
end

x = 0:h:T;          % x-axis values

plot(x,aC,'r-o','LineWidth',2); hold on;
plot(x,nC,'b-o','LineWidth',2); hold on;
legend('analytical solution', 'numerical solution');
