Dynamique d'une population population avec migrations internes
import numpy as np
import matplotlib.pyplot as plt
# commande "magique" pour améliorer la visualiation
%matplotlib notebook
# %matplotlib inline
# Changer ici par votre Prenom Nom:
prenom = "Joseph" # à remplacer
nom = "Salmon" # à remplacer
extension = ".ipynb"
tp = "TP3_HMLA310"
# utiliser filename pour votre nom de TP
filename = "_".join([tp, prenom, nom]) + extension
filename = filename.lower()
print(filename)
def population_array(n, pop_init, tau_0, tau_1):
total_pop = np.zeros((n + 1, 2))
total_pop[0, :] = pop_init
Mat = ### XXXX
if n == 0:
return total_pop, Mat
else:
for i in range(0, n):
total_pop[i + 1, :] = ### XXXX
return total_pop, Mat
u_0 = 51
r_0 = 9.
pop_init = np.array([r_0, u_0])
tau_0 = 0.3 # taux d'exode rural
tau_1 = 0.1 # taux d'exode urbain
n = 10
pop, Mat = population_array(n, pop_init, tau_0, tau_1)
annee = np.arange(0, n + 1)
fig = plt.figure(figsize=(6, 4))
### XXX
plt.title('Évolution de la population')
plt.xlabel('Années')
plt.ylim(0, 70) # set the xlim to left, right
plt.xlim(0, n) # set the xlim to left, right
plt.ylabel('Population (en M)')
plt.legend(['Ruraux', 'Urbain'])
### XXX
### XXX
plt.legend(['Ruraux', 'Urbain', 'Total'])
fig2 = plt.figure(figsize=(6, 4))
# cycle va permettre de prendre les couleurs dans l'ordre de la palette.
cycle = plt.rcParams['axes.prop_cycle'].by_key()['color']
plt.plot(annee, pop[:,0], color=cycle[0])
plt.plot(annee, pop[:,1], color=cycle[1])
plt.plot(annee, total, color='k')
plt.legend()
### XXXX
Remarque: pour les curieux de mathématiques on pourra aller voir https://fr.wikipedia.org/wiki/Cha%C3%AEne_de_Markov et le théorème central de cette théorie: https://fr.wikipedia.org/wiki/Th%C3%A9or%C3%A8me_de_Perron-Frobenius
### XXX
pop_converence
np.linalg.eig?
eig_val
#### XXXX
np.allclose?
np.linalg.matrix_power?
np.allclose?
Fractales: l'ensemle de Mandelbrot
# Paramètres pour l'affichage:
# équivalent de plt.axis('off') pour toutes les images
import matplotlib as mpl
mpl.rc('axes.spines', top=False, bottom=False, left=False, right=False)
mpl.rc(('xtick', 'ytick'), color=(1, 1, 1, 0))
mandelbrot_mat = np.zeros((largeur, hauteur))
for x in range(hauteur):
cx = (x * (xmax - xmin) / hauteur + xmin)
for y in range(largeur):
cy = (y * (ymin - ymax) / largeur + ymax)
xn, yn, n = 0, 0, 0
while (xn**2 + yn**2) < 4 and n < max_iteration:
tmp_x, tmp_y = xn, yn
xn = tmp_x**2 - tmp_y**2 + cx
yn = 2 * tmp_x * tmp_y + cy
n = n + 1
if n < max_iteration:
mandelbrot_mat[y, x] = 1.
def mandelbrot(max_iteration, hauteur, largeur, xmin, xmax, ymin, ymax):
"""Création de l'ensemble de Mandelbrot."""
### XXX
return
# Un exemple de visualisation de l'ensemble de Mandelbrot:
xmin, xmax, ymin, ymax = -2, 0.5, -1.25, 1.25
max_iteration = 100 # 1000 # high resolution
largeur, hauteur = 500, 500 # 1000 # high resolution
mandelbrot_mat = ### XXX
fig3 = plt.figure(figsize=((6, 6)))
plt.imshow?
max_iterations = [1, 2, 5, 10, 20, 50, 100]
largeur, hauteur = 500, 500 # taille de la fenêtre en pixels
nb_iterations = len(max_iterations)
mandelbrot_mats = np.zeros((largeur, hauteur, nb_iterations))
for row, max_iteration in enumerate(max_iterations):
#### XXX
fig, axes = plt.subplots(2,4,figsize=(8,4))
axs = axes.ravel()
for i in range(len(max_iterations)):
axs[i].plot(np.sin(np.linspace(0,1,num=100) * i *2 * np.pi))
plt.show()
def mandelbrot_color(max_iteration, hauteur, largeur, xmin, xmax, ymin, ymax):
"""Création de l'ensemble de Mandelbrot."""
mandelbrot_mat = np.zeros((largeur, hauteur))
for x in range(hauteur):
cx = (x * (xmax - xmin) / hauteur + xmin)
for y in range(largeur):
cy = (y * (ymin - ymax) / largeur + ymax)
xn, yn, n = 0, 0, 0
while (xn**2 + yn**2) < 4 and n < max_iteration:
tmp_x, tmp_y = xn, yn
xn = tmp_x**2 - tmp_y**2 + cx
yn = 2 * tmp_x * tmp_y + cy
n = n + 1
mandelbrot_mat[y, x] = #### XXX
return mandelbrot_mat
# Un exemple de visualisation de l'ensemble de Mandelbrot:
xmin, xmax, ymin, ymax = -2, 0.5, -1.25, 1.25
max_iteration = 200 # 1000 #high resolution
largeur, hauteur = 500, 500 # 1000 #high resolution
mandelbrot_mat = mandelbrot_color(max_iteration, hauteur, largeur, xmin, xmax, ymin, ymax)
fig5 = plt.figure(figsize=((6, 6)))
plt.imshow(mandelbrot_mat)