摆的数值解

2024-09-27 07:25:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我在提供我的系统的图形表示时遇到了问题,它恰好是一个和谐驱动的钟摆。下面显示的问题仅供参考。 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度的域


Tags: oftheforlentimenppiplt
1条回答
网友
1楼 · 发布于 2024-09-27 07:25:38

请更改数组计算

    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方法只承诺在系统闭合且保守的情况下具有一定的恒定能量,也就是说,在最常见的情况下,没有外部影响,所有力都是梯度力

相关问题 更多 >

    热门问题