Matlab doesn run the program , says at the editor 'input argument might be unused ' for t

15 次查看(过去 30 天)
function dxdt = odefun(t, x)
dxdt = zeros(3,1);
dxdt(1) = - (8/3)*x(1) + x(2)*x(3);
dxdt(2) = - 10*x(2) + 10*x(3);
dxdt(3) = - x(3) - x(2)*x(1) + 28*x(2);
end
>> tspan = [0 20];
>> x0 = [20 -5 5];
>> [t,x]=ode45(@(t,x)odefun(t,x),tspan,x0);
Unrecognized function or variable 'odefun'.
Error in @(t,x)odefun(t,x)
Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);

采纳的回答

Bora Eryilmaz
Bora Eryilmaz 2022-12-8
编辑:Bora Eryilmaz 2022-12-8
You have to save your odefun function into a MATLAB file with the name odefun.m.
The message "input argument might be unused ' for t" is just a warning that you can ignore. It is not your main error.
Alternatively, you can create a MATLAB script and use the following code in there. Note that the "local" function odefun is now in the same MATLAB script:
tspan = [0 20];
x0 = [20 -5 5];
[t,x]=ode45(@(t,x)odefun(t,x),tspan,x0);
ode45(@(t,x)odefun(t,x),tspan,x0); % For visualization
function dxdt = odefun(t, x)
dxdt = zeros(3,1);
dxdt(1) = - (8/3)*x(1) + x(2)*x(3);
dxdt(2) = - 10*x(2) + 10*x(3);
dxdt(3) = - x(3) - x(2)*x(1) + 28*x(2);
end
  2 个评论
Eleftherios
Eleftherios 2022-12-8
I saved with this name odefun.m but when i write the code at the command window the program doesn't run . Again the same messages.
Unrecognized function or variable 'odefun'.
Error in @(t,x)odefun(t,x)
Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
Bora Eryilmaz
Bora Eryilmaz 2022-12-8
You need to save only this part of the code in the file named odefun.m:
function dxdt = odefun(t, x)
dxdt = zeros(3,1);
dxdt(1) = - (8/3)*x(1) + x(2)*x(3);
dxdt(2) = - 10*x(2) + 10*x(3);
dxdt(3) = - x(3) - x(2)*x(1) + 28*x(2);
end
Then as long as the file's directory is on your MATLAB path or you are currently in that directory, you should be able to call it from another MATLAB file/script that contains:
tspan = [0 20];
x0 = [20 -5 5];
[t,x]=ode45(@(t,x)odefun(t,x),tspan,x0);
ode45(@(t,x)odefun(t,x),tspan,x0); % For visualization

请先登录,再进行评论。

更多回答(1 个)

Jon
Jon 2022-12-8
编辑:Jon 2022-12-8
If you have your odefun stored in it's own .m file then the syntax for calling ode45 is just
[t,x]=ode45(@odefun,tspan,x0);
For future reference, you can use the Code button on the MATLAB Answers toolbar to include nicely formatted MATLAB code that can be easily read, and also copied and pasted

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by