ode23,ode113,ode45,怎么選?對比下就知道了|何必手算?Matl


clc
close all
clear
syms y(t) t
equ = diff(y)-y+2*t/y == 0;
cond = y(0) == 1;
res = dsolve(equ, cond)
fplot(res, 'LineWidth', 2);
grid on;
hold on;
fcn = @(t, y)y-2*t/y;
tspan = [0 4];
timerValue = tic;
[t23, y23] = ode23(fcn, tspan, 1);
timer23 = toc(timerValue);
plot(t23, y23, 'LineWidth', 2);
timerValue = tic;
[t113, y113] = ode113(fcn, tspan, 1);
timer113 = toc(timerValue);
plot(t113, y113, 'LineWidth', 2);
timerValue = tic;
[t45, y45] = ode45(fcn, tspan, 1);
timer45 = toc(timerValue);
plot(t45, y45, 'LineWidth', 2);
legend('$\sqrt{1+2t}$', 'ode23', 'ode113', 'ode45', 'Interpreter', 'latex', 'Location', 'northwest');
errorfun = @(t, y)sqrt(sum((sqrt(1+2*t)-y).^2)/length(t));
ode23error = [timer23 errorfun(t23, y23)]
ode113error = [timer113 errorfun(t113, y113)]
ode45error = [timer45 errorfun(t45, y45)]