Symbolic calculation with solve command

조회 수: 4 (최근 30일)
Ali Kiral
Ali Kiral 2025년 5월 30일
댓글: Ali Kiral 2025년 6월 1일
I want MATLAB to express k1 k2 and k3 in terms of r through the following code but I can't get any useful result. The equations are pretty nonlinear. Is that the reason?
syms k1 k2 k3 r
eqns = [2*r*k3 + r*k2+r*k1 == 6, 7*r*k1 + 6*r*k2+2*r^2*k2*k3-r*k3 == 48, 8*r*k1-r*k2-r*k3-2*r^2*k1*k2+5*r^2*k1*k3+2*r^3*k1*k2*k3==63];
S = solve(eqns,[k1 k2 k3])
S = struct with fields:
k1: [4×1 sym] k2: [4×1 sym] k3: [4×1 sym]
S.k1
ans = 
S.k2
ans = 
S.k3
ans = 
[SL: formatted code as code and executed it. I also displayed each field of S.]

채택된 답변

Torsten
Torsten 2025년 5월 30일
Use
S = solve(eqns,[k1 k2 k3],'MaxDegree',4)
instead of
S = solve(eqns,[k1 k2 k3])
  댓글 수: 1
Walter Roberson
Walter Roberson 2025년 5월 30일
편집: Walter Roberson 2025년 5월 30일
Be warned that the form without the root() expression is pretty long.
syms k1 k2 k3 r
eqns = [2*r*k3 + r*k2+r*k1 == 6, 7*r*k1 + 6*r*k2+2*r^2*k2*k3-r*k3 == 48, 8*r*k1-r*k2-r*k3-2*r^2*k1*k2+5*r^2*k1*k3+2*r^3*k1*k2*k3==63];
S = solve(eqns, [k1 k2 k3], 'maxdegree', 4)
S = struct with fields:
k1: [4×1 sym] k2: [4×1 sym] k3: [4×1 sym]
S.k1
ans = 
char(S.k1(1))
ans = '-(96*r^2*(1/(40*r) + (9*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(2/3) - (1257*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/3))/(50*r^2) + 23213/(5120*r^4))^(1/2)/(6*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/6)) + (- (23213*(9*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(2/3) - (1257*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/3))/(50*r^2) + 23213/(5120*r^4))^(1/2))/(5120*r^4) - 9*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(2/3)*(9*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(2/3) - (1257*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/3))/(50*r^2) + 23213/(5120*r^4))^(1/2) - (1257*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/3)*(9*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(2/3) - (1257*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/3))/(50*r^2) + 23213/(5120*r^4))^(1/2))/(25*r^2) - (117459*6^(1/2)*(3*3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2) + 1302896879/(2048000*r^6))^(1/2))/(16000*r^3))^(1/2)/(6*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/6)*(9*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(2/3) - (1257*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/3))/(50*r^2) + 23213/(5120*r^4))^(1/4)))^2 - 160*r^3*(1/(40*r) + (9*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(2/3) - (1257*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/3))/(50*r^2) + 23213/(5120*r^4))^(1/2)/(6*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/6)) + (- (23213*(9*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(2/3) - (1257*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/3))/(50*r^2) + 23213/(5120*r^4))^(1/2))/(5120*r^4) - 9*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(2/3)*(9*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(2/3) - (1257*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/3))/(50*r^2) + 23213/(5120*r^4))^(1/2) - (1257*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/3)*(9*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(2/3) - (1257*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/3))/(50*r^2) + 23213/(5120*r^4))^(1/2))/(25*r^2) - (117459*6^(1/2)*(3*3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2) + 1302896879/(2048000*r^6))^(1/2))/(16000*r^3))^(1/2)/(6*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/6)*(9*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(2/3) - (1257*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/3))/(50*r^2) + 23213/(5120*r^4))^(1/4)))^3 - 773*r*(1/(40*r) + (9*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(2/3) - (1257*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/3))/(50*r^2) + 23213/(5120*r^4))^(1/2)/(6*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/6)) + (- (23213*(9*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(2/3) - (1257*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/3))/(50*r^2) + 23213/(5120*r^4))^(1/2))/(5120*r^4) - 9*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(2/3)*(9*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(2/3) - (1257*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/3))/(50*r^2) + 23213/(5120*r^4))^(1/2) - (1257*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/3)*(9*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(2/3) - (1257*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/3))/(50*r^2) + 23213/(5120*r^4))^(1/2))/(25*r^2) - (117459*6^(1/2)*(3*3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2) + 1302896879/(2048000*r^6))^(1/2))/(16000*r^3))^(1/2)/(6*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/6)*(9*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(2/3) - (1257*((3^(1/2)*(245367007511193/(16384000000*r^12))^(1/2))/18 + 1302896879/(110592000*r^6))^(1/3))/(50*r^2) + 23213/(5120*r^4))^(1/4))) + 42)/(27*r)'

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2025년 5월 30일
If you object to the presence of root() in the solutions, since you're using release R2023a or later you can use the rewrite function with the "expandroot" option. You could also try using vpa on that result to try to make it a little easier to read, but the expressions are long and complicated enough that it doesn't help that much IMO.
syms k1 k2 k3 r
eqns = [2*r*k3 + r*k2+r*k1 == 6, 7*r*k1 + 6*r*k2+2*r^2*k2*k3-r*k3 == 48, 8*r*k1-r*k2-r*k3-2*r^2*k1*k2+5*r^2*k1*k3+2*r^3*k1*k2*k3==63];
S = solve(eqns,[k1 k2 k3]);
rewrittenExpression = rewrite(S.k1, "expandroot")
rewrittenExpression = 
vpa(rewrittenExpression, 4)
ans = 
If you don't want to have it written in terms of those intermediate sub-expressions you can change one of the preferences, but again that makes the expression look even more complicated.
previousValue = sympref('AbbreviateOutput',false);
vpa(rewrittenExpression, 4)
ans = 
sympref('AbbreviateOutput',previousValue); % reset the preference
  댓글 수: 1
Ali Kiral
Ali Kiral 2025년 6월 1일
Thank you Steve! Equations are already horribly non-linear and I wasn't expecting a simple expression anyway

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numeric Solvers에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by