Function is not reading input from main script

조회 수: 25 (최근 30일)
ojonugwa adukwu
ojonugwa adukwu 2020년 1월 7일
댓글: ojonugwa adukwu 2020년 1월 10일
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

채택된 답변

Stephen23
Stephen23 2020년 1월 7일
편집: Stephen23 2020년 1월 7일
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:
  댓글 수: 2
ojonugwa adukwu
ojonugwa adukwu 2020년 1월 7일
Many Thanks Mr Stephen. I will take time to go through the links again. I will get back to you soon.
Thanks so much.
Regards.
ojonugwa adukwu
ojonugwa adukwu 2020년 1월 10일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by