Telechargé par samuel zougbede

TP Objet1

publicité
# TP Objet1, norme, produit scal, moment, produit vectoriel
class vecteur:
x, y, z = 0, 0, 0
def __init__(self, cord):
self.x = cord[0]
self.y = cord[1]
self.z = cord[2]
def affich(self):
print([self.x, self.y, self.z, self.t])
def norme(self):
#Soit M la norme d'un vecteur
M = ((self.x)**2 + (self.y)**2 + (self.z)**2)**(0.5)
print('La norme est: ', M)
def prod(self, other):
#Soit P le produit scalaire de deux vecteurs
P = (self.x)*(other.x) + (self.y)*(other.y) + (self.y)*(other.z)
print('Le produit scalaire est: ', P)
def vectoriel(self, other):
x1 = self.y*other.z - other.y*self.z
y1 = self.x*other.z - other.x*self.z
z1 = self.x*other.y - other.x*self.y
return [x1, y1, z1]
def neg(self):
x1 = -self.x
y1 = -self.y
z1 = -self.z
print([x1, y1, z1])
def add(self, other):
x1 = self.x + other.x
y1 = self.y + other.y
z1 = self.z + other.z
print([x1, y1, z1])
def diff(self, other):
x1 = self.x - other.x
y1 = self.y - other.y
z1 = self.z - other.z
print([x1, y1, z1])
u = vecteur([1, 2, 3])
v = vecteur([4, 5, 6])
u.norme()
v.norme()
u.prod(v)
v.vectoriel(u)
u.neg()
v.neg()
u.add(v)
u.diff(v)
class polynome:
coeff, x, a, b = [], 0, 0, 0
def __init__(self, coeff, x, a, b):
self.coeff = coeff
self.x = x
self.a = a
self.b = b
def poly(self): # poly donne la valeur de P(x) = A0 + A1X^1 + A2X^2 + ... + AnX^n
a0 = self.coeff[0] # ceci est le premier coefficient du polynome (A0)
for n in range(1, len(self.coeff)):
p = a0 + self.coeff[n]*self.x**n
a0 = p
return p
def derive(self): # Cette methode calcul la derive en un point x
a1 = 0
for n in range(1, len(self.coeff)):
v = a1 + n*self.coeff[n]*self.x**(n-1)
a1 = v
return v
def integral(self): # cette methode calcul l'integrale de P(x) sur [a, b], a < b
a2 = self.coeff[0]*(self.b - self.a)
for n in range(1, len(self.coeff)):
h = a2 + (self.coeff[n]/(n+1))*(self.b**(n+1) - self.a**(n+1))
a2 = h
return h
##############
##Class ended##
##############
u = polynome(coeff = [1, 2], x = 1, a = 1, b = 2) #Ici j'appelle la class polynome
print('P(x) = ', u.poly())
print('dP(x) = ', u.derive())
print('integral_P(x) = ', u.integral())
from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 20, 100)
def model(y, x, k):
dy= k*y
return dy
k = 0.1
y0 = odeint(model, 5, x, (k, ))
k = 0.3
y1 = odeint(model, 5, x, (k, ))
k = 0.5
y2 = odeint(model, 5, x, (k, ))
k = 1.2
y3 = odeint(model, 5, x, (k, ))
plt.plot(x, y0)
plt.plot(x, y1)
plt.plot(x, y2)
plt.plot(x, y3)
plt.show()
import scipy.integrate as Int
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 20, 100)
def model(x, y, k):
dxdy = -k*y + np.sin(y)
return dxdy
#y = Int.odeint(model, 0, x, (k, ), u)
k = 0.1
y0 = Int.odeint(model, 0, x, (k, ))
plt.plot(x, y0)
plt.show()
from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt
def model(y, x):
y1 = y[0]
y2 = y[1]
dy1 = np.exp(-y1)
dy2 = -y2 + 1
return [dy1, dy2]
x = np.linspace(0, 10, 100)
e0 = [1, 0]
Y = odeint(model, e0, x)
plt.figure(figsize = (15, 5))
plt.plot(x, Y)
#print(Y)
plt.xlabel('x')
plt.ylabel('y(x)')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
class equadiff:
coeff = []
cond = []
# Equadiff de la forme: a*d²x/dt² + b*dx/dt + c*x
def __init__(self, coeff, cond):
self.coeff = coeff
self.cond = cond
def resolve(self):
def resolution(y, x):
x = y[0]
p = y[1]
dx = p
dp = -(self.coeff[1]/self.coeff[0])*p - (self.coeff[2]/self.coeff[0])*x
return [dx, dp]
x = np.linspace(0, 10, 1000)
y = odeint(resolution, self.cond, x)
#print(y)
#print(y[0, :])
#print(y[0:5, 0:1])
plt.figure(figsize = (15, 5))
plt.xlabel('x')
plt.ylabel('y(x)')
plt.plot(x, y, linewidth = 2)
plt.show()
equadiff([1, 0.2, 2*np.pi], [10, 10]).resolve()
from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt
def model(f, x):
f0 = f[0]
f1 = f[1]
df0 = 5*f0
df1 = 2*f1
return df0
x = np.linspace(0, 5, 100)
y = odeint(model, 0, x)
plt.plot(x, y)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
def model(u, t, E):
du = 10**(9)*(E-u)
return du
t = np.linspace(0, 0.001, 100)
E=1
U = odeint(model, 0, t, (E, ))
plt.plot(t, U)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
def model(x, t):
x0 = x[0]
p = x[1]
dx = p
dp = 0.1*x0
return [dx, dp]
t = np.linspace(0, 10, 100)
Y = odeint(model, 0, t)
plt.plot(t, Y)
plt.show()
Téléchargement