-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMirankerLinigerP.py
More file actions
49 lines (40 loc) · 1.19 KB
/
MirankerLinigerP.py
File metadata and controls
49 lines (40 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import numpy as np
def mirankerLinigerP(f, tspan, y0, N):
"""
Solves ODEs using a parallel predictor-corrector method.
This function solves the differential equation dy/dt = f(t, y) with
an initial value y0 over the time interval specified by tspan,
using N steps of a parallel predictor-corrector method.
Parameters
----------
f : callable
Right-hand side of the ODE, f(t, y).
tspan : tuple
Time interval (start, end).
y0 : array
Initial value.
N : int
Number of time steps.
Returns
-------
t : array
Time points.
y : array
Solution values at time points.
"""
# Time step
dt = (tspan[1] - tspan[0]) / N
t = np.linspace(tspan[0], tspan[1], N+1)
# Initialize solution array
y = np.zeros((len(y0), N+1))
y[:, 0] = y0
# Start with an Euler step
y[:, 1] = y[:, 0] + dt * f(t[0], y[:, 0])
yp = y[:, 1]
# Parallel predictor-corrector method
for n in range(2, N):
# Parallel predictor step
y[:, n] = y[:, n-1] + dt / 2 * (f(t[n], yp) + f(t[n-1], y[:, n-1]))
# Corrector step
yp = y[:, n-1] + 2 * dt * f(t[n], yp)
return t, y