Python 尝试求解ODE系统时出错
这是我的密码:Python 尝试求解ODE系统时出错,python,ode,Python,Ode,这是我的密码: import numpy as np from scipy.integrate import odeint import matplotlib.pyplot as plt import math def VectorField(w, t, p): """ Defines the differential equations to find a in terms of eta. Arguments: w :
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import math
def VectorField(w, t, p):
"""
Defines the differential equations to find a in terms of eta.
Arguments:
w : vector of the state variables:
w = [eta, a]
t : time
p : vector of the parameters:
p = [H_0, m, r, d]
"""
eta, a = w
H_0, m, r, d = p
# Create f = (eta', a'):
f = [a, H_0 * (m*a^(-1) + r*a^(-2) + d* a^2)^(1/2)]
return f
# Parameter values
H_0 = 0.000000000000000002184057032
m = 0.315
r = 0.0000926
d = 0.685
# Initial Conditions
a0 = eta_0 = 0
# ODE solver parameters
abserr = 1.0e-8
relerr = 1.0e-6
stoptime = 10.0
numpoints = 250
#x axis values
t = [stoptime * float(i) / (numpoints - 1) for i in range(numpoints)]
# Pack up the parameters and initial conditions:
initials = [eta_0, a0]
p = [H_0, m, r, d]
# Call the ODE solver.
wsol = odeint(VectorField, initials, t, args=(p,), atol=abserr, rtol=relerr)
但是,当我运行代码时,我可以看到错误消息
TypeError:输入类型不支持ufunc“按位异或”,并且无法根据强制转换规则“安全”将输入安全强制为任何支持的类型
代码指向我的向量f。我不确定我在声明这个向量f时做错了什么。谁能告诉我我做错了什么?我怎样才能消除这个错误?谢谢当我在两种语言之间来回走动时,这个问题有时会让我感到困惑 这一行,例如:
f=[a,H_0*(m*a^(-1)+r*a^(-2)+d*a^2^(1/2)]
应该是
f=[a,H_0*(m*a**(-1)+r*a**(-2)+d*a**2)**(1/2)]
因为
**
是幂运算符,而不是^
您是否使用^
作为幂运算符?请注意,python中的幂运算符不是表示为a^b
,而是表示为a**b
。在python中,将某项操作提升到幂是通过**
实现的,而不是通过^
。^
字符用于@nobbynobsThanks@LutzLehmann谢谢