简体   繁体   中英

solve an integral equation by python

I need to solve an integral equation by python 3.2 in win7.

I want to find an initial guess solution first and then use "fsolve()" to solve it in python.

This is the code:

import numpy as np
from scipy.optimize.minpack import fsolve
from cmath import cos, exp
from scipy.integrate.quadpack import quad

def integrand2(x, b):
    return exp(-x)/b 

def intergralFunc2(b):
    integral,err = quad(integrand2, 0, 10, args=(b))  // **error here** 
    return 0.01 - integral

import matplotlib.pyplot as plt

def findGuess():
    vfunc = np.vectorize(intergralFunc2)
    f = np.linspace(-20, 20,10)
    plt.plot(f, vfunc(f))
    plt.xlabel('guess value')
    plt.show()

def solveFunction():
    y= fsolve(intergralFunc2, 10)
    return y


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

I got error:

 quadpack.error: Supplied function does not return a valid float.

Any help would be appreciated.

Just made the following change and it should work (it worked for me).

remove:

from cmath import exp, cos

include:

from numpy import exp, cos

as explained in the comments, the cmath functions accept only float inputs, not arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM