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
| # -*- coding: utf-8 -*-
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
# Calcul des lignes de champ
wires = [(-1, -1, 0), (1, 1, 0)]
xmin, ymin, xmax, ymax = -4, -3, 4, 3
T = np.linspace(0, 10, 1000)
def fieldE(p, t):
x, y = p
Ex, Ey = 0, 0
for qk, xk, yk in wires:
r = np.sqrt((x - xk) ** 2 + (y - yk) ** 2)
Ex += qk * (x - xk) / r ** 2
Ey += qk * (y - yk) / r ** 2
return Ex, Ey
p0 = (1.2, .2)
Ut = odeint(fieldE, p0, T)
Xt = Ut[:, 0]
Yt = Ut[:, 1]
Xt, Yt = odeint(fieldE, (1.2,.3), T).transpose()
P0 = []
for x, y in [(1.2, .3), (1.2, .2), (1.2, .1), (1.2, .05)]:
P0.append((x, y))
P0.append((x, -y))
linefield = [odeint(fieldE, p, T).transpose() for p in P0]
# Graphique
plt.title("Lignes de champ")
plt.axis('equal')
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
ax = plt.gca()
# Représentation des lignes de champ
for Xt, Yt in linefield:
plt.plot(Xt, Yt, 'blue')
plt.plot(Xt[::50], Yt[::50], 'black', linestyle='none',
marker='+', markersize=4)
# Affichage du graphique
plt.show()
# Extraction des coordonnées
# des points des lignes de champ
lignes_de_champ = []
for Xt, Yt in linefield:
ligne_de_champ = np.empty(shape=(Xt.shape[0], 2))
for i in xrange(Xt.shape[0]):
ligne_de_champ[i] = [Xt[i], Yt[i]]
lignes_de_champ.append(ligne_de_champ)
# Affiche le nombre de lignes de champ
print(len(lignes_de_champ))
# Affiche les coordonnées des points de
# la première des (ici 8) lignes de champ
print(lignes_de_champ[0]) |
Partager