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!

Answers (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 Comment

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)"

Sign in to comment.

Categories

Products

Release

R2022a

Asked:

on 17 Oct 2022

Edited:

on 17 Oct 2022

Community Treasure Hunt

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

Start Hunting!