重写,只需输出基波,三次谐波,五次谐波,七次谐波大小,并作出其大小对比柱状图
时间: 2024-04-24 07:26:24 浏览: 200
一个小的谐波分析程序
4星 · 用户满意度95%
好的,以下是重写后的程序,仅计算基波、三次谐波、五次谐波和七次谐波的大小,并绘制其大小对比的柱状图:
```python
import numpy as np
import matplotlib.pyplot as plt
# 定义公式中的常量参数
B_r = 300
sigma = 1
w_t = 2
w_s = 8
R_1 = 20
R_s = 25
p = 8
def B_g(theta):
# 定义公式中的函数
def K_delta(theta):
hp = np.sqrt((hm + R_1 - delta_h) ** 2 - (R_1 * np.sin(theta)) ** 2) + delta_h - R_1 * np.cos(theta)
lg = np.sqrt(R_s ** 2 - (R_1 * np.sin(theta)) ** 2) - hp - R_1 * np.cos(theta)
return (w_t / (w_t - delta_sm(lg, hp) * w_s)) * ((hp / lg) + 1) - (hp / lg)
def delta_sm(lg, hp):
return (2 / np.pi) * (np.arctan(w_s / (2 * (lg + hp))) - (lg / w_s) * np.log(1 + ((w_s / (2 * (lg + hp))) ** 2)))
def hp(theta):
return np.sqrt((hm + R_1 - delta_h) ** 2 - (R_1 * np.sin(theta)) ** 2) + delta_h - R_1 * np.cos(theta)
def lg(theta):
return np.sqrt(R_s ** 2 - (R_1 * np.sin(theta)) ** 2) - hp(theta) - R_1 * np.cos(theta)
# 计算B_g的值
return B_r / (sigma + K_delta(theta) * (lg(theta) / hp(theta)))
# 定义基波、三次谐波、五次谐波和七次谐波的theta值
theta_base = np.linspace(0, np.pi, 100)
theta_3rd = np.linspace(0, 3 * np.pi, 100)
theta_5th = np.linspace(0, 5 * np.pi, 100)
theta_7th = np.linspace(0, 7 * np.pi, 100)
# 计算基波、三次谐波、五次谐波和七次谐波的大小
B_base = [B_g(theta) for theta in theta_base]
B_3rd = [B_g(theta) for theta in theta_3rd]
B_5th = [B_g(theta) for theta in theta_5th]
B_7th = [B_g(theta) for theta in theta_7th]
# 绘制大小对比的柱状图
harmonics = ['Fundamental', '3rd Harmonic', '5th Harmonic', '7th Harmonic']
amplitudes = [np.max(B_base), np.max(B_3rd), np.max(B_5th), np.max(B_7th)]
plt.bar(harmonics, amplitudes)
plt.xlabel("Harmonic")
plt.ylabel("Amplitude")
plt.title("Amplitude comparison of harmonics")
plt.show()
```
该程序将计算基波、三次谐波、五次谐波和七次谐波的大小,并绘制它们的大小对比的柱状图。请注意,由于四个谐波的周期不同,可以通过调整theta的取值范围来确保它们在相应的周期内计算。
希望这个程序对您有所帮助!如果您有任何问题,请随时提问。
阅读全文