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

Method step_3

maths/numerical_analysis/adams_bashforth.py:100–135  ·  view source on GitHub ↗

>>> def f(x, y): ... return x + y >>> y = AdamsBashforth(f, [0, 0.2, 0.4], [0, 0, 0.04], 0.2, 1).step_3() >>> float(y[3]) 0.15533333333333332 >>> AdamsBashforth(f, [0, 0.2], [0, 0], 0.2, 1).step_3() Traceback (most recent call last):

(self)

Source from the content-addressed store, hash-verified

98 return y
99
100 def step_3(self) -> np.ndarray:
101 """
102 >>> def f(x, y):
103 ... return x + y
104 >>> y = AdamsBashforth(f, [0, 0.2, 0.4], [0, 0, 0.04], 0.2, 1).step_3()
105 >>> float(y[3])
106 0.15533333333333332
107
108 >>> AdamsBashforth(f, [0, 0.2], [0, 0], 0.2, 1).step_3()
109 Traceback (most recent call last):
110 ...
111 ValueError: Insufficient initial points information.
112 """
113 if len(self.x_initials) != 3 or len(self.y_initials) != 3:
114 raise ValueError("Insufficient initial points information.")
115
116 x_0, x_1, x_2 = self.x_initials[:3]
117 y_0, y_1, y_2 = self.y_initials[:3]
118
119 n = int((self.x_final - x_2) / self.step_size)
120 y = np.zeros(n + 4)
121 y[0] = y_0
122 y[1] = y_1
123 y[2] = y_2
124
125 for i in range(n + 1):
126 y[i + 3] = y[i + 2] + (self.step_size / 12) * (
127 23 * self.func(x_2, y[i + 2])
128 - 16 * self.func(x_1, y[i + 1])
129 + 5 * self.func(x_0, y[i])
130 )
131 x_0 = x_1
132 x_1 = x_2
133 x_2 += self.step_size
134
135 return y
136
137 def step_4(self) -> np.ndarray:
138 """

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected