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

45 ビュー (過去 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 Center および File ExchangeInteractive Control and Callbacks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by