MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / step_2

Method step_2

maths/numerical_analysis/adams_bashforth.py:67–98  ·  view source on GitHub ↗

>>> def f(x, y): ... return x >>> AdamsBashforth(f, [0, 0.2], [0, 0], 0.2, 1).step_2() array([0. , 0. , 0.06, 0.16, 0.3 , 0.48]) >>> AdamsBashforth(f, [0, 0.2, 0.4], [0, 0, 0.04], 0.2, 1).step_2() Traceback (most recent call last):

(self)

Source from the content-addressed store, hash-verified

65 raise ValueError("x-values must be equally spaced according to step size.")
66
67 def step_2(self) -> np.ndarray:
68 """
69 >>> def f(x, y):
70 ... return x
71 >>> AdamsBashforth(f, [0, 0.2], [0, 0], 0.2, 1).step_2()
72 array([0. , 0. , 0.06, 0.16, 0.3 , 0.48])
73
74 >>> AdamsBashforth(f, [0, 0.2, 0.4], [0, 0, 0.04], 0.2, 1).step_2()
75 Traceback (most recent call last):
76 ...
77 ValueError: Insufficient initial points information.
78 """
79
80 if len(self.x_initials) != 2 or len(self.y_initials) != 2:
81 raise ValueError("Insufficient initial points information.")
82
83 x_0, x_1 = self.x_initials[:2]
84 y_0, y_1 = self.y_initials[:2]
85
86 n = int((self.x_final - x_1) / self.step_size)
87 y = np.zeros(n + 2)
88 y[0] = y_0
89 y[1] = y_1
90
91 for i in range(n):
92 y[i + 2] = y[i + 1] + (self.step_size / 2) * (
93 3 * self.func(x_1, y[i + 1]) - self.func(x_0, y[i])
94 )
95 x_0 = x_1
96 x_1 += self.step_size
97
98 return y
99
100 def step_3(self) -> np.ndarray:
101 """

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected