I just started programming in Python and am very new to Numpy packages... I'm still trying to get a hang of it. I am trying to slove a function with the euler method.
我刚开始用Python编程,对Numpy包非常陌生……我还在试着弄懂它。我正在尝试用欧拉方法填充一个函数。
Here is my code:
这是我的代码:
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()
I do not get along with the similar solutions for such problems. Here is my Traceback:
对于这样的问题,我不赞同类似的解决办法。这是我的回溯:
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'
Anyone know what could be wrong or have any suggestions?
有谁知道哪里出了问题或者有什么建议吗?
1 个解决方案
#1
13
The caret operator (^) is not exponentiation. It is bitwise XOR, which only makes sense for integers. You want **
instead.
插入符号运算符(^)不是求幂。它是位XOR,只对整数有意义。你想要的* *。
#1
13
The caret operator (^) is not exponentiation. It is bitwise XOR, which only makes sense for integers. You want **
instead.
插入符号运算符(^)不是求幂。它是位XOR,只对整数有意义。你想要的* *。