Question

I need to get all solutions (2 unknowns) from fsolve in python 3.2.

But, it only returns one event though I have set full_output = true in fsolve.

def integrand1(x, b, c):
   return (x/b)**(c-1) * exp(-x/b)

def intergralFunc2(b):
   integral,err = quad(integrand2, 0, 100, args=(b))
   return 100 - integral

def solveFunction():
   sol= fsolve(intergralFunc1, 5, 5, full_output=True)
   return sol

if __name__ == '__main__':
    sol = solveFunction()
    print("sol is ", sol)

I need to know the solution for b and c, but, fsolve() only returns solution for b.

Was it helpful?

Solution

Using fmin (see the documentation) instead of fsolve and changing integralFunc so that it takes a sequence containing b and c and returns abs(100 - integral) will get both parameters.

from scipy.integrate import quad
from scipy.optimize import fmin

def integrand(x, b, c):
   return (x/b)**(c-1) * exp(-x/b)

def intergralFunc(bc):
   integral,err = quad(integrand, 0, 100, args=(bc[0], bc[1]))
   return abs(100 - integral)

def solveFunction():
   sol= fmin(intergralFunc, [5, 5])
   return sol

b, c = solveFunction()

Output:

Optimization terminated successfully.
         Current function value: 0.000000
         Iterations: 49
         Function evaluations: 89

Checking b and c

>>> integral, err = quad(integrand, 0, 100, args=(b, c))
>>> np.allclose(100, integral)
True

EDIT:

If you have to use fsolve, this will work since fsolve expects the shape output of the objective function to be the same same as the input, but you will get slightly different results for b and c than if you use fmin. I get b, c = (4.48517289097, 4.95091626134) with fsolve and b, c = (5.14997777422, 4.85783682022) with fmin

def intergralFunc(bc):
   integral,err = quad(integrand, 0, 100, args=(bc[0], bc[1]))
   return np.array([100 - integral] * 2)

def solveFunction():
   sol= fsolve(intergralFunc, [5, 5])
   return sol
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top