函数调用的结果不是正确的浮点数组。 fsolve

时间:2016-12-13 00:16:49

标签: python arrays solver

我对python不是很好,我需要根据贝塞尔函数内部包含未知的方程,代码如下:

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)

我收到错误:

---------------------------------------------------------------------------
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.

这是我在这里的第一个问题,对于eqution格式我很抱歉,我真的不知道怎么写它就像在Latex中那样。

我很感激任何帮助!提前谢谢!

编辑:我认为复杂是不对的 edit2:sp.j1和sp.j0是Bessel fucntions

1 个答案:

答案 0 :(得分:2)

fsolve()仅用于实函数,并且在ec() lambda函数的定义中使用复数值

    eff = 50-10j