gpt4 book ai didi

python - 类型错误 : unsupported operand type(s) for ^: 'numpy.float64' and 'numpy.float64'

转载 作者:太空狗 更新时间:2023-10-30 00:22:48 26 4
gpt4 key购买 nike

我刚开始用 Python 编程,对 Numpy 包还很陌生……我仍在努力掌握它。我正在尝试使用 euler 方法来解决一个函数。

这是我的代码:

Z=4
B=8
U=1
C=4

a,b=(0.0,10.0)
n=2000
x0=-1.0
t=linspace (a,b,n)
#-----------------------------------------------------------------------------
def euler (f,x0,t):
n=len (t)
x=np.array(n*[x0,])
for i in xrange (n-1):
float (x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] ))
return x



#---------------------------------------------------------------------------------
if __name__=="__main__":


def f(x,t):
return float((Z)*[-(1/6)*B*C*x^3+0.5*U*t^2])


#numerical solutions
x_euler=euler(f,x0,t)


#figure
plt.plot (t,x_euler, "b")
xlabel (t)
ylabel (x)
legend ("Euler")

show()

对于此类问题,我不接受类似的解决方案。这是我的回溯:

Traceback (most recent call last):
File "C:\Python27\testeuler.py", line 45, in <module>
x_euler=euler(f,x0,t)
File "C:\Python27\testeuler.py", line 31, in euler
float (x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] ))
File "C:\Python27\testeuler.py", line 41, in f
return float((Z)*[-(1/6)*B*C*x^3+0.5*U*t^2])
TypeError: unsupported operand type(s) for ^: 'numpy.float64' and 'numpy.float64'

任何人都知道什么可能是错误的或有任何建议?

最佳答案

脱字符运算符 (^) 不是幂运算。它是按位异或,只对整数有意义。你需要 ** 来代替。

关于python - 类型错误 : unsupported operand type(s) for ^: 'numpy.float64' and 'numpy.float64' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30084905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com