Evaluating polynomial functions to get integer as answer

15 vues (au cours des 30 derniers jours)
Rodrigo Toledo
Rodrigo Toledo le 4 Avr 2021
I am trying to evaluate:
syms x y
eq = (x^2 + y^3 == 31)
solve(eq)
eqs = [x^2 + y^3 == 31, x^2 == 31 - y^3]
S = solve(eq,[x y])
S.x and S.y still not 2 and 3
i am expecting to get as answer two integer: x=2 and y=3. How can i do it?
Thanks

Réponse acceptée

Walter Roberson
Walter Roberson le 4 Avr 2021
Modifié(e) : Walter Roberson le 4 Avr 2021
syms x y integer
eq = (x^2 + y^3 == 31)
eq = 
solx = solve(eq,x,'returnconditions',true)
solx = struct with fields:
x: [2×1 sym] parameters: [1×0 sym] conditions: [2×1 sym]
soly = solve(solx.conditions)
soly = 
3
X = subs(solx.x,y,soly)
X = 
Y = soly
Y = 
3
Caution: this kind of process will not generally attempt to find more than one solution for solx.conditions. But you could
soly = solve(eq,y,'returnconditions',true)
soly = struct with fields:
y: [3×1 sym] parameters: [1×0 sym] conditions: [3×1 sym]
solx = arrayfun(@solve, soly.conditions, 'uniform', 0)
Warning: Unable to find explicit solution. For options, see help.
solx = 3×1 cell array
{0×1 sym} {2×1 sym} {0×1 sym}
X = solx{2}
X = 
Y = subs(soly.y(2), x, X)
Y = 

Plus de réponses (1)

darova
darova le 4 Avr 2021
solve can be used for simple problems. Use fsolve or vpasolve to get numerical results
  1 commentaire
Walter Roberson
Walter Roberson le 4 Avr 2021
Not the point. The point is that solve() is having difficulty processing integer constraints in this case. fsolve and vpasolve have no chance of processing integer constraints.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by