Skip to content

Commit 548777c

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 418693c commit 548777c

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

maths/numerical_analysis/adams_bashforth.py

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@
99

1010
import numpy as np
1111

12-
class AdamsBashforth:
13-
def __init__(self, func: Callable[[float, float], float], x_initials: list[float], y_initials: list[float], step_size: float, x_final: float):
1412

13+
class AdamsBashforth:
14+
def __init__(
15+
self,
16+
func: Callable[[float, float], float],
17+
x_initials: list[float],
18+
y_initials: list[float],
19+
step_size: float,
20+
x_final: float,
21+
):
1522
if x_initials[-1] >= x_final:
16-
raise ValueError("The final value of x must be greater than the initial values of x.")
23+
raise ValueError(
24+
"The final value of x must be greater than the initial values of x."
25+
)
1726

1827
if step_size <= 0:
1928
raise ValueError("Step size must be positive.")
@@ -69,7 +78,7 @@ def __init__(self, func: Callable[[float, float], float], x_initials: list[float
6978
Traceback (most recent call last):
7079
...
7180
ValueError: Step size must be positive.
72-
81+
7382
>>> def f(x, y):
7483
... return (x -y)/2
7584
>>> y = AdamsBashforth(f, [0, 0.2, 0.4], [0, 0, 0.04], 0.2, 1).step_2()
@@ -91,7 +100,9 @@ def step_2(self) -> np.ndarray:
91100
y[1] = y_1
92101

93102
for i in range(n):
94-
y[i+2] = y[i+1] + (self.step_size/2)*(3*self.func(x_1, y[i+1]) - self.func(x_0, y[i]))
103+
y[i + 2] = y[i + 1] + (self.step_size / 2) * (
104+
3 * self.func(x_1, y[i + 1]) - self.func(x_0, y[i])
105+
)
95106
x_0 = x_1
96107
x_1 = x_1 + self.step_size
97108

@@ -110,8 +121,12 @@ def step_3(self) -> np.ndarray:
110121
y[1] = y_1
111122
y[2] = y_2
112123

113-
for i in range(n+1):
114-
y[i+3] = y[i+2] + (self.step_size/12)*(23*self.func(x_2 , y[i+2]) -16*self.func(x_1, y[i+1]) + 5*self.func(x_0, y[i]))
124+
for i in range(n + 1):
125+
y[i + 3] = y[i + 2] + (self.step_size / 12) * (
126+
23 * self.func(x_2, y[i + 2])
127+
- 16 * self.func(x_1, y[i + 1])
128+
+ 5 * self.func(x_0, y[i])
129+
)
115130
x_0 = x_1
116131
x_1 = x_2
117132
x_2 = x_2 + self.step_size
@@ -133,7 +148,12 @@ def step_4(self) -> np.ndarray:
133148
y[3] = y_3
134149

135150
for i in range(n):
136-
y[i+4] = y[i+3] + (self.step_size/24) * (55 * self.func(x_3, y[i+3]) - 59 * self.func(x_2 , y[i+2]) + 37*self.func(x_1, y[i+1]) - 9*self.func(x_0, y[i]))
151+
y[i + 4] = y[i + 3] + (self.step_size / 24) * (
152+
55 * self.func(x_3, y[i + 3])
153+
- 59 * self.func(x_2, y[i + 2])
154+
+ 37 * self.func(x_1, y[i + 1])
155+
- 9 * self.func(x_0, y[i])
156+
)
137157
x_0 = x_1
138158
x_1 = x_2
139159
x_2 = x_3
@@ -156,8 +176,14 @@ def step_5(self) -> np.ndarray:
156176
y[3] = y_3
157177
y[4] = y_4
158178

159-
for i in range(n+1):
160-
y[i+5] = y[i+4] + (self.step_size/720) * (1901 * self.func(x_4, y[i+4]) - 2774 * self.func(x_3, y[i+3]) - 2616 * self.func(x_2 , y[i+2]) - 1274 *self.func(x_1, y[i+1]) + 251*self.func(x_0, y[i]))
179+
for i in range(n + 1):
180+
y[i + 5] = y[i + 4] + (self.step_size / 720) * (
181+
1901 * self.func(x_4, y[i + 4])
182+
- 2774 * self.func(x_3, y[i + 3])
183+
- 2616 * self.func(x_2, y[i + 2])
184+
- 1274 * self.func(x_1, y[i + 1])
185+
+ 251 * self.func(x_0, y[i])
186+
)
161187
x_0 = x_1
162188
x_1 = x_2
163189
x_2 = x_3
@@ -166,7 +192,8 @@ def step_5(self) -> np.ndarray:
166192

167193
return y
168194

195+
169196
if __name__ == "__main__":
170197
import doctest
171198

172-
doctest.testmod()
199+
doctest.testmod()

0 commit comments

Comments
 (0)