• Aucun résultat trouvé

TD 8 - np.array - Euler CORRIGÉ 0 - Les np.array

N/A
N/A
Protected

Academic year: 2022

Partager "TD 8 - np.array - Euler CORRIGÉ 0 - Les np.array"

Copied!
4
0
0

Texte intégral

(1)

TD 8 - np.array - Euler

CORRIGÉ 0 - Les np.array

import numpy as np from random import *

# --- 1 ---

z = np.zeros((4,4))

# --- 3 ---

h = np.array( [[randint(1,6) for j in range(4)] for i in range(4)] )

# --- 5 ---

b = np.array( [[i+j for j in range(4)] for i in range(4)] )

# --- 6 ---

a = 100*np.sin(b)**2

# --- 7 et 8 ---

X1 = a[:,2] # renvoie un tableau 1D !!

X2 = a[:,2:3] # renvoie un tableau 2D !!

# --- 9 ---

a = np.dot(a[:,1:2] , a[0:1,:]) # a = (sa 2ème colonne) * (sa 1ère ligne)

# --- 10 ---

for k in range(1,4):a[0]+=a[k]

# --- 11 ---

aa = a # cela ne crée pas de copie de a dans la mémoire. aa[1,1]=69 affectera a également ! aa = np.copy(a) # cela crée une copie de a dans la mémoire. aa[1,1]=69 n'affectera pas a

# --- 12 ---

b = np.zeros((4,4)) for i in range(4) :

for j in range(4) :

if i!=j : b[:,i] += a[:,j]

a = b/3 # pas besoin d'écrire a = np.copy(b)/3 car le fait de faire une opération sur b print(a) # force Python a créer un nouveau tableau...!

1

(2)

I - Ordre 1 tout bête +Calcul d'erreur

1. Dessiner sur[0,1]le graphe de la solution du problème de Cauchy : (

y0(x) =−xy(x) +ex

2 2

y(0) = 0 yH(x) =Kex

2

2 etyP(x) =K(x)ex

2

2 oùK(x) = Z x

0

et

2 2

| {z }

=b(t)

et

2 2

|{z}

=eA(t)

dt=xd'oùyP(x) =x ex

2 2 . Ainsi, commey(0) = 0, on trouveK= 0et donc y(x) =x ex

2 2

2. from math import *

import matplotlib.pyplot as plt import numpy as np

n = 20

y = [0]*(n+1) for k in range(n) :

h = 1.0/n x = k*h

y[k+1] = y[k] + h * (-x*y[k]+exp(-x*x/2)) plt.clf()

xx = np.linspace(0,1,n+1) plt.plot(xx,y)

plt.plot(xx,xx*np.exp(-xx*xx/2)) plt.show()

3. from math import *

import matplotlib.pyplot as plt import numpy as np

def ValeurEn1(n) : y = [0]*(n+1) for k in range(n) :

h = 1.0/n x = k*h

y[k+1] = y[k] + h * (-x*y[k]+exp(-x*x/2)) return y[n-1]

plt.clf()

nn = np.linspace(10,200,21)

yy = [ abs(ValeurEn1(int(n))-exp(-1/2)) for n in nn ] plt.plot(nn,yy)

plt.show()

II - Système d'équations diérentielles from math import *

import numpy as np

import matplotlib.pyplot as plt n = 1000

b = 2

x = [1]*(n+1) y = [1]*(n+1) for k in range(n):

t = b * k/n h = b/n

x[k+1] = x[k] + h * (t - x[k]*y[k]) y[k+1] = y[k] + h * (x[k] + y[k]) plt.plot(x,y)

plt.show()

2

(3)

III - Système matriciel d'équation diérentielles from math import *

import numpy as np

import matplotlib.pyplot as plt n = 100

b = 2

A = np.array([[1,3,-1],[0,2,1],[1,-1,1]]) X = np.zeros((3,n+1))

X[:,0] = [1,0,-1]

for k in range(n):

t = b * k/n h = b/n

X[:,k+1:k+2] = X[:,k:k+1] + h * np.dot(A,X[:,k:k+1]) plt.plot(np.linspace(0,b,n+1) , X[0,:])

plt.plot(np.linspace(0,b,n+1) , X[1,:]) plt.plot(np.linspace(0,b,n+1) , X[2,:]) plt.show()

IV - Evolution spacio-temporelle from math import *

import numpy as np

import matplotlib.pyplot as plt LongBarre = 2 # la barre mesure 2m tpsTot = 3 # l'expérience dure 3s nLong = 100

nDur = 1000

y = np.zeros((nLong+1,nDur+1)) # y[kx,kt] contient y à la position kx*LongBarre/nLong

y += 1 # et à l'instant kt*tpsTot/nDur

xx = np.linspace(0,LongBarre,nLong+1) # positions de la barre for k in range(nDur):

h = tpsTot/nDur # durée d'un pas de temps t = k/h # date au k-ième pas

y[:,k+1] = y[:,k] + h * y[:,k] * np.cos(xx*5) * abs(cos(t)) # on utilise des np.array à 1 dimension for kt in np.linspace(0,nDur,10) :

plt.plot(np.linspace(0,LongBarre,nLong+1) , y[:,int(kt)]) plt.show()

3

(4)

TD 8 - Bonus : Diagrammes de Feigenbaum

1. La fonction f est croissante sur[0,12]et décroissante sur[12,1], etf(0) =f(1) = 0 etf(12) = p4. L'intervalle[0,1]est donc stable parf

⇐⇒ p∈[0,4]

# --- 2 ---

import matplotlib.pyplot as plt import numpy as np

def suite(p,ndeb,nfin) : u = [0.3]

for k in range (nfin) : u.append(p*u[-1]*(1-u[-1])) return u[ndeb:nfin+1]

# --- 3 ---

def escalier(p,nfin) :

xx = np.linspace(0,1,300)

plt.plot(xx,p*xx*(1-xx),color="#000055") #--- 1 --- graphe de fp plt.plot([0,1],[0,1] ,color="#0000FF") #--- 2 --- 1ère bissectrice u = suite(p,0,nfin)

ux,uy = [] , []

for k in range(len(u)-1) : ux.append(u[k])

ux.append(u[k])

if (k==0) : uy.append(0) else : uy.append(u[k]) uy.append(u[k+1])

plt.plot(ux,uy,color="#770000"); #--- 3 --- la ligne brisée plt.clf()

escalier(3.1,1000);

plt.show()

# --- 6 ---

for p in np.linspace(1,4,1000) : u = suite(p,100,200)

plt.plot([p]*len(u),u,",",color="#FF0000") plt.show()

# --- 7 ---

import matplotlib.pyplot as plt import numpy as np

def f(x,p) : return x*x*x-p*x+1 def fp(x,p) : return 3*x*x-p def Newton(p,nDeb,nFin) :

u = [0]

for k in range (nFin) :

u.append(u[-1] - f(u[-1],p)/fp(u[-1],p) )

plt.plot([p]*(nFin-nDeb+1),u[nDeb:nFin+1],",",color="#FF0000") plt.clf()

pTabl = np.linspace(1.26,1.29,1000);

for p in pTabl : Newton(p, 100,200) plt.show()

4

Références

Documents relatifs

Université Ibn Zohr Année universitaire. Faculté des Sciences d’Agadir Filières

[r]

En tout, j'ai cumulé 620 min de pose avec le filtre Halpha, 500 min de pose avec le filtre OIII et 460 min de pose avec le filtre SII soit au total 26 h et 20 min de pose, pour

Quelle diérence y'a-t-il entre eectuer les instructions aa=a et

Join us in our mission: Positions for long-term research assistants, PhD students, and postdocs. Visit meelgroup.github.io for details on how

Noter aussi qu'on suppose implicitement, ci-dessus, que le coût d'une division est constant, alors qu'il est en O ( n 2 ), et donc la complexité est rigoureusement O (

VertexCover est dans NP, parce que la question de l’appartenance d’un couple (G, k) revient ` a une question d’existence d’une solution et parce sur la donn´ ee d’un couple (G,

[r]