Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python nsolve/solve triple of equations

Tags:

python

sympy

I keep getting errors when I tried to solve a system of three equations using the following code in python3:


import sympy
from sympy import Symbol, solve, nsolve

x = Symbol('x')
y = Symbol('y')
z = Symbol('z')

eq1 = x - y + 3
eq2 = x + y
eq3 = z - y

print(nsolve( (eq1, eq2, eq3), (x,y,z), (-50,50)))

Here is the error message:

Traceback (most recent call last): File "/usr/lib/python3/dist-packages/mpmath/calculus/optimization.py", line 928, in findroot fx = f(*x0) TypeError: () missing 1 required positional argument: '_Dummy_15'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 1, in File "", line 12, in File "/usr/lib/python3/dist-packages/sympy/solvers/solvers.py", line 2498, in nsolve x = findroot(f, x0, J=J, **kwargs) File "/usr/lib/python3/dist-packages/mpmath/calculus/optimization.py", line 931, in findroot fx = f(x0[0]) TypeError: () missing 2 required positional arguments: '_Dummy_14' and '_Dummy_15'


The strange thing is, the error message goes away if I only solve the first two equation --- by changing the last line of the code to

print(nsolve( (eq1, eq2), (x,y), (-50,50)))

output:

exec(open('bug444.py').read())
[-1.5]
[ 1.5]

I'm baffled; your help is most appreciated!

A few pieces of additional info:

  • I'm using python3.4.0 + sympy 0.7.6-3 on ubuntu 14.04. I got the same error in python2

  • I could solve this system using

    solve( [eq1,eq2,eq3], [x,y,z] )

but this system is just a toy example; in the actual applications the system is non-linear and I need higher precision, and I don't see how to adjust the precision for solve, whereas for nsolve I could use nsolve(... , prec=100)

THANKS!

like image 594
underflow Avatar asked Oct 15 '25 03:10

underflow


1 Answers

In your print statement, you are missing your guess for z:

print(nsolve((eq1, eq2, eq3), (x, y, z), (-50, 50)))

Try this (in most cases, using 1 for all the guesses is fine):

print(nsolve((eq1, eq2, eq3), (x, y, z), (1, 1, 1)))

Output:

[-1.5]
[ 1.5]
[ 1.5]
like image 104
Sash Sinha Avatar answered Oct 17 '25 18:10

Sash Sinha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!