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

Function three_point

differentiation.py:40–75  ·  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

38
39
40def three_point(x, y):
41 """Calculate the first derivative.
42
43 All values in 'x' must be equally spaced.
44
45 Args:
46 x (numpy.ndarray): x values.
47 y (numpy.ndarray): y values.
48
49 Returns:
50 dy (numpy.ndarray): the first derivative values.
51 """
52 if x.size < 3 or y.size < 3:
53 raise ValueError("'x' and 'y' arrays must have 3 values or more.")
54
55 if x.size != y.size:
56 raise ValueError("'x' and 'y' must have same size.")
57
58 def dy_mid(h, y0, y2):
59 return (1 / (2 * h)) * (y2 - y0)
60
61 def dy_end(h, y0, y1, y2):
62 return (1 / (2 * h)) * (-3 * y0 + 4 * y1 - y2)
63
64 hx = x[1] - x[0]
65 n = x.size
66 dy = np.zeros(n)
67 for i in range(0, n):
68 if i == 0:
69 dy[i] = dy_end(hx, y[i], y[i + 1], y[i + 2])
70 elif i == n - 1:
71 dy[i] = dy_end(-hx, y[i], y[i - 1], y[i - 2])
72 else:
73 dy[i] = dy_mid(hx, y[i - 1], y[i + 1])
74
75 return dy
76
77
78def five_point(x, y):

Callers

nothing calls this directly

Calls 2

dy_endFunction · 0.85
dy_midFunction · 0.85

Tested by

no test coverage detected