why does the anonymous calculate incorrectly

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

Risposte (2)

Walter Roberson
Walter Roberson il 26 Mag 2017
fx1 ends in tan(). fx2 ends in atan()

3 Commenti

I had miss typed the equation in my post, both end in atan(), I apologize for the confusion
Please copy and paste your code so I can test further.
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

Accedi per commentare.

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 Commenti

Here is a snippet of some of my code. I am actually trying to pass this anonymous function result to another function. I brute forced that answer and then started trying to figure out why it did not work. It had to do with how this anonymous function is defined. I looked at changing the M's as you referenced but that was irrelevant to the problem. So here is some of the code. I will send you more of how I am attempting to use it in another function if needed. Thanks for looking
%%supersonic airflow
clear *
format long
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)))-atan(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)));
y_A=fx(2.595)%trying to figure out why anonymous function is not working
y2=fx2(2.595)%second anonymous function works works for this one
y_B=vM1-theta-y2%checks out close to zero
error = y_A - y_B
What EXACTLY does "did not work" mean? Assume when writing your description that we know the syntax of MATLAB but we have no clue what those equations represent or what the "right answer" should be.
If by "I will send you more" you mean to send it to me via email or via a message through Answers, please post your explanation / clarification / additional information here instead so that everyone can see and contribute.
Fair enough, what I need is for the error to approach zero, as it should if the functions are working correctly or I am coding it correctly.
I am attempting to pass these functions to fzero. I have found out I can make it work but what puzzles me is why y_A and y_B give different answers - they should NOT, as far as I am concerned.
I can give you the code associated with using the fzero command but as far as this is concerned it is noise to the discussion. I am after why y_A is not equal to y_B.
Thanks for your help
n 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
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.
... and I posted that explanation four days ago.

Accedi per commentare.

Categorie

Richiesto:

il 25 Mag 2017

Commentato:

il 30 Mag 2017

Community Treasure Hunt

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

Start Hunting!

Translated by