欢迎光临散文网 会员登陆 & 注册

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

2023-07-28 23:39 作者:苍纳斯  | 我要投稿


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)]

ode23,ode113,ode45,怎么选?对比下就知道了|何必手算?Matl的评论 (共 条)

分享到微博请遵守国家法律