Shafin M
Shafin M

Reputation: 91

Result from function call is not a proper array of floats using scipy.fsolve

I am trying to solve this simple simultaneous equations using scipy's fsolve function:

x + 2 = 10 & x^2 = 64.

I am expecting 8 as the solution. However I'm getting an error saying "minpack.error: Result from function call is not a proper array of floats."

I am pretty new to python scientific library. Can someone please explain how to solve this error? Thanks!

from scipy.optimize import fsolve

def equations(p):
    x = p
    return (x-8, x**2 - 64)

x =  fsolve(equations, 1)

print(x)

Upvotes: 0

Views: 1526

Answers (1)

Marc
Marc

Reputation: 1629

When you look at how fsolve is defined in the scipy module we see:

def fsolve(func, x0, args=(), fprime=None, full_output=0,
           col_deriv=0, xtol=1.49012e-8, maxfev=0, band=None,
           epsfcn=None, factor=100, diag=None):
    """
    Find the roots of a function.

    Return the roots of the (non-linear) equations defined by
    ``func(x) = 0`` given a starting estimate.

    Parameters
    ----------
    func : callable ``f(x, *args)``
        A function that takes at least one (possibly vector) argument,
        and returns a value of the same length.
    '''

So your input value for p should consist of just as many elements as are returned by your function. Try for example:

from scipy.optimize import fsolve
import numpy as np


def equations(p):
    x1 = p[0]
    x2 = p[1]
    return x1-8, x2**2 - 64

x = fsolve(equations, np.array([1, 2]))

print(x)

which gives 8, 8 as an answer.

Upvotes: 2

Related Questions