Why MATLAB asks for "more input arguments" in the function which is to be handled by ode45?

3 vues (au cours des 30 derniers jours)
I have following code to solve a system of differential equations
function v_out = system_ex(t,v)
v_out = zeros(2,1);
v_out(1) = v(2); % The dx/dt eq
v_out(2) = -v(1); % The dy/dt eq
[t,v] = ode45(@system_ex,[0,20],[1,1])
end
When I press Run, it gives error that not enough input arguments. However, exactly this is the syntax in the MATLAB documentation and from the source where I have taken this code (link) for ode45. Where I am lacking and why MATLAb says system_ex requires more input arguments?
  1 commentaire
Steven Lord
Steven Lord le 19 Oct 2020
Stephen Cobeldick has explained what you need to do to resolve the problem. I want to explain briefly why what you wrote didn't work.
All those lines do appear in the text of the lecture notes + assignment to which you linked, in section 2.2. However they don't appear together. The section containing the system_ex function is in the first bullet, while the call to ode45 is in the fourth.
If you had called system_ex with two inputs, it would have executed all the lines of code. This include the ode45 call which would call system_ex.
This second call to system_ex would call ode45 which would call system_ex.
This third call to system_ex would call ode45 which would call system_ex.
This fourth call to system_ex would call ode45 which would call system_ex.
... eventually MATLAB would throw an error to break the infinite loop.
Removing the ode45 call from system_ex is the right solution.

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 17 Oct 2020
Modifié(e) : Stephen23 le 17 Oct 2020
"However, exactly this is the syntax in the MATLAB documentation"
I very much doubt that the MATLAB documentation shows any recursive function being used with an ODE solver.
"When I press Run, it gives error that not enough input arguments."
That is not related to the ODE solver, but your use of that awful RUN button: by default it calls functions with zero input arguments, but your function requires two input arguments... by clicking that button you are calling your function without telling it what the input arguments should be, so of course an error is to be expected.
Call your function at the command line and forget about the RUN button
(of course you will need to fix the recursion as well, e.g. by renaming your function)

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by