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

1.1k views Asked by At

Im not very good with python and I need to root an equation that contains the unknown inside Bessel functions and the code goes like this:

import scipy.special as sp
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fsolve
import math


eff = 50-10j                
e = 2.25                 
w = 4e9*2*np.pi          
a = 7.5e-6           
c = 3e8                
p = 0.0022/100.         
L = np.sqrt(np.pi*a**2/p) 
lc= 0.05                   
largo=0.38                
ancho=0.34               
prof=2./1000.             
volm=largo*ancho*prof      
volc=np.pi*a**2*lc          
volct=volm*p               
n=volct/volc               
nh=n*1e-4/(largo*ancho)


func = lambda ec : e - (np.pi*a**2/L**2)*(2*ec*(sp.j1((ec**0.5)*w*a/c)/((ec**0.5)*w*(a/c)*sp.j0((ec**0.5)*w*a/c))))/(((((ec**0.5)*w*a/c)**2)*(sp.j1((ec**0.5)*w*a/c))/(((ec**0.5)*w*(a/c)*sp.j0((ec**0.5)*w*a/c))*np.log(L/a)))-1) - eff


# Solver

ec_initial_guess = 0.1
ec_solution = fsolve(func, ec_initial_guess)


print "Solution for ec is = %f" % ec_solution
print "when the value of the expression is %f" % func(ec_solution)

and I get the error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
TypeError: Cannot cast array data from dtype('complex128') to dtype('float64') according to the rule 'safe'

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-13-a9b1acf35136> in <module>()
     40 
     41 ec_initial_guess = 1
---> 42 ec_solution = fsolve(func, ec_initial_guess)
     43 
     44 

/home/feliperossik/anaconda2/lib/python2.7/site-packages/scipy/optimize/minpack.pyc in fsolve(func, x0, args, fprime, full_output, col_deriv, xtol, maxfev, band, epsfcn, factor, diag)
    144                'diag': diag}
    145 
--> 146     res = _root_hybr(func, x0, args, jac=fprime, **options)
    147     if full_output:
    148         x = res['x']

/home/feliperossik/anaconda2/lib/python2.7/site-packages/scipy/optimize/minpack.pyc in _root_hybr(func, x0, args, jac, col_deriv, xtol, maxfev, band, eps, factor, diag, **unknown_options)
    222             maxfev = 200 * (n + 1)
    223         retval = _minpack._hybrd(func, x0, args, 1, xtol, maxfev,
--> 224                                  ml, mu, epsfcn, factor, diag)
    225     else:
    226         _check_func('fsolve', 'fprime', Dfun, x0, args, n, (n, n))

error: Result from function call is not a proper array of floats.

This is my first question here, Im sorry for the eqution format, I dont really know how to write it like I would in Latex.

I'd appreciate any help! Thanks in advance!

edit: Im thinking the complex is not right edit2: The sp.j1 and sp.j0 are Bessel fucntions

1

There are 1 answers

1
MarianD On

fsolve() is only for real functions, and you use in the definition of the ec() lambda function the complex value

    eff = 50-10j