Replacing segmented equations within equation with variables

6 views (last 30 days)
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?

Answers (1)

John D'Errico
John D'Errico on 30 Aug 2023
Edited: John D'Errico on 30 Aug 2023
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 Comment
Kathryn
Kathryn on 31 Aug 2023
Edited: Kathryn on 31 Aug 2023
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.

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!