Skip to content
This repository was archived by the owner on Jul 25, 2023. It is now read-only.

Commit fa05935

Browse files
committed
FEAT: skeleton code to test derivatives
1 parent b830ff5 commit fa05935

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

day_6/2023/derivatives_test.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#!/usr/bin/env python
2+
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
# ----------------------------------------------------------------------
7+
# Take first derivative of a function
8+
# ----------------------------------------------------------------------
9+
10+
def first_derivative(f, x):
11+
12+
""" Function that takes the first derivative
13+
14+
Parameters
15+
----------
16+
f - values of a function that is dependent on x -> f(x)
17+
x - the location of the point at which f(x) is evaluated
18+
19+
Returns
20+
-------
21+
dfdx - the first derivative of f(x)
22+
23+
Notes
24+
-----
25+
take the first derivative of f(x) here
26+
27+
"""
28+
29+
nPts = len(f)
30+
31+
dfdx = np.zeros(nPts)
32+
33+
# do calculation here - need 3 statements:
34+
# 1. left boundary ( dfdx(0) = ...)
35+
# 2. central region (using spans, like dfdx(1:nPts-2) = ...)
36+
# 3. right boundary ( dfdx(nPts-1) = ... )
37+
38+
return dfdx
39+
40+
# ----------------------------------------------------------------------
41+
# Take second derivative of a function
42+
# ----------------------------------------------------------------------
43+
44+
def second_derivative(f, x):
45+
46+
""" Function that takes the second derivative
47+
48+
Parameters
49+
----------
50+
f - values of a function that is dependent on x -> f(x)
51+
x - the location of the point at which f(x) is evaluated
52+
53+
Returns
54+
-------
55+
d2fdx2 - the second derivative of f(x)
56+
57+
Notes
58+
-----
59+
take the second derivative of f(x) here
60+
61+
"""
62+
63+
nPts = len(f)
64+
65+
d2fdx2 = np.zeros(nPts)
66+
67+
# do calculation here - need 3 statements:
68+
# 1. left boundary ( dfdx(0) = ...)
69+
# 2. central region (using spans, like dfdx(1:nPts-2) = ...)
70+
# 3. right boundary ( dfdx(nPts-1) = ... )
71+
72+
return d2fdx2
73+
74+
# ----------------------------------------------------------------------
75+
# Get the analytic solution to f(x), dfdx(x) and d2fdx2(x)
76+
# ----------------------------------------------------------------------
77+
78+
def analytic(x):
79+
80+
""" Function that gets analytic solutions
81+
82+
Parameters
83+
----------
84+
x - the location of the point at which f(x) is evaluated
85+
86+
Returns
87+
-------
88+
f - the function evaluated at x
89+
dfdx - the first derivative of f(x)
90+
d2fdx2 - the second derivative of f(x)
91+
92+
Notes
93+
-----
94+
These are analytic solutions!
95+
96+
"""
97+
98+
f = 4 * x ** 2 - 3 * x -7
99+
dfdx = 8 * x - 3
100+
d2fdx2 = np.zeros(len(f)) + 8.0
101+
102+
return f, dfdx, d2fdx2
103+
104+
105+
if __name__ == "__main__":
106+
107+
# get figures:
108+
fig = plt.figure(figsize = (10,10))
109+
ax1 = fig.add_subplot(311)
110+
ax2 = fig.add_subplot(312)
111+
ax3 = fig.add_subplot(313)
112+
113+
# define dx:
114+
dx = np.pi / 8
115+
116+
# arange doesn't include last point, so add explicitely:
117+
x = np.arange(-2.0 * np.pi, 2.0 * np.pi + dx, dx)
118+
119+
# get analytic solutions:
120+
f, a_dfdx, a_d2fdx2 = analytic(x)
121+
122+
# get numeric first derivative:
123+
n_dfdx = first_derivative(f, x)
124+
125+
# get numeric first derivative:
126+
n_d2fdx2 = second_derivative(f, x)
127+
128+
# plot:
129+
130+
ax1.plot(x, f)
131+
132+
# plot first derivatives:
133+
ax2.plot(x, a_dfdx, color = 'black', label = 'Analytic')
134+
ax2.plot(x, n_dfdx, color = 'red', label = 'Numeric')
135+
ax2.legend()
136+
137+
# plot second derivatives:
138+
139+
plotfile = 'plot.png'
140+
print('writing : ',plotfile)
141+
fig.savefig(plotfile)
142+
plt.close()
143+

0 commit comments

Comments
 (0)