How to make solve() return a purely symbolic output?

4 ビュー (過去 30 日間)
Florian Haeussler
Florian Haeussler 2019 年 4 月 20 日
コメント済み: Walter Roberson 2019 年 4 月 20 日
Hi,
I'm using the live editor and want to format my live script in the following way:
Introduce some measurements like:
syms temp1 temp2 mass1 mass2
assign the values of the measurements:
temp1 = 240 %Kelvin
temp2 = 250 %Kelvin
Then I want to introduce a formula and solve it for the unknown symbolically and also present it symbolically.
But now the solve() funktion gets in the way by already substituting the values of temp1 und temp2.
mass1 = solve( temp1/temp2*mass2+mass1 == 0 , mass1) %formula
Is there a way to prevent that?
Thanks a lot.
FH

採用された回答

Walter Roberson
Walter Roberson 2019 年 4 月 20 日
No there is no way to prevent it. You have to not assign values to the variables if you do not want the values used.
  2 件のコメント
Florian Haeussler
Florian Haeussler 2019 年 4 月 20 日
Thank you.
Walter Roberson
Walter Roberson 2019 年 4 月 20 日
syms temp1
is the same as
temp1 = sym('temp1');
This goes into the Symbolic Engine and finds (or creates) a symbol named temp1 inside the engine, and the symbolic engine returns back an internal reference to the symbol that looks something like "_sym_A830fv32fga_" . That internal reference is then stored inside the MATLAB variable temp1 and temp1 is marked as being class symbolic.
Later when you refer to temp1 then MATLAB fetches its datatype and finds that it is symbolic, and pulls back the "_sym_whatever" reference and passes it to the symbolic engine for the operation you are doing, such as temp1/temp2 -- the symbolic engine would receive something like _divide(_sym_A830fv32fga_,_sym_932vad2230_) . The symbolic engine looks up those _sym references and finds its internal "temp1" and "temp2" symbols and does the symbolic calculation for them inside the symbolic engine, creating a symbolic result. A _sym* reference for it is returned back from the symbolic engine, but the actual expression is not returned. The MATLAB side just knows the datatype and the _sym* references and how to pass them back and forth to the symbolic engine, but has no idea what they mean.
Now, when you then assign 240 to temp1, then that is at the MATLAB level, and the MATLAB temp1 variable no longer stores the sym:"_sym_A830fv32fga_" or whatever: it now stores double:240 . This does not affect the symbolic engine at all: the symbolic engine still knows that "_sym_A830fv32fga_" refers to a symbol "temp1" inside it, but there would be a disconnect between the symbol "temp1" and the MATLAB level "temp1" variable. The MATLAB level would have lost track of the reference to the symbolic variable. Then when you use temp1/temp2 in solve(), MATLAB looks, sees that it is double:240 and uses that value -- and that is done before solve() is called, so there is no way for solve to say "Oh, you must mean the symbolic temp1, not the numeric temp1 that you assigned a value to!". solve() never knows: solve() receives everything by value, and for a symbolic expression it would already have been calculated and transformed into one of the _sym references to an expression that lives inside the symbolic engine. When the symbolic engine gets control, it would look up the correspondance to the symbolic expression in its internal tables and do the appropriate calculations.
So... if you don't want solve to see the numeric temp1, then don't assign a numeric value to temp1 when you want temp1 to refer to the symbol.
What you should learn to do is to calculate symbolically, and later subs() specific numeric values into the formula:
temp1_ = 240
temp2_ = 250
sol = solve(....)
subs(sol, {temp1, temp2}, {temp1_, temp2_})

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by