用Python 写同一直线上简谐振动的合成同频率情况下
时间: 2024-02-06 20:10:27 浏览: 25
假设有两个简谐振动在同一直线上,振动频率分别为 $f_1$ 和 $f_2$,振幅分别为 $A_1$ 和 $A_2$,初相位分别为 $\phi_1$ 和 $\phi_2$。它们的位移分别为:
$$
x_1 = A_1 \cos(2\pi f_1 t + \phi_1) \\
x_2 = A_2 \cos(2\pi f_2 t + \phi_2)
$$
在同频率情况下,$f_1 = f_2 = f$,我们可以将这两个振动合成为一个振动:
$$
x = A \cos(2\pi f t + \phi)
$$
其中,$A$ 是合成振幅,$\phi$ 是合成初相位。根据叠加原理,$A$ 和 $\phi$ 可以表示为:
$$
A = \sqrt{A_1^2 + A_2^2 + 2A_1A_2\cos(\phi_1 - \phi_2)} \\
\phi = \arctan\left(\frac{A_1\sin\phi_1 + A_2\sin\phi_2}{A_1\cos\phi_1 + A_2\cos\phi_2}\right)
$$
下面是Python代码实现:
```python
import math
# 振动频率
f = 10
# 振幅和初相位
A1, A2 = 1, 2
phi1, phi2 = math.pi/4, math.pi/3
# 合成振幅和初相位
A = math.sqrt(A1**2 + A2**2 + 2*A1*A2*math.cos(phi1 - phi2))
phi = math.atan2(A1*math.sin(phi1) + A2*math.sin(phi2), A1*math.cos(phi1) + A2*math.cos(phi2))
# 时间范围
t_start, t_end = 0, 1
dt = 0.01
t = [t_start + i*dt for i in range(int((t_end - t_start)/dt) + 1)]
# 合成振动
x = [A*math.cos(2*math.pi*f*t[i] + phi) for i in range(len(t))]
# 绘图
import matplotlib.pyplot as plt
plt.plot(t, x)
plt.xlabel('Time (s)')
plt.ylabel('Displacement (m)')
plt.show()
```
这段代码会绘制出合成振动的位移随时间变化的图像。你可以通过修改代码中的振动频率、振幅和初相位来观察合成振动的变化。