-
Notifications
You must be signed in to change notification settings - Fork 19
/
Derivatives_of_Trigonometric_Functions.py
84 lines (67 loc) · 2.24 KB
/
Derivatives_of_Trigonometric_Functions.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sympy as sp
from matplotlib.animation import FuncAnimation
# Define the angles in degrees
angles_degrees = np.arange(0, 361, 1)
# Convert degrees to radians
angles_radians = np.deg2rad(angles_degrees)
# Calculate sine and cosine values
sin_values = np.sin(angles_radians)
cos_values = np.cos(angles_radians)
# Create a DataFrame
data = {
'Degrees': angles_degrees,
'Radians': angles_radians,
'Sine': sin_values,
'Cosine': cos_values,
}
df = pd.DataFrame(data)
# Create a plot with derivatives
x = sp.symbols('x')
sine_expr = sp.sin(x)
cosine_expr = sp.cos(x)
sine_derivative = sp.diff(sine_expr, x)
cosine_derivative = sp.diff(cosine_expr, x)
# Calculate derivatives
sine_derivative_values = [sp.N(sine_derivative.subs(x, sp.rad(deg))) for deg in angles_degrees]
cosine_derivative_values = [sp.N(cosine_derivative.subs(x, sp.rad(deg))) for deg in angles_degrees]
# Create a new DataFrame
data['Sine Derivative'] = sine_derivative_values
data['Cosine Derivative'] = cosine_derivative_values
df = pd.DataFrame(data)
# Create a plot with derivatives
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 6))
ax1.plot(df['Degrees'], df['Sine'], label='Sine')
ax1.plot(df['Degrees'], df['Cosine'], label='Cosine')
ax1.set_xlabel('Angle (Degrees)')
ax1.set_ylabel('Value')
ax1.set_title('Trigonometric Functions')
ax1.legend()
ax2.plot(df['Degrees'], df['Sine Derivative'], label='Sine Derivative')
ax2.plot(df['Degrees'], df['Cosine Derivative'], label='Cosine Derivative')
ax2.set_xlabel('Angle (Degrees)')
ax2.set_ylabel('Value')
ax2.set_title('Derivatives of Trigonometric Functions')
ax2.legend()
# Animation of sine and cosine waves
fig, ax = plt.subplots()
x_data = []
y_data = []
ln, = plt.plot([], [], 'r-', animated=True, label='Sine')
def init():
ax.set_xlim(0, 360)
ax.set_ylim(-1, 1)
ax.set_xlabel('Angle (Degrees)')
ax.set_ylabel('Value')
ax.set_title('Sine Wave Animation')
ax.legend(loc='upper right')
return ln,
def update(frame):
x_data.append(frame)
y_data.append(np.sin(np.deg2rad(frame)))
ln.set_data(x_data, y_data)
return ln,
ani = FuncAnimation(fig, update, frames=np.arange(0, 360, 1), init_func=init, blit=True)
plt.show()