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

Method step_5

maths/numerical_analysis/adams_bashforth.py:181–225  ·  view source on GitHub ↗

>>> def f(x,y): ... return x + y >>> y = AdamsBashforth( ... f, [0, 0.2, 0.4, 0.6, 0.8], [0, 0.02140, 0.02140, 0.22211, 0.42536], ... 0.2, 1).step_5() >>> float(y[-1]) 0.05436839444444452 >>> AdamsBashforth(f, [0, 0.2, 0.4

(self)

Source from the content-addressed store, hash-verified

179 return y
180
181 def step_5(self) -> np.ndarray:
182 """
183 >>> def f(x,y):
184 ... return x + y
185 >>> y = AdamsBashforth(
186 ... f, [0, 0.2, 0.4, 0.6, 0.8], [0, 0.02140, 0.02140, 0.22211, 0.42536],
187 ... 0.2, 1).step_5()
188 >>> float(y[-1])
189 0.05436839444444452
190
191 >>> AdamsBashforth(f, [0, 0.2, 0.4], [0, 0, 0.04], 0.2, 1).step_5()
192 Traceback (most recent call last):
193 ...
194 ValueError: Insufficient initial points information.
195 """
196
197 if len(self.x_initials) != 5 or len(self.y_initials) != 5:
198 raise ValueError("Insufficient initial points information.")
199
200 x_0, x_1, x_2, x_3, x_4 = self.x_initials[:5]
201 y_0, y_1, y_2, y_3, y_4 = self.y_initials[:5]
202
203 n = int((self.x_final - x_4) / self.step_size)
204 y = np.zeros(n + 6)
205 y[0] = y_0
206 y[1] = y_1
207 y[2] = y_2
208 y[3] = y_3
209 y[4] = y_4
210
211 for i in range(n + 1):
212 y[i + 5] = y[i + 4] + (self.step_size / 720) * (
213 1901 * self.func(x_4, y[i + 4])
214 - 2774 * self.func(x_3, y[i + 3])
215 - 2616 * self.func(x_2, y[i + 2])
216 - 1274 * self.func(x_1, y[i + 1])
217 + 251 * self.func(x_0, y[i])
218 )
219 x_0 = x_1
220 x_1 = x_2
221 x_2 = x_3
222 x_3 = x_4
223 x_4 += self.step_size
224
225 return y
226
227
228if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected