how to code equation this equation?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Cesar Cardenas
il 12 Feb 2022
Commentato: Cesar Cardenas
il 15 Mar 2022
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 Commenti
Risposta accettata
DGM
il 12 Feb 2022
Modificato: DGM
il 12 Feb 2022
Assuming T is a function:
n = @(z) n(z0)*(T(z0)/T(z))^(1+g).*exp(-u*c(z))
10 Commenti
Walter Roberson
il 14 Feb 2022
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.
Più risposte (1)
Cesar Cardenas
il 15 Mar 2022
2 Commenti
Voss
il 15 Mar 2022
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.
Vedere anche
Categorie
Scopri di più su Particle & Nuclear Physics in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!