Symbolic calculation with solve command

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

Accepted Answer

Torsten
Torsten on 30 May 2025
Use
S = solve(eqns,[k1 k2 k3],'MaxDegree',4)
instead of
S = solve(eqns,[k1 k2 k3])
  1 Comment
Walter Roberson
Walter Roberson on 30 May 2025
Edited: Walter Roberson on 30 May 2025
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)'

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 30 May 2025
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 Comment
Ali Kiral
Ali Kiral on 1 Jun 2025
Thank you Steve! Equations are already horribly non-linear and I wasn't expecting a simple expression anyway

Sign in to comment.

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!