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!