python-3.x - 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 例程的方式有什么问题。请你帮助我好吗?
干杯,
解决方案
参数maxiter
必须是整数。1e6
是一个浮点数。利用
maxiter=int(1e6)
或者
maxiter=1000000
推荐阅读
- ios - 尽管使用了工作区,但 Azure Pipeline '没有这样的模块'
- firefox-addon-webextensions - JSON.parse: end of data after property value in object at line 61 column 2 of the JSON data
- java - 集群中的 Tomcat TLS 会话恢复
- bash - 在我现有的关键字检测脚本中引入一个函数,该函数将在预定时间段后重新检查相同的条件
- splunk - 有没有办法在计数层中按字典顺序对 top 命令的输出进行排序
- r - Rstudio代码使用断点和ctrl-Enter乱序执行?
- excel - Dynamically add a formula with my Excel userform everytime I hit submit
- php - AWS Lightsail 上的错误“检测到捆绑包的循环依赖项”,在 XAMP 中工作正常
- assembly - 如何管理 JMP CALL POP 例程中的结构?
- javascript - 如何在时钟应用程序中用当前时间自动替换过去的秒/分钟/小时