Function is not reading input from main script
11 views (last 30 days)
Show older comments
ojonugwa adukwu
on 7 Jan 2020
Commented: ojonugwa adukwu
on 10 Jan 2020
My function is not reading my inputs from the main script. When i run the main script, I get the problems. The common problems are; 'Not enough input arguments', 'too many input arguments' and the third thing is that it is not reading some inputs from the main script when the main script calls the function.
My original intention is for the functions to read every input from the main script but as this was giving me too may problems, I decided to copy all the constants to all the scripts and reduce the inputs to the functions to only the ones that changes. This makes the functions look so lengthy though.
I am uplading five files (a script and 4 functions). When I run the script, the above error message appears. What is really the problem?
main_NMPC_tal_AGL is the main script, Integer_S computes the DAE systems, cfunNMPC computes the nonlinear constraints for the fmincon, CostNMPC computes the cost function and sstate computes the system steady state at each point (kept fixed here).
Thanks
Accepted Answer
Stephen23
on 7 Jan 2020
Edited: Stephen23
on 7 Jan 2020
You need to parameterize your function calls:
You need to use function handles instead of strongly discouraged character vectors:
When you read the fmincon documenation it clearly states that the input function "fun is a function that accepts a vector or array x and returns a real scalar f", but you are providing functions with multiple input arguments, which is not supported by the documentation. Your functions need to have one input and one output argument, or whatever the documentation states. That is why the fmincon page links twice to this page:
so that you will know how to parameterize your function and pass all of those other inputs. For example, instead of this strongly discouraged and buggy syntax:
fmincon('costNMPC_Sla_AGL',...
you should be parameterizing a function handle to pass that data correctly, e.g. using an anonymous function:
A = ...;
B = ...;
...
E = ...;
fmincon(@(u)costNMPC_Sla_AGL(u,A,B,C,D,E),...
where A, B, C, D, and E are the other five input arrays that you need to provide to that function.
Or use nested functions, if you prefer. Nested functions are simple and intuitive, although it requires putting all of the functions into one file:
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!