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

조회 수: 37 (최근 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