Spyder无法运行代码 - Python
我刚开始学习编程,正在用Python来模拟一些物理系统,使用的是OSX 10.9.2上的Spyder。我觉得这不是我代码的问题,因为它第一次运行得很好,但之后每次我点击运行,命令行(我想这叫Python解释器?)就只显示runfile('/Users/Paddy/....文件名'),然后我就无法再运行代码了。连其他简单的小程序也不能运行。命令行里的'>>>'也消失了。
我在网上找了解决办法,但说实话,我不太确定自己在找什么,也不知道这是什么类型的错误,是Spyder的bug还是其他原因。我的代码需要某种“结束”吗?
我把我正在工作的完整代码也附上了,以防里面有错误。就像我说的,我对这些完全不熟悉,无法判断这是Spyder的问题还是我代码的问题。任何帮助都非常感谢,我的截止日期快到了!谢谢
# Velocity Verlet integrator
def Verlet(x, V, dt, A):
x_new = x + V*dt + (A(x,V,R)*dt**2)/2
V_new = V + (A(x,V,R) + (2/(2-dt))*((((48/x_new**13)-(24/x_new**7)) - V + (0.5)*A(x,V,R)*dt + 2**(0.5) * R)) )/2 * dt
return (x_new, V_new)
# Start main program
# Import required libraries
import numpy as np
from numpy import array, zeros
import random
mu, sigma = 0, 0.1 # mean and variance
S = np.random.normal(mu, sigma, 1000) # Random numbers generated from gaussian
# Then the function itself
def A(x,V,R):
Acc = (((48/x**13)-(24/x**7)) - V + 2**(0.5) * R)
return Acc
# Set starting values for position and velocity
x = array([5])
V = array([0])
N = 1000 # integration time steps
M = 10 # save position every M timestep
dt = 1.0 / (N) # calculate timestep length in seconds
# Lists for storing the position and velocity
Xlist = zeros([1,N/M]) #define vector dimensions
Vlist = zeros([1,N/M])
# Put the initial values into the lists
Xlist[:,0] = x
Vlist[:,0] = V
# Run simulation
print "Total number of steps:", N
print "Saving location every %d steps." % (M)
print "Start."
for i in range(N/M):
# Run for M steps before saving values
for j in range(M):
# Update position and velocity based on the current ones
# and the acceleration function
R = random.choice(S) # selects random number from S
x, V = Verlet(x, V, dt, A)
# Save values into lists
Xlist[:, i] = x
Vlist[:, i] = V
print ("Stop.")
print (Xlist)
print (Vlist)
L = zeros([1,N/M])
k=0
while k < 101:
l = k+1
L[:,l]
print (L)
# Plot results
from matplotlib import pyplot as plt
#plt.plot(L, Xlist)
# Set equal axis
plt.axis('equal')
# Draw x and y axis lines
plt.axhline(color="black")
plt.axvline(color="black")
#plt.show()
1 个回答
1
你的 while k < 101 循环里出现了无限循环,因为你从来没有让 k 增加。你可以试试下面的例子:
k=0
while k < 100:
L[:,k]
k += 1
另外要注意,Python 是从 0 开始计数的。所以如果你想要一个长度为 100 的数组,k 应该从 0 增加到 99,而不是从 1 增加到 100。