Week 10: Exercise Solutions

Exercise 10.1

import math
import matplotlib.pyplot as plt

x_min = 0
x_max = 2*math.pi
NO_OF_INTERVALS = 100

x_values, y_values, y_values_2 = [], [], []

for i in range(NO_OF_INTERVALS + 1):
    x = x_min + i * (x_max - x_min) / NO_OF_INTERVALS
    x_values.append(x)
    y_values.append(math.sin(x))
    y_values_2.append(math.cos(x))

plt.plot(x_values, y_values)
plt.plot(x_values, y_values_2, color="red")

plt.xlabel("x")
plt.ylabel("y")
plt.title("A plot of sin(x) and cos(x)")
plt.show()

Exercise 10.2

Add the following line before the plt.show() command:

plt.plot(x_values, np.cos(x_values), color="red")