# Replacing segmented equations within equation with variables

1 回表示 (過去 30 日間)
Kathryn 2023 年 8 月 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 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 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
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.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeCommon Operations についてさらに検索

R2022b

### Community Treasure Hunt

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

Start Hunting!