MCPcopy Index your code
hub / github.com/cfgnunes/numerical-methods-python / five_point

Function five_point

differentiation.py:78–114  ·  view source on GitHub ↗

Calculate the first derivative. All values in 'x' must be equally spaced. Args: x (numpy.ndarray): x values. y (numpy.ndarray): y values. Returns: dy (numpy.ndarray): the first derivative values.

(x, y)

Source from the content-addressed store, hash-verified

76
77
78def five_point(x, y):
79 """Calculate the first derivative.
80
81 All values in 'x' must be equally spaced.
82
83 Args:
84 x (numpy.ndarray): x values.
85 y (numpy.ndarray): y values.
86
87 Returns:
88 dy (numpy.ndarray): the first derivative values.
89 """
90 if x.size < 6 or y.size < 6:
91 raise ValueError("'x' and 'y' arrays must have 6 values or more.")
92
93 if x.size != y.size:
94 raise ValueError("'x' and 'y' must have same size.")
95
96 def dy_mid(h, y0, y1, y3, y4):
97 return (1 / (12 * h)) * (y0 - 8 * y1 + 8 * y3 - y4)
98
99 def dy_end(h, y0, y1, y2, y3, y4):
100 return (1 / (12 * h)) * \
101 (-25 * y0 + 48 * y1 - 36 * y2 + 16 * y3 - 3 * y4)
102
103 hx = x[1] - x[0]
104 n = x.size
105 dy = np.zeros(n)
106 for i in range(0, n):
107 if i in (0, 1):
108 dy[i] = dy_end(hx, y[i], y[i + 1], y[i + 2], y[i + 3], y[i + 4])
109 elif i in (n - 1, n - 2):
110 dy[i] = dy_end(-hx, y[i], y[i - 1], y[i - 2], y[i - 3], y[i - 4])
111 else:
112 dy[i] = dy_mid(hx, y[i - 2], y[i - 1], y[i + 1], y[i + 2])
113
114 return dy

Callers

nothing calls this directly

Calls 2

dy_endFunction · 0.85
dy_midFunction · 0.85

Tested by

no test coverage detected