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

42 Ansichten (letzte 30 Tage)
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);

Akzeptierte Antwort

Bora Eryilmaz
Bora Eryilmaz am 8 Dez. 2022
Bearbeitet: Bora Eryilmaz am 8 Dez. 2022
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 Kommentare
Eleftherios
Eleftherios am 8 Dez. 2022
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 am 8 Dez. 2022
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jon
Jon am 8 Dez. 2022
Bearbeitet: Jon am 8 Dez. 2022
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

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by