graphics_toolkit("gnuplot")


function [x,t]=odelinsol19(a,t0,x0,L,N)
%explicit Euler solver of a linear ODE: dx/dt=ax x(t0)=x0 a constant
%x(k+1)=x(k)+h*ax(k) x(1)=x0
%INPUT
%a a scalar
%t0 initial time
%x0 initial value i.e x(t0)=x0
%L the end of the time interval L>t0
%N - no of computed numerical approximation i.e. x(k+1) approximate solution at
%    t0+k*h for h=(L-t0)/N x(N+1) approx solution at t=L
%OUTPUT
%x vector of numerical solutions comuted for 
%t - vector of time moments t=[t(k)]=[t0+(k-1)*h]

 h=(L-t0)/N;
 x=zeros(N+1,1);
 t=linspace(t0,L,N+1)';
 x(1)=x0;
 for k=1:N, 
  x(k+1)=x(k)+a*h*x(k);
 end
end

[x,t]=odelinsol19(a=1,t0=0,x0=1,L=1,N=10);
plot(t,x,t,s=exp(a*(t-t0)));
pause(1);
er=norm(x-s,'inf');
rer=norm((x-s)./s,'inf');
[er,rer] %error er=max|x(t(k),solution at t(k)]
% and relative error i.e. |x(t(k)-sol(t(k)|/|sol(t(k))|
[x,t]=odelinsol19(a=1,t0=0,x0=1,L=1,N=100);
plot(t,x,t,exp(a*(t-t0)));
pause(1);


[x,t]=odelinsol19(a=1,t0=0,x0=1,L=10,N=10);
plot(t,x,t,s=exp(a*(t-t0)));
pause(1);

er=norm(x-s,'inf');
rer=norm((x-s)./s,'inf');
[er,rer] %error er=max|x(t(k),solution at t(k)]
% and relative error i.e. |x(t(k)-sol(t(k)|/|sol(t(k))|

[x,t]=odelinsol19(a=1,t0=0,x0=1,L=10,N=100);
plot(t,x,t,s=exp(a*(t-t0)));
pause(1);

er=norm(x-s,'inf');
rer=norm((x-s)./s,'inf');
[er,rer] %error er=max|x(t(k),solution at t(k)]
% and relative error i.e. |x(t(k)-sol(t(k)|/|sol(t(k))|

er=norm(x-s,'inf');
rer=norm((x-s)./s,'inf');
[er,rer] %error er=max|x(t(k),solution at t(k)]
% and relative error i.e. |x(t(k)-sol(t(k)|/|sol(t(k))|
