Cours Python - Programmation Scientifique-Numpy-Matplot
Cours Python - Programmation Scientifique-Numpy-Matplot
Les tableaux dans numpy ressemblent à des listes: objet mutable qui permet de lister différents
éléments, mais ils doivent tous être du même type (entiers, décimaux, chaînes de caractères,
listes, tuples…). Si le contenu du tableau peut être modifié, la longueur du tableau n’est pas
modifiable.
ATTENTION : comme pour une liste, l’indice du premier élément d’un tableau numpy a pour valeur 0 !
T1 = np.arange(10) [0 1 2 3 4 5 6 7 8 9]
T2 = np.arange(0.1,3,0.2) [0.1 0.3 0.5 0.7 0.9 1.1 1.3 1.5 1.7 1.9 2.1 2.3 2.5 2.7 2.9]
py
um
N [0. 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 0.2 0.22 0.24 0.26
0.28 0.3 0.32 0.34 0.36 0.38 0.4 0.42 0.44 0.46 0.48 0.5 0.52 0.54
0.56 0.58 0.6 0.62 0.64 0.66 0.68 0.7 0.72 0.74 0.76 0.78 0.8 0.82
T3 = np.linspace(0,2,101) 0.84 0.86 0.88 0.9 0.92 0.94 0.96 0.98 1. 1.02 1.04 1.06 1.08 1.1
1.12 1.14 1.16 1.18 1.2 1.22 1.24 1.26 1.28 1.3 1.32 1.34 1.36 1.38
1.4 1.42 1.44 1.46 1.48 1.5 1.52 1.54 1.56 1.58 1.6 1.62 1.64 1.66
1.68 1.7 1.72 1.74 1.76 1.78 1.8 1.82 1.84 1.86 1.88 1.9 1.92 1.94
1.96 1.98 2. ]
[[1. 1. 1.]
T5=np.ones((2,3))
[1. 1. 1.]]
py
um
N
a=np.array([1.5, 3.7])
a[0]=5
b=np.array([[0,1],[2,3]])
print(a[0])
print(b[1,1])
py
um
N
a=np.array([1.5, 3.7])
a[0]=5
b=np.array([[0,1],[2,3]])
print(a[0]) 5.0
print(b[1,1]) 3
py
um Mathematical operations between arrays
N
import numpy as np
T1=np.array([1,2,3,4])
T2=np.array([16,4,2,1])
print(T1+T2)
print(T1-T2)
print(T1*T2)
print(T1/T2)
print(T1**2)
print(np.sqrt(T2))
py
um Mathematical operations between arrays
N
import numpy as np
T1=np.array([1,2,3,4])
T2=np.array([16,4,2,1])
print(T1+T2) [17 6 5 5]
print(T1-T2) [-15 -2 1 3]
print(T1*T2) [16 8 6 4]
print(T1/T2) [0.0625 0.5 1.5 4. ]
print(T1**2) [1 4 9 16]
print(np.sqrt(T2)) [4. 2. 1.41421356 1. ]
py
um
N import numpy as np
T1=np.array([[1,2],[3,4]])
T2=np.array([[16,4],[2,1]])
print(np.array(T1@T2))
py
um
N import numpy as np
T1=np.array([[1,2],[3,4]]) [[20 6]
T2=np.array([[16,4],[2,1]])
[56 16]]
print(np.array(T1@T2))
Modéliser la fonction sinus sur l’intervalle 0, 2π.
import numpy as np
import matplotlib.pyplot as plt
# Définition de l'intervalle
xmin = 0
xmax = 2 * np.pi
nb_points = 1000
VA =q*l/2
VA+VB -q*l=0
2 VB =q*l/2
VB *l-q*l /2=0
2
y
2
M =VA* x - q * x / 2 = q * (l * x - x ) / 2
p
um
N V = VA - q * x=q * (l / 2 - x)
import numpy as np
l=5 #m
[0. 0.26315789 0.52631579 0.78947368 1.05263158 1.31578947
q=20 #kN/m 1.57894737 1.84210526 2.10526316 2.36842105 2.63157895 2.89473684
3.15789474 3.42105263 3.68421053 3.94736842 4.21052632 4.47368421
x=np.linspace(0,l,20) 4.73684211 5. ]
M=q/2*(l*x-x**2)
V=q*(l/2-x) [ 0. 12.46537396 23.54570637 33.24099723 41.55124654 48.47645429
54.0166205 58.17174515 60.94182825 62.32686981 62.32686981 60.94182825
58.17174515 54.0166205 48.47645429 41.55124654 33.24099723 23.54570637
print(x) 12.46537396 0. ]
print(M)
print(V) [ 50. 44.73684211 39.47368421 34.21052632 28.94736842
23.68421053 18.42105263 13.15789474 7.89473684 2.63157895
-2.63157895 -7.89473684 -13.15789474 -18.42105263 -23.68421053
-28.94736842 -34.21052632 -39.47368421 -44.73684211 -50. ]
MATPLOT
import numpy as np
from matplotlib import pyplot as plt
l=5 #m
q=20 #kN/m
x=np.linspace(0,l,20)
M=q/2*(l*x-x**2)
V=q*(l/2-x)
print(x)
print(M)
print(V)
plt.figure(figsize=(10,4))
plt.plot(x)
plt.plot(M)
plt.plot(V)
plt.show()
Modéliser la fonction sinus sur l’intervalle 0, 2π.
import numpy as np
import matplotlib.pyplot as plt
# Définition de l'intervalle
xmin = 0
xmax = 2 * np.pi
nb_points = 1000
# Tracé de la courbe
plt.plot(x, y, label='sin(x)')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.title('Fonction sinus sur [0, 2π]')
plt.show()