Matlab doesn run the program , says at the editor 'input argument might be unused ' for t
44 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Eleftherios
il 8 Dic 2022
Commentato: Bora Eryilmaz
il 8 Dic 2022
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);
0 Commenti
Risposta accettata
Bora Eryilmaz
il 8 Dic 2022
Modificato: Bora Eryilmaz
il 8 Dic 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 Commenti
Bora Eryilmaz
il 8 Dic 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
Più risposte (1)
Jon
il 8 Dic 2022
Modificato: Jon
il 8 Dic 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
0 Commenti
Vedere anche
Categorie
Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!