Is there a right order for input variables in a function?

조회 수: 45 (최근 30일)
ojonugwa adukwu
ojonugwa adukwu 2019년 12월 14일
댓글: ojonugwa adukwu 2020년 1월 7일
Good day everyone.
I have written a function that is called from the main program and the function input variables are obtained from the main program. But I observed that the input variables values are mixed up. For example, if the values are a=3; b=9; c=-1; in the main program, these values are mixed up as a=-1; b=3; c=9 or similar thing. Consequently, my program is not running. How do I make the function see the input variables correctly as it is in the main program?
Thanks so much
  댓글 수: 1
ojonugwa adukwu
ojonugwa adukwu 2020년 1월 7일
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).
The file are attached in the link here.
Thanks

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

채택된 답변

Stephen23
Stephen23 2019년 12월 14일
편집: Stephen23 2019년 12월 14일
"Is there a right order for input variables in a function?"
Yes: MATLAB function inputs are positional:
The 1st input provided is the 1st input argument inside the function.
The 2nd input provided is the 2nd input argument inside the function.
etc.
The names of any variables that might be used when calling a function are (and should be) totally irrelevant. Beginners sometimes like to use the same names inside a function and also for the names of variables when they call that function, but often this just adds to their confusion as they think that those two independent sets of names should "match up" somehow, regardless of the order they provide them in.
"How do I make the function see the input variables correctly as it is in the main program?"
You provide the input data in the correct order, based on the specification of that function. For example:
function Z = mysub(A,B)
Z = A - B;
end
This will subtract the second input argument from the first input argument (regardless of any possible variable names that might be used when calling the function):
>> A = 10;
>> B = 4;
>> mysub(A,B)
ans = 6
>> B = 10;
>> A = 4;
>> mysub(B,A)
ans = 6
I strongly recommend that you do NOT write your code assuming that the calling variable names are anything like the ones used inside the function.
  댓글 수: 4
Stephen23
Stephen23 2020년 1월 7일
"Can I add the scripts and the functions for you to help me go through?"
Make a new comment, upload the files, and please also show the complete error message.
ojonugwa adukwu
ojonugwa adukwu 2020년 1월 7일
편집: Stephen23 2020년 1월 7일
Ok. Thanks. I have done that under the topic "Function is not reading input from main script":
Thanks

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by