matlab symbolic subs() does not substitute expression

21 次查看(过去 30 天)
Hi everyone,
I've got a long symbolic expression I would like to simplify by substitution in a live script. Unfortunatelly, I'm not able to provide much information or a lot of code, since there is an NDA but I hope you can help anyways. Sorry for the inconvenience.
Implementation of some equations lead to a quite long expression - so, I've already done some simplifications via simplify(), combine(), subs(), factor() and collect().
My current formulation looks like this:
where
Every variable included is defined as symbolic and dependent to additional symbolic variables (with exception of ).
Coming from this, I would like to substitute:
(similar for subscript v)
I already tried different variations with the subs()-function (Annotation: ff4 is a previous form of ff6 (without two simplification steps)):
syms beta_l beta_v
ff7=subs(ff6,6*(alpha_f^2 + 3*alpha_f + 2)/(6*alpha_f^2 + 7*alpha_f + 2),beta_l)
ff8=subs(ff7,(6*(alpha_v^2 + 3*alpha_v + 2)/(6*alpha_v^2 + 7*alpha_v + 2),beta_v)
eqn_6_f = beta_l == 6*(alpha_f^2 + 3*alpha_f + 2)/(6*alpha_f^2 + 7*alpha_f + 2)
% factor((6*alpha_f^2 + 18*alpha_f + 12)/(6*alpha_f^2 + 7*alpha_f + 2))
subs(ff4,rhs(eqn_6_f),lhs(eqn_6_f))
subs(ff4,6*(alpha_f^2 + 3*alpha_f + 2)/(6*alpha_f^2 + 7*alpha_f + 2),lhs(eqn_6_f))
subs(ff4,6*(alpha_v^2 + 3*alpha_v + 2)/(6*alpha_v^2 + 7*alpha_v + 2),beta_v)
% subexpr(rhs(eqn_6_f),lhs(eqn_6_f))
%subs(ff4,6*(alpha_f^2 + 3*alpha_f + 2)/(6*alpha_f^2 + 7*alpha_f + 2),beta_l)
(also more variation which is not shown herr - like: string2sym(char(6*(alpha_f^2 + 3*alpha_f + 2)/(6*alpha_f^2 + 7*alpha_f + 2))) within the subs()-function)
Currently I'm really confused why the substitution is not working like intended and hopefully you can help me.
Your help is really appreciated greatly.
Thank you so much in advance.
Cheers,
Peter
PS: previosly, I used the subs()-function solely "the other way round" (e.g. subs(equation, symbolic variable x, expression for x)) which worked like intended.
  1 个评论
Peter Weigand
Peter Weigand 2022-9-2
Hi,
I found a workaround. The subs()-function was not able to substitute the whole expression so I tried it with several steps and combinations instead of one single substitution or the steps shown above.
It worked with this procedure:
substitute
with term1,
with term2,
with beta
I'm not sure why this was not working when trying the other combinations - shown above. But now it works and I'm happy.

请先登录,再进行评论。

回答(1 个)

Adarsh
Adarsh 2025-1-29
Based on the code and expression I understand that you are trying to replace a term of a symbolic expression in a larger expression with an alternate symbol.
I have tried to reproduce it and I have found a workaround using the direct substitution with the “subs” function itself, here is the code demonstrating the required substitution using the “subs” function:
syms R_l R_v Xbar_f Xbar_r rho_f rho_v ubar_f ubar_r alpha_f alpha_v pi Xbar_v sigma_1
sigma_1 = 6*alpha_v^2 + 7*alpha_v + 2
sigma_1 = 
mdotff = (R_l^2 * Xbar_f * rho_f * pi * (alpha_f^2 + 3*alpha_f + 2) * 6 * ubar_f/ (6*alpha_f^2 + 7*alpha_f + 2) ...
- R_l^2 * Xbar_r * rho_v * ubar_r * pi * (alpha_v^2 + 3*alpha_v + 2) * 6 / sigma_1 ...
+ R_v^2 * Xbar_v * rho_v * ubar_r * pi * (alpha_v^2 + 3*alpha_v + 2) * 6 / sigma_1)
mdotff = 
ff6 = mdotff;
syms beta_l beta_v
subs(ff6,{ 6*((alpha_f^2 + 3*alpha_f + 2)/(6*alpha_f^2 + 7*alpha_f + 2))},{beta_l})
ans = 
The reason for the inconsistency in substitution is understood by running the two code examples below:
Example 1:
eq = 6*((alpha_f^2 + 3*alpha_f + 2)/(6*alpha_f^2 + 7*alpha_f + 2))
eq = 
Example 2:
eq = 6*(alpha_f^2 + 3*alpha_f + 2)/(6*alpha_f^2 + 7*alpha_f + 2)
eq = 
Here, based on the order of evaluation, the final expression is different for both the expressions, hence the inconsistent results from “subs” function occur.
For better understanding, kindly refer to the following documentation link of “subs” function:
I hope it helps in getting the consistent results.

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by