how to code equation this equation?
1 回表示 (過去 30 日間)
古いコメントを表示
not sure how to code this equation:
I have tried this:
nz = @(z) n(z0)*(Tz0/Tz)^(1+g).*exp(-uc(z))
but I think it is not right.
0 件のコメント
採用された回答
DGM
2022 年 2 月 12 日
編集済み: DGM
2022 年 2 月 12 日
Assuming T is a function:
n = @(z) n(z0)*(T(z0)/T(z))^(1+g).*exp(-u*c(z))
10 件のコメント
Walter Roberson
2022 年 2 月 14 日
No. Read the equation again. Read the middle term, which is written using . Read the last term, which is also written in terms of .
Now look at your proposed implementation. The middle term in your proposed implementation is written in terms of the variable Re so we know that where appears in the equation, it will be replaced with Re in the code. Now look at the last term, which in your code is written in terms of R. The R-type variable in the last term needs to be the same as the R-type variable in the middle term, and since you used Re for the middle term, it follows that you need to use Re in the last term to be consistent with the middle term.
Your proposed code has an * immediately after the @(z) part that introduces the anonymous function and indicates what the dummy parameter names are for the function. That * could only work in that position if MATLAB had a unary asterisk operator... which it does not have. Some programming languages do define a unary asterisk operator; for example in C and C++, a unary asterisk is used to indicate pointer indirection. In C, the expression *(z-z0) would be valid, and would indicate that z-z0 is to be calculated and the difference is to be used as a pointer and the content stored at that pointer was to be extracted and used in further calculation of the expression. But MATLAB does not define any such operator. The * there is a syntax error in MATLAB.
I would also point out that if you continue to use the / operator, then your expression is not vectorized -- and so could not be used to fplot or integrate or anything else that requires a vector result for a vector input.
The code I posted for you in https://www.mathworks.com/matlabcentral/answers/1648105-how-to-code-equation-this-equation#comment_1984045 already took care of all of those problems. I do not understand why you felt it was necessary to revert back to your older incorrect code.
その他の回答 (1 件)
Cesar Cardenas
2022 年 3 月 15 日
2 件のコメント
Voss
2022 年 3 月 15 日
You have an extraneous * there which is causing the error. Change that line to:
a = exp(1./(2*Hn)*z);
exp() is a function. Doing exp*(something) is the same as exp()*(something), i.e., calling exp() with no inputs, which is what the error is saying.
参考
カテゴリ
Help Center および File Exchange で Particle & Nuclear Physics についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!