

#testing local truncation error of explicitte Euler


1==1;

function [erm,er,t]=exEulerLTE(f,sol,h,a,b)
%testing lTE of explicite Euler
%INPUT
%f- function handle to vector field
%sol - function handle to solution of ODE
%h- step
%a,b- interval ends
%OUTPUT
%er- vector with local LTE i.e. er(k)=||x(t+h)-x(t)-h*f(t,x(t)|| x=sol t=t(k)=a+(k-1)*h
%erm - max of er
%t - discret time i.e. t=[a a+h ..a+k*h]
%Example:
%f=@(x,t) x;sol=@(t) exp(t); a=0;b=1;h=0.1;[erm,er,t]=exEulerLTE(f,sol,h,a,b);erm
tt=t(1)=a;k=1;
while(tt<=b)
 er(k)=norm(sol(tt+h)-sol(tt)-h*f(sol(tt),tt),'inf');
 tt=t(k+1)=tt+h;
 k++;
end%while
er=er';t=t';
erm=max(er);
end


f=@(x,t) -x;
sol=@(t) exp(-t);
a=0;b=1;
h=0.2;
M=5;
erm=zeros(M,1);
for k=1:M,
 erm(k)=exEulerLTE(f,sol,h,a,b);
 h*=0.5;
end
erm(1:M-1)./erm(2:M)



f=@(x,t) [x(2);-x(1)];
sol=@(t) [cos(t);-sin(t)];
a=0;b=1;
h=0.2;
M=5;
erm=zeros(M,1);
for k=1:M,
 erm(k)=exEulerLTE(f,sol,h,a,b);
 h*=0.5;
end
erm(1:M-1)./erm(2:M)



function [erm,er,t]=imEulerLTE(f,sol,h,a,b)
%testing lTE of implicite Euler
%INPUT
%f- function handle to vector field
%sol - function handle to solution of ODE
%h- step
%a,b- interval ends
%OUTPUT
%er- vector with local LTE i.e. er(k)=||x(t+h)-x(t)-h*f(t+h,x(t+h)|| x=sol t=t(k)=a+(k-1)*h
%erm - max of er
%t - discret time i.e. t=[a a+h ..a+k*h]
%Example:
%f=@(x,t) x;sol=@(t) exp(t); a=0;b=1;h=0.1;[erm,er,t]=imEulerLTE(f,sol,h,a,b);erm
tt=t(1)=a;k=1;
while(tt<=b)
 er(k)=norm(sol(tt+h)-sol(tt)-h*f(sol(tt+h),tt+h),'inf');
 tt=t(k+1)=tt+h;
 k++;
end%while
er=er';t=t';
erm=max(er);
end


f=@(x,t) -x;
sol=@(t) exp(-t);
a=0;b=1;
h=0.2;
M=5;
erm=zeros(M,1);
for k=1:M,
 erm(k)=imEulerLTE(f,sol,h,a,b);
 h*=0.5;
end
erm(1:M-1)./erm(2:M)



f=@(x,t) [x(2);-x(1)];
sol=@(t) [cos(t);-sin(t)];
a=0;b=1;
h=0.2;
M=5;
erm=zeros(M,1);
for k=1:M,
 erm(k)=imEulerLTE(f,sol,h,a,b);
 h*=0.5;
end
erm(1:M-1)./erm(2:M)




function [erm,er,t]=AdamsB2sLTE(f,sol,h,a,b)
%testing lTE of explicit Adams 2 step
%INPUT
%f- function handle to vector field
%sol - function handle to solution of ODE
%h- step
%a,b- interval ends
%OUTPUT
%er- vector with local LTE i.e. 
%er(k)=||x(t+2h)-x(t+h)-0.5h*[3*f(t+h,x(t+h)-f(t,x(t))|| x=sol t=t(k)=a+(k-1)*h
%erm - max of er
%t - discret time i.e. t=[a a+h ..a+k*h]
%Example:
%f=@(x,t) x;sol=@(t) exp(t); a=0;b=1;h=0.1;[erm,er,t]=AdamsB2sLTE(f,sol,h,a,b);erm
tt=t(1)=a;k=1;
while(tt<=b)
 er(k)=norm(sol(tt+2*h)-sol(tt+h)-0.5*h*(3*f(sol(tt+h),tt+h)-f(sol(tt),tt)),'inf');
 tt=t(k+1)=tt+h;
 k++;
end%while
er=er';t=t';
erm=max(er);
end


f=@(x,t) -x;
sol=@(t) exp(-t);
a=0;b=1;
h=0.2;
M=5;
erm=zeros(M,1);
for k=1:M,
 erm(k)=AdamsB2sLTE(f,sol,h,a,b);
 h*=0.5;
end
erm(1:M-1)./erm(2:M)



f=@(x,t) [x(2);-x(1)];
sol=@(t) [cos(t);-sin(t)];
a=0;b=1;
h=0.2;
M=5;
erm=zeros(M,1);
for k=1:M,
 erm(k)=AdamsB2sLTE(f,sol,h,a,b);
 h*=0.5;
end
erm(1:M-1)./erm(2:M)

