Replacing segmented equations within equation with variables

8 vues (au cours des 30 derniers jours)
Kathryn
Kathryn le 30 Août 2023
Modifié(e) : Kathryn le 31 Août 2023
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?

Réponses (1)

John D'Errico
John D'Errico le 30 Août 2023
Modifié(e) : John D'Errico le 30 Août 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 commentaire
Kathryn
Kathryn le 31 Août 2023
Modifié(e) : Kathryn le 31 Août 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.

Connectez-vous pour commenter.

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by