Why does the SUBS command is not working with numbers?

Hi everyone,
I'm struggling with the subs command: I need to replace all the occurencies of a certain symbolic term "0.8909*Mcf*f" with a symbolic variable called J. If I execute the following line
array_simplified_L = subs(array_simplified, str2sym("0.8909*Mcf*f"),J);
nothing happens, but if I leave out the number and try to subs only "Mcf*f" I manage to do that (i.e. I obtain "0.8909*J"). Is there a problem with numbers and subs command which I am not aware of?
Thanks in advance!

回答(1 个)

Odds are, you don't understand floating point numbers. When you see this value in MATLAB: 0.8909, generally that is not the true value stored internally. All you see is the number, rounded to 4 decimal places. So then, when it goes looking for that value, it does not find it. Essentially, it is looking for EXACTLY that pattern, and it is not to be found, because you only thing 0.8909 is exact.
So it is probably simpler to do the replace as:
array_simplified_L = subs(array_simplified,Mcf*f,J/0.8909);
The net result should be the same.

1 个评论

Is there a way to know the exact value of the number and subs it instead of using the "trick" you just showed me? Because I need to replace not only "0.8909*Mcf*f" but all the following expression with J
"0.8909*Mcf*f - 0.4542*Mcf*c + 0.8909*Mwf*f + 0.8909*Gcfz*Mcf + Mcf*Rf - 1.0*Mcf*Rr - 1.0*Mcf*k*cos(angleD - 0.4715)"

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

产品

版本

R2022a

标签

Community Treasure Hunt

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

Start Hunting!

Translated by