MATLAB Answers

Function is not reading input from main script

3 views (last 30 days)
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).

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 7 Jan 2020
Edited: Stephen Cobeldick 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:
you should be parameterizing a function handle to pass that data correctly, e.g. using an anonymous function:
A = ...;
B = ...;
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:


ojonugwa adukwu
ojonugwa adukwu on 7 Jan 2020
Many Thanks Mr Stephen. I will take time to go through the links again. I will get back to you soon.
Thanks so much.
ojonugwa adukwu
ojonugwa adukwu on 10 Jan 2020
Thanks so much Mr Stephen. It is running now after modifying the fmincon inputs arguements. The "false" character vector were replaced with function handles followed by other modification and the program now works.
Thanks so much

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Translated by