Replacing segmented equations within equation with variables

11 次查看(过去 30 天)
I have a really long and stupid equation and I need to factor out a fraction of variables. For example x/y=z, and my function is filled with x and y variables, but I want to replace them with z while keeping the relationship x/y true. Is there a MATLAB function for this purpose or am I condemned to handwriting it?

回答(1 个)

John D'Errico
John D'Errico 2023-8-30
编辑:John D'Errico 2023-8-30
Literally trivial. For example...
syms x y z
EQ = y/x + sin(x^2/y^2)
EQ = 
simplify(subs(EQ,x,y*z))
ans = 
  1 个评论
Kathryn
Kathryn 2023-8-31
编辑:Kathryn 2023-8-31
So while this problem worked on my first set of code, it didn't work on my second. By that I mean I have this naasty equation:
da_do =
(4*((dl^2*asin((2*(dp^2/4 - s^2)^(1/2))/dl))/4 + (dp^2*asin((2*(dp^2/4 - s^2)^(1/2))/dp))/4 - s*(dp^2/4 - s^2)^(1/2) - (dl*(1 - (4*(dp^2/4 - s^2))/dl^2)^(1/2)*(dp^2/4 - s^2)^(1/2))/2))/(dp^2*pi)
>> simplify(subs(da_do,dl,ro*dp)) %subbing in ro=dl/dp
da_do =
(4*((dp^2*asin((2*(dp^2/4 - s^2)^(1/2))/dp))/4 - s*(dp^2/4 - s^2)^(1/2) + (dp^2*ro^2*asin((2*(dp^2/4 - s^2)^(1/2))/(dp*ro)))/4 - (dp*ro*(1 - (4*(dp^2/4 - s^2))/(dp^2*ro^2))^(1/2)*(dp^2/4 - s^2)^(1/2))/2))/(dp^2*pi)
>> simplify(subs(da_do,s,dp*S)) %subbing in S=s/dp
da_do =
(dp*asin((-dp^2*(4*S^2 - 1))^(1/2)/dp) - 2*S*(-dp^2*(4*S^2 - 1))^(1/2) - ro*((4*S^2 + ro^2 - 1)/ro^2)^(1/2)*(-dp^2*(4*S^2 - 1))^(1/2) + dp*ro^2*asin((-dp^2*(4*S^2 - 1))^(1/2)/(dp*ro)))/(dp*pi)
As you can clearly see, I still have dp variables in my equation, which I need to remove.
I accepted the answer because I saw it worked on the first try removing my dl variables; didn't realize it wouldn't remove the dp variables, too. Sorry.

请先登录,再进行评论。

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by