MATLAB ode45 函数 'Too many inputs error'

MATLAB ode45 function 'Too many inputs error'

我的函数是:

function [tout, yout] = Lorenz (rho, x0)
%define constants
sigma = 10;
beta = 8/3;

%initial conditions
y0 = [x0; 1; 0];

f = @(t, y) [sigma*(y(2)-y(1)); (y(1)*(rho-y(3)))-y(2); (y(1)*y(2)) - (beta*y(3))];

[tout, yout] = ode45(f, [0 100], y0, 'RelTol', 1e-6, 'AbsTol', 1e-8);
end

当我运行命令中的函数window和

Lorenz(14,0)

我return

Error using Lorenz>@(t,y)[sigma*(y(2)-y(1));(y(1)*(rho-y(3)))-y(2);(y(1)*y(2))-(beta*y(3))]
Too many input arguments.

如有任何帮助,我们将不胜感激。

尽管 MathWorks 未正式 documented 语法,但 ODE 套件确实接受语法:

[t,y] = ode45(odefun,tspan,y0,options,extra1,extra2,...);

其中 options 应该是由 odeset (not falling inline with the name-value system of other functions) and extra1,extra2,... is any number of extra, solve-constant parameters to be passed to odefun. I imagine it's a hold over from before anonymous functions possessed their own workspace to allow creation-time function parametrization 创建的 struct

因此,由于您传递的选项不是 struct 的一部分,ode45 将语法设为

[t,y] = ode45(odefun,tspan,y0,extra1,extra2,extra3,extra4);

并通过 feval 进行调用 odefun(t,y,extra1,extra2,extra3,extra4)。使用 odeset 的小重写应该可以很好地完成工作:

options      = odeset('RelTol', 1e-6, 'AbsTol', 1e-8);
[tout, yout] = ode45(f, [0 100], y0, options);