Could anyone explain me what a line like this works?
1 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
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.
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!