首页 > 解决方案 > RuntimeError:使用 scipy.optimize.bisect 时无法解析参数

问题描述

我在使用 scipy.optimize.bisect root finder 1的代码时遇到问题。在使用该例程时,我收到以下错误:

Traceback (most recent call last):
File "project1.py", line 100, in <module>
zero1=bisect(rho_function1,x[0],crit,maxiter=1e6,full_output=True)
File "/home/irya/anaconda3/envs/hubble/lib/python3.6/site-packages/scipy        /optimize/zeros.py", line 287, in bisect
r = _zeros._bisect(f,a,b,xtol,rtol,maxiter,args,full_output,disp)
RuntimeError: Unable to parse arguments

我正在使用的代码如下:

import numpy as np
from scipy.optimize import bisect
gamma_default=-0.8
gamma_used=-2.0
def rho_function(x,P_0=5.0e3,B_0=3.0e-6,gamma=gamma_default):
    """function used to solve rho. Here, B_0[G] (default 3.0 uG), _0[K/cm^3] and 
     gamma are constants. The variable x is mean to be rho/rho_0"""
     kb=1.3806e-16 # boltzmann  constant in erg/k
     f= B_0**2/(8.0*np.pi*kb)*(x**(4./3)-1) + P_0*(x**gamma-1)
     return f,P_0,B_0,gamma
def rho_function1(x):
     P_0=5.0e3
     B_0=3.0e-6
     gamma=gamma_default
     """function used to solve rho. Here, B_0[G] (default 3.0 uG),  P_0[K/cm^3] and 
     gamma are constants. The variable x is mean to be rho/rho_0"""
     kb=1.3806e-16 # boltzmann  constant in erg/k
     f= B_0**2/(8.0*np.pi*kb)*(x**(4./3)-1) + P_0*(x**gamma-1)
     return f
def rho_prime_function(x,P_0=5.0e3,B_0=3.0e-6,gamma=gamma_default):
     """Derivative of the rho_function"""
     kb=1.3806e-16 # boltzmann  constant in erg/k
     f=B_0**2/(6.0*np.pi*kb)*x**(1./3) + P_0*gamma*x**(gamma-1)
     return f
def rho_2nd_prime_function(x,P_0=5.0e3,B_0=3.0e-6,gamma=gamma_default):
     kb=1.3806e-16 # boltzmann  constant in erg/k
     f=B_0**2/(18.0*np.pi*kb)*x**(-2./3) + P_0*gamma*(gamma-1)*x**   (gamma-2)
     return f
def magnetic_term(x,B_0=3.0e-6):
     """"Magnetic term of the rho function"""
     kb=1.3806e-16 # boltzmann  constant in erg/k
     f=B_0**2/(8.0*np.pi*kb)*(x**(4./3)-1) 
     return f
def TI_term(x,P_0=5.0e3,gamma=gamma_default):
     f=P_0*(x**gamma-1)
     return f

x=np.arange(0.8,2,0.01)

f,P_0_out,B_0_out,gamma_out=rho_function(x,gamma=gamma_used)
f_prime=rho_prime_function(x,gamma=gamma_used)
b_term=magnetic_term(x)
ti_term=TI_term(x,gamma=gamma_used)

kb=1.3806e-16 
crit=(-B_0_out**2/(6.*np.pi*kb*P_0_out*gamma_out))**(3./(3.*gamma_out-4))
print("crit =",crit)
print("Using interval: a=",x[0],", b=",crit)
#using bisect
zero1=bisect(rho_function1,x[0],crit,maxiter=1e6,full_output=True)

我想知道我将参数传递给 bisect 例程的方式有什么问题。请你帮助我好吗?

干杯,

标签: python-3.xparsingscipybisection

解决方案


参数maxiter必须是整数。1e6是一个浮点数。利用

maxiter=int(1e6)

或者

maxiter=1000000

推荐阅读