This repository was archived by the owner on Dec 25, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathRK.py
More file actions
137 lines (127 loc) · 4.76 KB
/
Copy pathRK.py
File metadata and controls
137 lines (127 loc) · 4.76 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
Méthodes de RUNGE-KUTTA d'ordre 1 et 2
"""
import numpy as np
def RK(step_fun, fun, t_span, y0, t_eval = None) :
"""
Applique une méthode itérative pour évaluer la solution de l'équation différentielle :
| y = `fun`(t, y)
| y(`t_span[0]`) = `y0`
Parameters
----------
step_fun : fonction
Fonction définissant le calcul d'une itération. Elle doit avoir la signature
step_fun(fun, t, y, t_next, args) avec :
* fun : second membre de l'équation différentielle
* t : l'instant courant
* y : l'estimation de la solution à l'instant `t`
* t_next : l'instant suivant
fun : fonction
Second membre de l'équation différentielle. Elle doit avoir la signature
fun(t, y, args) avec :
* t : l'instant courant
* y : l'estimation de la solution à l'instant `t`
t_span : couple de flottants
Intervalle de résolution [t_0, t_f]
La résolution démarre à l'instant t_0 et termine à l'instant t_f.
y0 : ndarray NumPy, dimension (n, )
Condition initiale
t_eval : ndarray NumPy, dimension (n, n_points) ou `None`, optionnel
Subdivision utilisée pour la résolution
Les valeurs doivent être triées, commencer par `t_span[0]` et terminer par `t_span[-1]`.
Si `t_eval` vaut `None`, alors la subdivision est simplement `t_span`.
Returns
-------
ndarray NumPy, dimension (n, n_points)
Estimation des valeurs de la solution aux instants de `t_eval` ou `t_span` si `t_eval` vaut `None`
"""
(t0, tf) = t_span
if t_eval is None :
t, y = np.array(t_span), np.zeros((y0.size, 2))
y[:, 0] = y0
y[:, 1] = step_fun(fun, t0, y0, tf)
else :
n = y0.size
n_points = t_eval.size
t = t_eval
y = np.zeros((n, n_points))
y[:, 0] = y0
for i in range(1, n_points) :
y[:, i] = step_fun(fun, t[i - 1], y[:, i - 1], t[i])
return y
def RK1_step(fun, t, y, t_next) :
return y + (t_next - t) * fun(t, y)
def RK1(fun, t_span, y0, t_eval = None) :
"""
Applique une méthode de RUNGE-KUTTA d'ordre 1 (a.k.a. méthode d'EULER)
| y = `fun`(t, y)
| y(`t_span[0]`) = `y0`
Parameters
----------
step_fun : fonction
Fonction définissant le calcul d'une itération. Elle doit avoir la signature
step_fun(fun, t, y, t_next, args) avec :
* fun : second membre de l'équation différentielle
* t : l'instant courant
* y : l'estimation de la solution à l'instant `t`
* t_next : l'instant suivant
fun : fonction
Second membre de l'équation différentielle. Elle doit avoir la signature
fun(t, y, args) avec :
* t : l'instant courant
* y : l'estimation de la solution à l'instant `t`
t_span : couple de flottants
Intervalle de résolution [t_0, t_f]
La résolution démarre à l'instant t_0 et termine à l'instant t_f.
y0 : ndarray NumPy, dimension (n, )
Condition initiale
t_eval : ndarray NumPy, dimension (n, n_points) ou `None`, optionnel
Subdivision utilisée pour la résolution
Les valeurs doivent être triées, commencer par `t_span[0]` et terminer par `t_span[-1]`.
Si `t_eval` vaut `None`, alors la subdivision est simplement `t_span`.
Returns
-------
ndarray NumPy, dimension (n, n_points)
Estimation des valeurs de la solution aux instants de `t_eval` ou `t_span` si `t_eval` vaut `None`
"""
return RK(RK1_step, fun, t_span, y0, t_eval)
def RK2_step(fun, t, y, t_next) :
h = t_next - t
t_mid = 0.5 * (t + t_next)
y_mid = y + 0.5 * h * fun(t, y)
return y + h * fun(t_mid, y_mid)
def RK2(fun, t_span, y0, t_eval = None) :
"""
Applique une méthode de RUNGE-KUTTA d'ordre 2
pour évaluer la solution de l'équation différentielle :
| y = `fun`(t, y)
| y(`t_span[0]`) = `y0`
Parameters
----------
step_fun : fonction
Fonction définissant le calcul d'une itération. Elle doit avoir la signature
step_fun(fun, t, y, t_next, args) avec :
* fun : second membre de l'équation différentielle
* t : l'instant courant
* y : l'estimation de la solution à l'instant `t`
* t_next : l'instant suivant
fun : fonction
Second membre de l'équation différentielle. Elle doit avoir la signature
fun(t, y, args) avec :
* t : l'instant courant
* y : l'estimation de la solution à l'instant `t`
t_span : couple de flottants
Intervalle de résolution [t_0, t_f]
La résolution démarre à l'instant t_0 et termine à l'instant t_f.
y0 : ndarray NumPy, dimension (n, )
Condition initiale
t_eval : ndarray NumPy, dimension (n, n_points) ou `None`, optionnel
Subdivision utilisée pour la résolution
Les valeurs doivent être triées, commencer par `t_span[0]` et terminer par `t_span[-1]`.
Si `t_eval` vaut `None`, alors la subdivision est simplement `t_span`.
Returns
-------
ndarray NumPy, dimension (n, n_points)
Estimation des valeurs de la solution aux instants de `t_eval` ou `t_span` si `t_eval` vaut `None`
"""
return RK(RK2_step, fun, t_span, y0, t_eval)