why does the anonymous calculate incorrectly

조회 수: 4 (최근 30일)
Lou Jackson
Lou Jackson 2017년 5월 25일
댓글: Walter Roberson 2017년 5월 30일
in my code below, i am calculating a result two different ways. When I embed more into the anonymous function, it calculates an incorrect answer. Note y_A does not equal y_B. Does anyone have an idea why?
g=1.4;
M1=2;
theta=-15*pi/180;
M=M1;
vM1= sqrt((g+1)/(g-1))*atan(sqrt(((g-1)/(g+1))*(M^2-1)))-atan(sqrt((M^2)-1))
fx = @(M) vM1 - theta - sqrt((g+1)/(g-1))*atan(sqrt(((g-1)/(g+1))*(M^2-1)))-tan(sqrt((M^2)-1));
fx2= @(M)(sqrt((g+1)/(g-1))*atan(sqrt(((g-1)/(g+1))*(M^2-1)))-atan(sqrt((M^2)-1)));
y2=fx2(2.595)%second anonymous function works works for this one
y_A=fx(2.595)%trying to figure out why anonymous function is not working
y_B=vM1-theta-y2%checks out close to zero

답변 (2개)

Walter Roberson
Walter Roberson 2017년 5월 26일
fx1 ends in tan(). fx2 ends in atan()
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 5월 26일
Please copy and paste your code so I can test further.
Walter Roberson
Walter Roberson 2017년 5월 26일
in fx, you have vM1 - theta - expression - atan
if fx2 you have expression - atan
in y_B you have vM1 - theta - fx2, so that is vM1 - theta - (expression - atan), which gives vm1 - theta - expression + atan
Notice you have the opposite signs on the atan

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


Steven Lord
Steven Lord 2017년 5월 26일
One potential problem I noticed is that in your expression for fx, you use vM1 which you had defined on the previous line. The definition of vM1 uses M, which was defined on the previous line. Even though fx and fx2 both include M as an input argument, fx uses the value of vM1 that was previously defined; it does not recalculate vM1 using the value of M that you passed into fx.
But I'm not completely clear exactly what the problem is. What exactly do you expect to happen here? What does "not working" mean? The more detail you provide about what you expect to see and how what you actually saw differs from your expectation, the easier it will be for us to help determine what's wrong.
One more suggestion that may simplify the problem a bit is to create separate anonymous functions or variables for the expressions that you use in multiple function handles. For instance, you compute (g+1)/(g-1) (or its reciprocal) multiple times. Abstract that out.
gpm = (g+1)/(g-1);
Now instead of (g+1)/(g-1) appearing all over your code, you can have gpm instead. This will make your expressions easier to read.
  댓글 수: 6
Lou Jackson
Lou Jackson 2017년 5월 30일
you nailed it Walter, thanks. I had erroneously assumed it was something weird with the anonymous function since I had not seen many examples of its usage as I had desired and since I had beat the code upwards and backwards.
Walter Roberson
Walter Roberson 2017년 5월 30일
... and I posted that explanation four days ago.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by