# Revision history [back]

The original scenario involves two variables and only one equation. That is what the "shape mismatch" error is saying. ff needs to return a list. Here is a working example (with two equations):

import scipy.optimize

var('x,y')
f = x^2 + y^2 - 1
g = x - y^2

def ff(v):
x, y = map(float, v)
return [f(x=x, y=y), g(x=x, y=y)]

print scipy.optimize.fsolve(ff, [1, 1])


The code could be slightly more readable if f and g are defined with explicit arguments:

import scipy.optimize

var('x,y')
f(x,y) = x^2 + y^2 - 1
g(x,y) = x - y^2

def ff(v):
x, y = map(float, v)
return [f(x, y), g(x, y)]

print scipy.optimize.fsolve(ff, [1, 1])


In both cases, the result should look like: [ 0.61803399 0.78615138]

The original scenario involves two variables and only one equation. That is what Because ff accepts two variables but does not return a list (or tuple or array, ...) of length 2, the "shape mismatch" error is saying. ff needs to return a list. thrown. Here is a working example (with two equations):

import scipy.optimize

var('x,y')
f = x^2 + y^2 - 1
g = x - y^2

def ff(v):
x, y = map(float, v)
return [f(x=x, y=y), g(x=x, y=y)]

print scipy.optimize.fsolve(ff, [1, 1])


The code could be slightly more readable if f and g are defined with explicit arguments:

import scipy.optimize

var('x,y')
f(x,y) = x^2 + y^2 - 1
g(x,y) = x - y^2

def ff(v):
x, y = map(float, v)
return [f(x, y), g(x, y)]

print scipy.optimize.fsolve(ff, [1, 1])


In both cases, the result should look like: [ 0.61803399 0.78615138]

The original scenario involves two variables and only one equation. Because ff accepts two variables but does not return a list (or tuple tuple, or array, ...) of length 2, the "shape mismatch" error is thrown. Here is a working example (with two equations):

import scipy.optimize

var('x,y')
f = x^2 + y^2 - 1
g = x - y^2

def ff(v):
x, y = map(float, v)
return [f(x=x, y=y), g(x=x, y=y)]

print scipy.optimize.fsolve(ff, [1, 1])


The code could be slightly more readable if f and g are defined with explicit arguments:

import scipy.optimize

var('x,y')
f(x,y) = x^2 + y^2 - 1
g(x,y) = x - y^2

def ff(v):
x, y = map(float, v)
return [f(x, y), g(x, y)]

print scipy.optimize.fsolve(ff, [1, 1])


In both cases, the result should look like: [ 0.61803399 0.78615138]

The original scenario involves two variables and only one equation. Because ff accepts two variables but does not return a list (or tuple, or array, ...) of length 2, the "shape mismatch" error is thrown. Here is a working example (with two equations):equations and minimal modifications):

import scipy.optimize

var('x,y')
f = x^2 + y^2 - 1
g = x - y^2

def ff(v):
x, y = map(float, v)
return [f(x=x, y=y), g(x=x, y=y)]

print scipy.optimize.fsolve(ff, [1, 1])


The code could be Here is a more compact version using lambda functions and the trick described link:here

import scipy.optimize

var('x,y')
f(x,y) = (x^2 + y^2 - 1, x - y^2)

print scipy.optimize.fsolve(lambda v: f(*map(float, v)), (1, 1))


Since you said you have a large number of variables and equations, here is a slightly more readable if f and g are defined with explicit arguments:"scale-friendly" approach (but somewhat kludgy):

import scipy.optimize

var('x,y')
f(x,y) = x^2 # Create symbolic variables "x_0" and "x_1".
# This is a pretty messy kludge
x = tuple(
SR.var(
"".join(['x_', str(t)])
)
for t in range(2)
)

# Represent system of equations as a vector over callable symbolic ring.
# I use the intermediate "exprs" variable to allow breaking up the tuple
# over multiple lines.
exprs = (
x^2 + y^2 - 1
g(x,y) = x - y^2

def ff(v):
x, y = map(float, v)
return [f(x, y), g(x, y)]
x^2 - 1,
x - x^2
)

f(*x) = exprs

# Guess values
guess = (1, 1)

print scipy.optimize.fsolve(ff, [1, 1])
scipy.optimize.fsolve(lambda v: f(*map(float, v)), guess)


In both all cases, the result should look like: [ 0.61803399 0.78615138]

The original scenario involves two variables and only one equation. Because ff accepts two variables but does not return a list (or tuple, or array, ...) of length 2, the "shape mismatch" error is thrown. Here is a working example (with two equations and minimal modifications):

import scipy.optimize

var('x,y')
f = x^2 + y^2 - 1
g = x - y^2

def ff(v):
x, y = map(float, v)
return [f(x=x, y=y), g(x=x, y=y)]

print scipy.optimize.fsolve(ff, [1, 1])


Here is a more compact version using lambda functions and the trick described link:here

import scipy.optimize

var('x,y')
f(x,y) = (x^2 + y^2 - 1, x - y^2)

print scipy.optimize.fsolve(lambda v: f(*map(float, v)), (1, 1))


Since you said you have a large number of variables and equations, here is a slightly more "scale-friendly" approach (but somewhat kludgy):

import scipy.optimize

# Create symbolic variables "x_0" and "x_1".
# This is a pretty messy kludge
x = tuple(
SR.var(
"".join(['x_', str(t)])
)
for t in range(2)
)

# Represent system of equations using "x" as a vector over callable symbolic ring.
defined above.
# I use the intermediate "exprs" variable to allow breaking up the tuple
# over multiple lines.
exprs = (
x^2 + x^2 - 1,
x - x^2
)

f(*x) = exprs

# Guess values
guess = (1, 1)

print scipy.optimize.fsolve(lambda v: f(*map(float, v)), guess)


In all cases, the result should look like: [ 0.61803399 0.78615138]