Finding roots of function in Python
Question: I'm trying to calculate the roots for a function using the scipy function
`fsolve`, but an error keeps flagging:

    
    
    TypeError: 'numpy.array' object is not callable
    

I assume it's probably easier to define the equation as a function but I've
tried that a few times to no avail.

Code:

    
    
    import scipy
    import numpy as np
    import matplotlib.pyplot as plt
    from scipy import optimize
    
    # Constants
    wavelength = 0.6328
    ncore = 1.462420
    nclad = 1.457420
    a = 8.335
    
    # Mode Order
    l = 0
    
    # Mode parameters
    V = (2 * np.pi * a / wavelength) * np.sqrt(ncore**2 - nclad**2)
    U = np.arange(0, V, 0.01)
    W = np.sqrt(V**2-U**2)
    
    func = U * scipy.special.jv(l+1, U) / scipy.special.jv(l, U) - W * scipy.special.kv(l+1, W) / scipy.special.kv(l, W)
    
    from scipy.optimize import fsolve
    x = fsolve(func,0)
    print x
    

StackTrace:

    
    
    Traceback (most recent call last):
    
      File "<ipython-input-52-081a9cc9c0ea>", line 1, in <module>
        runfile('/home/luke/Documents/PythonPrograms/ModeSolver_StepIndex/ModeSolver_StepIndex.py', wdir='/home/luke/Documents/PythonPrograms/ModeSolver_StepIndex')
    
      File "/usr/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 580, in runfile
        execfile(filename, namespace)
    
      File "/home/luke/Documents/PythonPrograms/ModeSolver_StepIndex/ModeSolver_StepIndex.py", line 52, in <module>
        x = fsolve(func,0)
    
      File "/usr/lib64/python2.7/site-packages/scipy/optimize/minpack.py", line 140, in fsolve
        res = _root_hybr(func, x0, args, jac=fprime, **options)
    
      File "/usr/lib64/python2.7/site-packages/scipy/optimize/minpack.py", line 197, in _root_hybr
        shape, dtype = _check_func('fsolve', 'func', func, x0, args, n, (n,))
    
      File "/usr/lib64/python2.7/site-packages/scipy/optimize/minpack.py", line 20, in _check_func
        res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
    
    TypeError: 'numpy.ndarray' object is not callable
    


Answer: That is because fsolve takes a function as argument. Try this, Note you still
will encounter some runtime error , you will have to check if your return from
func is properly constructed, I will leave that for you to figure out.

    
    
    import scipy
    import numpy as np
    import matplotlib.pyplot as plt
    from scipy import optimize
    
    # Constants
    wavelength = 0.6328
    ncore = 1.462420
    nclad = 1.457420
    a = 8.335
    
    # Mode Order
    # l = 0
    
    # Mode parameters
    V = (2 * np.pi * a / wavelength) * np.sqrt(ncore**2 - nclad**2)
    U = np.arange(0, V, 0.01)
    W = np.sqrt(V**2-U**2)
    
    def func(l):
        return U * scipy.special.jv(l+1, U) / scipy.special.jv(l, U) - W * scipy.special.kv(l+1, W) / scipy.special.kv(l, W)
    
    from scipy.optimize import fsolve
    x = fsolve(func,0)
    print x
    

