首页

喜欢蓝天、白云和你

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()

Results

《Python Course homework 4》

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*
*