why does the anonymous calculate incorrectly
Mostrar comentarios más antiguos
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
Respuestas (2)
Walter Roberson
el 26 de Mayo de 2017
0 votos
fx1 ends in tan(). fx2 ends in atan()
3 comentarios
Lou Jackson
el 26 de Mayo de 2017
Walter Roberson
el 26 de Mayo de 2017
Please copy and paste your code so I can test further.
Walter Roberson
el 26 de Mayo de 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
Steven Lord
el 26 de Mayo de 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 comentarios
Lou Jackson
el 26 de Mayo de 2017
Editada: Walter Roberson
el 26 de Mayo de 2017
Steven Lord
el 26 de Mayo de 2017
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.
Lou Jackson
el 30 de Mayo de 2017
Walter Roberson
el 30 de Mayo de 2017
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
Lou Jackson
el 30 de Mayo de 2017
Walter Roberson
el 30 de Mayo de 2017
... and I posted that explanation four days ago.
Categorías
Más información sobre Function Creation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!