diff --git a/tp1/TP1.py b/tp1/TP1.py new file mode 100644 index 0000000000000000000000000000000000000000..2244db65d6d53fcb958b88535496da449904e26d --- /dev/null +++ b/tp1/TP1.py @@ -0,0 +1,107 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Tue Nov 26 10:30:38 2024 + +@author: dlahanqu +""" + +import autograd as ag +import autograd.numpy as np +import matplotlib.pyplot as plt +import scipy.linalg as nla + +def f(x): + return 0.5*x**3 - 2*x**2 +x +7 + +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) 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, 2) +print(erreur_initiale) + +plt.scatter(Tx, Ty_sur_la_courbe) +plt.xlim(-1.2,2.1) +plt.show() + +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), Ty_experimentaux) + +alpha, beta, gamma, mu = nla.solve_triangular(R1, Q1Tb, lower = False) + +print(alpha,beta, gamma, mu) + +def f_ajuster(x): + return alpha*x**3 + beta* x**2 + gamma *x + mu + +erreur_mini = np.linalg.norm(Ty_experimentaux-[f_ajuster(x) for x in Tx], 2) +print(erreur_mini) + +"""plt.scatter(Tx, [f_ajuster(x) for x in Tx]) +xplot = np.linspace(-1.2, 2.1, 50) +yplot = np.array([f_ajuster(x) for x in xplot]) +plt.plot(xplot, yplot)""" + + +plt.close() +# METHODE NEWTON A UNE VARIABLE +def f(x): + return x**3 - 2 + +def f_prime(x): + return 3*x**2 + +def un_suiv(x): + return x- f(x)/f_prime(x) + +Tx=[] +un = 2 +for i in range(10): + Tx.append(un) + print('u[%d] =' % i, un) + un = un_suiv(un) + +print(un) + +def f(x): + return alpha*x**3 + beta* x**2 + gamma *x + mu + +xplot = np.linspace(-1,2, 50) +yplot = np.array([f(x) for x in xplot]) +plt.scatter(Tx, [0 for x in Tx]) +plt.plot(xplot, yplot) + +def f(x): + return alpha*x**3 + beta* x**2 + gamma *x + mu + +def f_prime(x): + return 3*alpha*x**2 + 2*beta*x +gamma + +def f_seconde(x): + return 6*alpha*x + 2*beta + +Tx=[] +u = 1 +for i in range(10): + Tx.append(u) + print('u[%d] = ' %i, u) + u = u - f_prime(u)/f_seconde(u) + +xplot = np.linspace(-1,2, 100) +yplot = np.array([f_prime(x) for x in xplot]) +plt.scatter(Tx, [0 for x in Tx]) +plt.plot(xplot, yplot) +plt.axhline() +plt.show() + + + + + +