Replacing segmented equations within equation with variables

조회 수: 20 (최근 30일)
Kathryn
Kathryn 2023년 8월 30일
편집: Kathryn 2023년 8월 31일
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