ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 10 Dec 2017 10:13:34 -0600How to solve a nonlinear equation system numerically?https://ask.sagemath.org/question/40063/how-to-solve-a-nonlinear-equation-system-numerically/I am new to sagemath and am having some trouble by printing the solution of a nonlinear equation system in a numerical way. I tried it with `print(soln[0].n(160))` but that didn't work. Has someone an idea how to overcome this problme?
var('x y')
eq1 = x*y^2 - 5*y + 4*x - 3 == 0
eq2 = x*y - 2*x + 2 == 0
solutions = solve([eq1,eq2],x,y)
sol_n = solutions[0] # save only the negative results
sol_p = solutions[1] # save only the positive results
print(sol_n[0])
print(sol_n[1])
print(sol_p[0])
print(sol_p[1])
Resulting (exact) output:
x == -1/16*I*sqrt(7) + 21/16
y == -1/14*I*sqrt(7) + 1/2
x == 1/16*I*sqrt(7) + 21/16
y == 1/14*I*sqrt(7) + 1/2Sun, 10 Dec 2017 08:52:15 -0600https://ask.sagemath.org/question/40063/how-to-solve-a-nonlinear-equation-system-numerically/Answer by tmonteil for <p>I am new to sagemath and am having some trouble by printing the solution of a nonlinear equation system in a numerical way. I tried it with <code>print(soln[0].n(160))</code> but that didn't work. Has someone an idea how to overcome this problme?</p>
<pre><code>var('x y')
eq1 = x*y^2 - 5*y + 4*x - 3 == 0
eq2 = x*y - 2*x + 2 == 0
solutions = solve([eq1,eq2],x,y)
sol_n = solutions[0] # save only the negative results
sol_p = solutions[1] # save only the positive results
print(sol_n[0])
print(sol_n[1])
print(sol_p[0])
print(sol_p[1])
</code></pre>
<p>Resulting (exact) output: </p>
<pre><code>x == -1/16*I*sqrt(7) + 21/16
y == -1/14*I*sqrt(7) + 1/2
x == 1/16*I*sqrt(7) + 21/16
y == 1/14*I*sqrt(7) + 1/2
</code></pre>
https://ask.sagemath.org/question/40063/how-to-solve-a-nonlinear-equation-system-numerically/?answer=40069#post-id-40069The result you get are expressions (see the `==` sign):
sage: a = sol_n[0]
sage: a
x == -1/16*I*sqrt(7) + 21/16
What you could do is to get their right-hand side, with the `rhs` method:
sage: a.rhs()
-1/16*I*sqrt(7) + 21/16
sage: a.rhs().n(160)
1.3125000000000000000000000000000000000000000000 - 0.16535945694153691190635098460245377660689119894*ISun, 10 Dec 2017 10:13:34 -0600https://ask.sagemath.org/question/40063/how-to-solve-a-nonlinear-equation-system-numerically/?answer=40069#post-id-40069