how to code equation

9 ビュー (過去 30 日間)
Cesar Cardenas
Cesar Cardenas 2023 年 10 月 28 日
コメント済み: Walter Roberson 2023 年 11 月 4 日
Hello, I'm trying to code this equation but I think it is not right. Not sure how to adjust the LHS to avoid errors. Any help will be greatly appreciated. Thanks.
psi - log(psi) = -3 * -4 * log(l/2) + 4 * log(E) + 2 * l/E
  1 件のコメント
Walter Roberson
Walter Roberson 2023 年 10 月 30 日
See by the way https://www.mathworks.com/matlabcentral/answers/225896-how-can-i-calculate-ln-x-in-matlab-code#answer_1195970 in which I talk about how different computer languages represent natural logarithm in practice.

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2023 年 10 月 28 日
Using the Symbolic MAth Toolbox —
syms xi lambda psi
Eqn = psi - log(psi) == -3 * -4 * log(lambda/2) + 4 * log(xi) + 2 * lambda/xi
Eqn = 
You can then manipulate it symbolically and calculate with it symbolically.
.
  18 件のコメント
Cesar Cardenas
Cesar Cardenas 2023 年 11 月 4 日
Right thank you. Could you give at least any clue on how to set up the code for this? I'm not a matlab expert. Thank you.
Walter Roberson
Walter Roberson 2023 年 11 月 4 日
Q = @(v) sym(v);
k = Q(physconst('Boltzmann'));
T = Q(1.2)*(1e6);
mH = Q(1.66)*(1e-27);
G = Q(6.67)*(1e-11);
Ms = Q(1.99)*(1e30);
uc = sqrt(2 * k * T/mH);
rc = G * Ms * mH/4 * k * T;
syms u r real
Eqn = (u/uc)^2 - 2 * log(u/uc) == 4 * log(r/rc) + 4 * log(rc/r) - 3
Eqn = 
simplify(Eqn)
ans = 
sol = simplify(solve(Eqn, u))
Warning: Possibly spurious solutions.
Warning: Solutions are only valid under certain conditions. To include parameters and conditions in the solution, specify the 'ReturnConditions' value as 'true'.
sol = 
sol1 = sol(1);
sol1r = real(sol1);
sol1i = imag(sol1);
tiledlayout('flow');
nexttile(); fplot(sol1r, [0.01 1]); title('real component')
nexttile(); fplot(sol1i, [0.01 1]); title('imaginary component')
Look at the right hand side of your Eqn. You have
4 * log(r/rc) + 4 * log(rc/r) - 3
When r and rc are positive and non-zero then log(r/rc) = -log(rc/r) and the log terms on the right hand side cancel.
Eqn1 = (u/uc)^2 - 2 * log(u/uc) == - 3
Eqn1 = 
solve(Eqn1, u)
ans = Empty sym: 0-by-1
syms x
solve( x^2 - 2*log(x) == -3 )
ans = 
vpa(ans)
ans = 
so u/uc would have to be complex-valued for there to be a solution.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAssumptions についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by