why does the anonymous calculate incorrectly

4 views (last 30 days)
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

Answers (2)

Walter Roberson
Walter Roberson on 26 May 2017
fx1 ends in tan(). fx2 ends in atan()
  3 Comments
Walter Roberson
Walter Roberson on 26 May 2017
Please copy and paste your code so I can test further.
Walter Roberson
Walter Roberson on 26 May 2017
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

Sign in to comment.


Steven Lord
Steven Lord on 26 May 2017
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 Comments
Lou Jackson
Lou Jackson on 30 May 2017
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 on 30 May 2017
... and I posted that explanation four days ago.

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!