Python Course homework 4

Exercise 1

Plot the function

f(x) = \cos(\pi \theta x) \exp(-x)

over the interval [0, 5] for each \theta in np.linspace(0, 2, 10)

Place all the curves in the same figure

Answer

import matplotlib.pyplot as plt
import numpy as np
import math

fig, ax = plt.subplots()
theta = np.linspace(0, 2, 10)
for i in range(len(theta)):
    x = np.linspace(0,5,50)
    y = np.cos(math.pi*theta[i]*x)*np.exp(-x)
    current_label = f'$theta = {theta[i]:.2}$'
    ax.plot(x, y, linewidth=2, alpha=0.6, label=current_label)
ax.legend()
plt.show()
Matplotlib 绘图结果

发表评论

关闭菜单