why does the anonymous calculate incorrectly
4 ビュー (過去 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
0 件のコメント
回答 (2 件)
Walter Roberson
2017 年 5 月 26 日
fx1 ends in tan(). fx2 ends in atan()
3 件のコメント
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
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Function Creation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!