how to code equation this equation?

조회 수: 1 (최근 30일)
Cesar Cardenas
Cesar Cardenas 2022년 2월 12일
댓글: Cesar Cardenas 2022년 3월 15일
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.

채택된 답변

DGM
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
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.
Cesar Cardenas
Cesar Cardenas 2022년 2월 14일
Right, thanks so much for the clarification and explanation, I did not realize the mistakes. Thank you.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Cesar Cardenas
Cesar Cardenas 2022년 3월 15일
Not sure why I'm getting this errors?? As always any help will be appreciated thanks
Error using exp
Not enough input arguments.
Hn = 28.4305;
z = 132;
a = exp*(1./(2*Hn)*z);
  댓글 수: 2
Voss
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.
Cesar Cardenas
Cesar Cardenas 2022년 3월 15일
yes, thanks I didn't see the * Thanks

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Puzzles에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by