# import math import numpy as np import matplotlib pyplot as plt def sin

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53``` ```import math import numpy as np import matplotlib.pyplot as plt def sin_taylor(x): """Return the sine of x value using Taylor's series with true estimated error e = 0,000001. Keyword arguments: x -- angle value (measured in degrees)""" sign = 1 if x < 0: sign = -1 elif x == 0: return 0.0 x = math.fabs(x) while math.fabs(x) > 360: x -= 360 x = math.radians(x) sum_n = 0 sum_prev = 0 e = 0.000001 n = 0 while n < 2 or math.fabs((sum_n - sum_prev) / sum_prev) >= e: sum_prev = sum_n a_n = (-1) ** n * (x ** (2 * n + 1)) / math.factorial(2 * n + 1) sum_n = sum_prev + a_n n += 1 return sign * sum_n print("sin(65) using Taylor's series:", sin_taylor(65)) print("sin(65) using built-in math lib function:", math.sin(math.radians(65)), end='\n\n') print("sin(195) using Taylor's series:", sin_taylor(195)) print("sin(195) using built-in math lib function:", math.sin(math.radians(195))) angles_first = np.arange(-3 * np.pi, 4 * np.pi, 0.1) angles_second = np.arange(np.pi / 2, 4 * np.pi, 0.1) built_sin = np.sin(angles_first) taylor_sin = [sin_taylor(math.degrees(angle)) for angle in angles_second] rows, cols = plt.subplots() cols.plot(angles_first, built_sin) cols.plot(angles_second, taylor_sin) cols.set_ylim([-4, 4]) cols.legend(["sin(x) built-in function", "sin(x) Taylor's series"]) plt.show() ```