underflow
underflow

Reputation: 165

python nsolve/solve triple of equations

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:

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!

Upvotes: 4

Views: 16585

Answers (4)

Sash Sinha
Sash Sinha

Reputation: 22473

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]

Upvotes: 5

lahiru lowe
lahiru lowe

Reputation: 1

your code should be:

nsolve([eq1, eq2, eq3], [x,y,z], [1,1,1])

your code was:

nsolve([eq1, eq2, eq3], [x,y,z], [1,1])

you were mising one guess value in the last argument.

point is: if you are solving for n unknown terms you provide a guess for each unknown term (n guesses in the last argument)

Upvotes: 0

Tanay Agrawal
Tanay Agrawal

Reputation: 411

The Problem is number of variables should be equal to the number of guess vectors,

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

If you're using a numerical solver on a multidimensional problem, it wants to start from somewhere and follow a gradient to the solution. the guess vector is where you start. if there are multiple local minima / maxima in the space, different guess vectors can lead to diffierent outputs. Or an unfortunate guess vector may not converge at all. For a one-dimensional problem the guess vector is just x0. For most functions you can write down easily, almost any vector will converge to the one global solutions.

so (1,1,1) guess vectors here is as good as (-50,50,50) Just don't leave a null space for the sake of program

Upvotes: 1

Moses Koledoye
Moses Koledoye

Reputation: 78564

You can discard the initial guesses/dummies if you use linsolve:

>>> from sympy import linsolve
>>> print(linsolve((eq1, eq2, eq3), x,y,z))
{(-3/2, 3/2, 3/2)}

And then you can use nonlinsolve for your non linear problem set.

Upvotes: 1

Related Questions