Skip to content
Snippets Groups Projects
Commit 256ccddd authored by Malo Auffray's avatar Malo Auffray
Browse files

tp1

parent 869b74c4
No related branches found
No related tags found
No related merge requests found
import autograd as ag
import autograd.numpy as np
import matplotlib.pyplot as plt
import scipy.linalg as nla
""" Question1"""
alpha, beta, gamma, mu = 0.5, -2, 1, 7
m = 4
def f(x,alpha,beta,gamme,mu):
return alpha*x**3 + beta*x**2 + gamma *x + mu
xplot = np.linspace(-1.2,2.1,50)
yplot = f(xplot,alpha, beta, gamma, mu)
Tx = np.array ([-1.1, 0.17, 1.22, -.5, 2.02, 1.81])
p = Tx.shape[0]
Ty_sur_la_courbe = np.array ([f(x,alpha, beta, gamma, mu) for x in Tx])
perturbations = 0.5*np.array ([-1.3, 2.7, -5, 0, 1.4, 6])
Ty_experimentaux = Ty_sur_la_courbe + perturbations
erreur_initiale = np.linalg.norm(Ty_sur_la_courbe-Ty_experimentaux)
print(erreur_initiale)
plt.scatter(Tx,Ty_sur_la_courbe,vmin=-1.2, vmax=2.1)
plt.plot(xplot,yplot,color='blue')
b = Ty_experimentaux
A = np.array([[x**3,x**2,x,1] for x in Tx], dtype = np.float64)
Q1 , R1 = nla.qr(A, mode = 'economic')
Q1Tb = np.dot(np.transpose(Q1),b)
alpha , beta , gamma , mu = nla.solve_triangular(R1, Q1Tb, lower = False)
print(alpha , beta , gamma , mu)
Ty = np.array([f(x,alpha, beta, gamma, mu) for x in Tx])
yplot= f(xplot,alpha, beta, gamma, mu)
plt.scatter(Tx,Ty_experimentaux,vmin=-1.2, vmax=2.1)
plt.plot(xplot, yplot)
"""2 Méthode de Newton en une variable"""
# Question 2
def g(x):
return x**3 -2
def gprime(x):
return 3*x**2
def fprime(x):
return 3 * alpha * x**2 + 2*beta*x + gamma
def fsecond(x):
return 6 * alpha * x + 2*beta
u = 4
Tx = []
for i in range(10):
print('u[%d] =' %i,u)
u = u - g(u)/gprime(u)
u= 4
for i in range(10):
print('u[%d] =' %i,u)
u = u - fprime(u)/fsecond(u)
Tx.append(u)
plt.scatter(Tx,[0 for x in Tx])
plt.axhline(label='question3')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment