Question 7.17: ODE23, ODE45 Write a MATLAB script that solves the following...

ODE23, ODE45

Write a MATLAB script that solves the following initial-value problem using ode23 and ode45 solvers and returns a table that includes the solution estimates at t=1:0.2:3, as well as the exact solution and the % relative error for both methods at each point.

ty + y= e^{-t} y(1)=1,  1≤t≤3

disp(' t yode23 yode45 yExact e_23 e_45')
t = 1:0.2:3; y0 = 1;
f = @(t,y)((exp(-t)-y)/t);
[t,y23] = ode23(f,t,y0);
[t,y45] = ode45(f,t,y0);
yExact = matlabFunction(dsolve('t*Dy+y=exp(-t)','y(1)=1'));
for k=1:length(t),
t_coord = t(k);
yode23 = y23(k);
yode45 = y45(k);
yEx = yExact(t(k));
e_23 = (yEx - yode23)/yEx*100;
e_45 = (yEx - yode45)/yEx*100;
fprintf('%6.2f %11.6f%11.6f %11.6f %11.6f %11.8f\n',t _ coord,yode23,
yode45,yEx,e_23,e_45)
end

t yode23 yode45 yExact e_23 e_45
1.00 1.000000 1.000000 1.000000 0.000000 0.00000000
1.20 0.888872 0.888904 0.888904 0.003589 −0.00000000
1.40 0.800866 0.800916 0.800916 0.006202 −0.00000001
1.60 0.728684 0.728739 0.728739 0.007554 −0.00000001
1.80 0.668045 0.668100 0.668100 0.008314 −0.00000001
2.00 0.616218 0.616272 0.616272 0.008770 −0.00000001
2.20 0.571347 0.571398 0.571398 0.009058 −0.00000001
2.40 0.532101 0.532151 0.532151 0.009250 −0.00000001
2.60 0.497494 0.497541 0.497541 0.009383 −0.00000001
2.80 0.466766 0.466810 0.466810 0.009479 −0.00000001
3.00 0.439323 0.439364 0.439364 0.009470 −0.00000001

As expected, ode45 produces much more accurate estimates but is often slower than ode23.

Related Answered Questions