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

Method step_4

maths/numerical_analysis/adams_bashforth.py:137–179  ·  view source on GitHub ↗

>>> def f(x,y): ... return x + y >>> y = AdamsBashforth( ... f, [0, 0.2, 0.4, 0.6], [0, 0, 0.04, 0.128], 0.2, 1).step_4() >>> float(y[4]) 0.30699999999999994 >>> float(y[5]) 0.5771083333333333 >>> AdamsBashforth(f, [0,

(self)

Source from the content-addressed store, hash-verified

135 return y
136
137 def step_4(self) -> np.ndarray:
138 """
139 >>> def f(x,y):
140 ... return x + y
141 >>> y = AdamsBashforth(
142 ... f, [0, 0.2, 0.4, 0.6], [0, 0, 0.04, 0.128], 0.2, 1).step_4()
143 >>> float(y[4])
144 0.30699999999999994
145 >>> float(y[5])
146 0.5771083333333333
147
148 >>> AdamsBashforth(f, [0, 0.2, 0.4], [0, 0, 0.04], 0.2, 1).step_4()
149 Traceback (most recent call last):
150 ...
151 ValueError: Insufficient initial points information.
152 """
153
154 if len(self.x_initials) != 4 or len(self.y_initials) != 4:
155 raise ValueError("Insufficient initial points information.")
156
157 x_0, x_1, x_2, x_3 = self.x_initials[:4]
158 y_0, y_1, y_2, y_3 = self.y_initials[:4]
159
160 n = int((self.x_final - x_3) / self.step_size)
161 y = np.zeros(n + 4)
162 y[0] = y_0
163 y[1] = y_1
164 y[2] = y_2
165 y[3] = y_3
166
167 for i in range(n):
168 y[i + 4] = y[i + 3] + (self.step_size / 24) * (
169 55 * self.func(x_3, y[i + 3])
170 - 59 * self.func(x_2, y[i + 2])
171 + 37 * self.func(x_1, y[i + 1])
172 - 9 * self.func(x_0, y[i])
173 )
174 x_0 = x_1
175 x_1 = x_2
176 x_2 = x_3
177 x_3 += self.step_size
178
179 return y
180
181 def step_5(self) -> np.ndarray:
182 """

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected