How to treat with the error "Undefined function or variable 'M'." where M is mentioned in a Matlab ode function, and it's also a function of defined variables in the Matalb code?

1 view (last 30 days)
I have Matlab code and the function that it calls it.
where inside the ode function file:
M=sqrt(Mo-f*t);
where Mo is defined in the code, f is defined in the file of ode function, and t is the time change through ode function.
When I search for the values of M after succeful run on th Commans Window, Matlab gives the error: Undefined function or variable 'M'.
Thanks in advance for each support.

Accepted Answer

Jan
Jan on 13 Feb 2021
Each function has its own worksapce, whereby "workspace" means the set of locally known variables. If you want to access a value of a variable, which is used inside a function, you have to export this variable as output argument.
  7 Comments
Walter Roberson
Walter Roberson on 14 Feb 2021
Note that it is common for people to expect to be able to get "all" the values of one of the local variables calculated by an ode function that is being executed under the control of ode45() or similar.
Something like that is possible to get, but it is seldom the right thing to ask for. The ode*() functions evaluate at a whole series of points "near" a proposed point; odePQ evaluates at (P+1) points in order to figure out what the output value should be, and another (Q-P) extra points to cross-check that the prediction works out. If the prediction turns out to be too far out of range, then the step fails and is withdrawn and a new smaller step size is tried. This can happen a number of times in steep areas, so dozens of intermediate locations could have been evaluated at in order for the numeric routines to figure out a single good prediction. Also, points outside the boundaries set by event functions could have been evaluated at.
Furthermore, the ode*() functions do not necessarily report the results of any location they actually evaluated at. Especially when a vector of times was given in the tspan, the ode*() functions make make predictions based upon the current step and tolerances and report those rather than at exact values.
It isn't that it isn't possible to get at all of the values that were evaluated at; it is that it is seldom useful to get at all of those values.
The second approch Jan used, of taking the reported outputs and evaluating the ode function at the reported outputs, turns out to be much more useful in practice.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!