Inputs must be floats, namely single or double.
8 views (last 30 days)
Show older comments
I was given an error message about "Nonscalar arrays of function handles are not allowed; use cell arrays instead." I corrected this error, but now I am receiving an "Inputs must be floats, namely single or double" error."
I am supposed to use ode45 to solve the differential equations.
Any help and advice is much appreciated!
3 Comments
Stephen23
on 15 Feb 2021
I don't think you understand how to use numeric ODE solvers.
You certainly did not follow the ODE solver documentation.
Most of the lines of code in your function file define anonymous functions. I did not count them, but it seems you have defined hundreds and hundreds of anonymous functions. Why?
The variables which you are attempting to return at the end are also anonymous functions. Why?
The ODE solver documentation specifies that the function output should be a column vector of type single or double (i.e. numeric). It does NOT accept function handles as the function output. I doubt that any (or few) of those function handles anywhere in your code are required (when I read through, they just get called and there seems to be no reason why basic numeric operation on basic numeric data could not have been used).
Your approach, with hundreds of anonymous function, is going to be slow and much more complex than it needs to be (if you can get it work). But really your best choice would be to write the function properly using numeric data.
Answers (1)
Walter Roberson
on 15 Feb 2021
You are trying to return function handles but you need to return specific numeric values instead.
When you use ode45, the function invoked is not expecting you to return instructions on how to compute the output for inputs: it is expecting you to just go ahead and compute the outputs yourself.
You should be getting rid of most of the function handles in the sr function, replacing them with numeric calculations given the inputs.
Reserve the function handles for a couple of different situations:
- you are calling a function such as arrayfun() or integral() that needs to be passed a function handle
- you are doing the same kind of calculation in multiple cases in the code with different inputs and using a function makes the code more clear. In this respect note that a true function (even a nested function) is more efficient than an anonymous function
- if you have the same calculation repeatedly with the same inputs, do not use a function for that: calculate once and assign to a variable and use the variable.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!