Thanks for your help. the exact solution is correct for the given differential equation yp=0.5*(3+y)*cos(t) and still the output is not correct even after changing in the function to [row,column] = size(t'). I am going to attach MATLAB results and the exact solution. Thanks in advance.
Errors/misconceptions:
You write in the comment that t
is a column vector. But actually it is a row vector. Thus you do exactly one difference computation, and that on the initial value where there is no difference. Easiest fix, use the transpose of t
to make the assumption true:
[row,column] = size(t');
Your exact solution is not the exact solution of the ODE yp = -(3+y/2)*cos(t) = -0.5*(6+y)*cos(t)
. After separation this gives
6+y(t) = (6+y(0))*exp(-0.5*sin(t))
With both these corrections I get errors 1.953D-07, 4.954D-12, 5.978D-07
for adams, rkf and lsoda
As per comment, to get some better insight, enhance the script to count function evaluations and test multiple error tolerances
clearfunction y = exact(t)y = -6+2*exp(-sin(t)/2);end//functionglobal fcnt;function yp=de(t,y)global fcntfcnt = fcnt + 1;yp = -(3+y/2)*cos(t)end//functiont=linspace(0,%pi,10);y0=exact(t(1));method=["lsode","adams","rkf","stiff"]for k = 1:4for j=5:8tol = 10^(-j);fcnt = 0;if k==1y = ode(y0, 0, t, 0.1*tol, tol, de); elsey = ode(method(k), y0, 0, t, 0.1*tol, tol, de); //t0=0enderr=y-exact(t);Error=mean(abs(err));//*t(length(t));printf("%6s: toll=%6.3g, err=%6.3g, fcnt=%6d\n",method(k),tol,Error,fcnt)endend
This results in the table
lsode: toll= 1e-05, err=2.32e-05, fcnt= 57lsode: toll= 1e-06, err=5.81e-07, fcnt= 63lsode: toll= 1e-07, err=8.43e-08, fcnt= 81lsode: toll= 1e-08, err=6.41e-08, fcnt= 107adams: toll= 1e-05, err=2.21e-05, fcnt= 40adams: toll= 1e-06, err=5.88e-07, fcnt= 47adams: toll= 1e-07, err=7.45e-08, fcnt= 58adams: toll= 1e-08, err=4.29e-08, fcnt= 72rkf: toll= 1e-05, err=7.35e-07, fcnt= 61rkf: toll= 1e-06, err=7.39e-07, fcnt= 61rkf: toll= 1e-07, err=2.24e-07, fcnt= 113rkf: toll= 1e-08, err=2.35e-08, fcnt= 132stiff: toll= 1e-05, err=2.70e-05, fcnt= 72stiff: toll= 1e-06, err=3.40e-06, fcnt= 60stiff: toll= 1e-07, err=5.09e-07, fcnt= 87stiff: toll= 1e-08, err=1.55e-07, fcnt= 121
The exact solution of the given DE