我在提供我的系统的图形表示时遇到了麻烦,这恰好是一个谐波驱动的钟摆。问题显示在下面以供参考。Problem
我使用的源代码如下所示,使用的是Verlet方案。
#Import needed modules
import numpy as np
import matplotlib.pyplot as plt
#Initialize variables (Initial conditions)
g = 9.8 #Gravitational Acceleration
L = 2.0 #Length of the Pendulum
A0 = 3.0 #Initial amplitude of the driving acceleration
v0 = 0.0 #Initial velocity
theta0 = 90*np.pi/180 #Initial Angle
drivingPeriod = 20.0 #Driving Period
#Setting time array for graph visualization
tau = 0.1 #Time Step
tStop = 10.0 #Maximum time for graph visualization derived from Kinematics
t = np.arange(0., tStop+tau, tau) #Array of time
theta = np.zeros(len(t))
v = np.zeros(len(t))
#Verlet Method
theta[0] = theta0
v[0] = v0
for i in range(len(t)-1):
    accel = -((g + (A0*np.sin((2*np.pi*t) / drivingPeriod)))/L) * np.sin(theta[i])
    theta[i+1] = theta[i] + tau*v[i] + 0.5*tau**2*accel[i]
    v[i+1] = v[i] + 0.5*tau*(accel[i] + accel[i+1])
#Plotting and saving the resulting graph
fig, ax1 = plt.subplots(figsize=(7.5,4.5))
ax1.plot(t,theta*(180/np.pi))
ax1.set_xlabel("Time (t)")
ax1.set_ylabel("Theta")
plt.show()示例输出如图所示。Output
钟摆应该回到它的初始角度。我该如何解决这个问题?请注意,随着时间的推移,我的角度度量(度数)也会增加。我希望它只有0度到360度的范围。
发布于 2021-03-21 16:32:55
请更改数组计算
    accel = -((g + (A0*np.sin((2*np.pi*t) / drivingPeriod)))/L) * np.sin(theta[i])
    theta[i+1] = theta[i] + tau*v[i] + 0.5*tau**2*accel[i]在正确的位置进行正确的元素计算
    theta[i+1] = theta[i] + tau*v[i] + 0.5*tau**2*accel[i]
    accel[i+1] = -((g + (A0*np.sin((2*np.pi*t[i+1]) / drivingPeriod)))/L) * np.sin(theta[i+1])请注意,您需要在循环之外单独计算accel[0]。
如果您分离出物理模型的细节并在开头声明,这将使代码更具可读性
def acceleration(t,theta):
    return -((g + (A0*np.sin((2*np.pi*t) / drivingPeriod)))/L) * np.sin(theta)所以稍后您只需调用
accel[i+1]=acceleration(t[i+1],theta[i+1])即使这样,在强迫振荡的情况下,你的系统是开放的,也有可能是强迫作用将能量注入到钟摆中,导致它开始旋转。这就是你的图表所显示的。
Verlet方法和任何辛方法一样,只有在系统是封闭和保守的情况下才有一定的恒定能量,也就是说,在最常见的情况下,没有外部影响,所有的力都是梯度力。
https://stackoverflow.com/questions/66729957
复制相似问题