Could anyone explain me what a line like this works?

조회 수: 1 (최근 30일)
Douglas Alves
Douglas Alves 2014년 6월 12일
답변: Douglas Alves 2014년 6월 14일
I've got 2 files the first file with a line (1) calls the file (2).
(1) P0=fzero(@(P0) estimateSurfacePressure(P0,false,extPar), [1 25])
(2) function [Ps,Rs]=estimateSurfacePressure(P0,isCompressed,extPar)
So what I know is a value for P0 is chosen between 1 and 25 and then the program will stop when a proper Ps is zero. (There's an fzero inside the 2nd file) what I don't understand is that since at the very last line it says P0 = Ps (so when the program runs P0 chosen by (1) was altered). Then after several times varying P0 in order to find Ps = 0 once it's found when the program comes back to the previous file where the line (1) is. What P0 is is actually the value chosen firstly in order to find Ps = 0 and not the value Ps itself. I thought P0 should be Ps because it's established at the botton of the file (2) P0 = Ps. How come does that happen?

채택된 답변

dpb
dpb 2014년 6월 12일
편집: dpb 2014년 6월 13일
If I interpret the question correctly I suspect the problem is you're confusing the name used as dummy arguments and in a function as being the same variable as a return value in the calling context.
Consider
P0=fzero(@(P0) estimateSurfacePressure(P0,false,extPar), [1 25])
There are two uses of a variable P0 that are separate and distinct here. The LHS is a variable in the calling script or function workspace that receives the result of the eventual final value determined by the function fzero. The other P0 is a dummy argument that is simply a place holder in the (anonymous) function definition that is recipient of the array used when the code is executed. In this case that turns out to be a constant vector [1 25].
Similarly in the second function definition P0 is also a dummy argument that receives the value from the caller by association(*); that the two happen to have the same name is coincidental but has no bearing on anything other than by position. The function returns its first output in the variable Ps which is returned to the caller as the value of the function and that value is assigned to the P0 in the caller's context. The name in the function is immaterial; it's simply a placeholder in that context for the result which gets returned.
Read the section on Programming Scripts and Functions in the documentation for the details of how arguments and association works as well the concepts of "scope" and "lifetime (or extent)"
(*) The content passed to the function when called is the "actual argument" so that internally to the function the local variable of the name of the dummy argument is a copy the passed data (hence "actual"). Note the key point that is somewhat obscured by the choice of names used in your example is that the association is by position/order in the function definition and calling statement not having anything at all to do with the names used.

추가 답변 (1개)

Douglas Alves
Douglas Alves 2014년 6월 14일
I'm 100% sure that was my doubt. I was thinking about it. Maybe the problem was different workspaces and iquality in the variables' names. I see what you mean. Thank you very much

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by