=====Outil pour le développement en ligne=====
https://www.tutorialspoint.com/execute_matplotlib_online.php
=====Code fourni pour l'affichage====
listx=[]
listfx=[]
#xmin=-4
#xmax=4
xmin=min(px)-1
xmax=max(px)+1
# https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.html
#https://matplotlib.org/3.4.3/gallery/subplots_axes_and_figures/subplot.html#sphx-glr-gallery-subplots-axes-and-figures-subplot-py
# j'aurai pu faire avec un np.linspace
for i in range(int(xmin*100),int(xmax*100),1):
x=i/100.0
#fx=f(x,a,b,c)
fx=a*x+b
listx.append(x)
listfx.append(fx)
plt.plot(listx, listfx,'g-')
plt.show()
=====Solution AII1=====
import matplotlib.pyplot as plt
import numpy as np
#import math
#px=[1,2]
#py=[-3,3]
px=[1,2,-3,3]
py=[-3,3,-23,9]
#test pour determinant =0
#px=[1,1]
#py=[-3,-3]
nbpts=len(px)
#A = np.zeros((nbpts,2))
A = np.zeros((nbpts,3))
B = np.zeros((nbpts))
for i in range(0,nbpts):
#A[i,0]=px[i]
#A[i,1]=1
A[i,0]=px[i]*px[i]
A[i,1]=px[i]
A[i,2]=1
B[i]=py[i]
print("A:"+str(A))
print("B:"+str(B))
#if nbpts==2:
if nbpts==3:
detA=np.linalg.det(A)
#if detA==0:
#if math.fabs(detA)<1e-15:
if np.abs(detA)<1e-15:
print("le systeme d'équations n'admet pas de solution")
exit()
Ainv=np.linalg.inv(A)
else:
#calcul de la pseudo inverse
At=np.matrix.transpose(A)
AtA=np.dot(At,A)
Ainv=np.dot(np.linalg.inv(AtA),At)
print("Ainv:"+str(Ainv))
Solution=np.dot(Ainv,np.transpose(B))
print("Solution:"+str(Solution))
a=Solution[0]
b=Solution[1]
c=Solution[2]
listx=[]
listfx=[]
#xmin=-4
#xmax=4
xmin=min(px)-1
xmax=max(px)+1
# https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.html
#https://matplotlib.org/3.4.3/gallery/subplots_axes_and_figures/subplot.html#sphx-glr-gallery-subplots-axes-and-figures-subplot-py
# j'aurai pu faire avec un np.linspace
for i in range(int(xmin*100),int(xmax*100),1):
x=i/100.0
#fx=f(x,a,b,c)
fx=a*pow(x,2)+b*x+c
#fx=a*pow(x,1)+b
listx.append(x)
listfx.append(fx)
plt.plot(listx, listfx,'g-')
plt.scatter(px, py)
plt.show()
=====Solution AII2=====
import matplotlib.pyplot as plt
import numpy as np
px=[1,20,-3,10]
py=[-3,3,-23,13]
#test pour determinant =0
#px=[1,1]
#py=[-3,-3]
nbpts=len(px)
A = np.zeros((nbpts,3))
B = np.zeros((nbpts))
for i in range(0,nbpts):
#A[i,0]=px[i]
#A[i,1]=1
A[i,0]=px[i]*px[i]
A[i,1]=px[i]
A[i,2]=1
B[i]=py[i]
print("A:"+str(A))
print("B:"+str(B))
if nbpts==3:
detA=np.linalg.det(A)
#if detA==0:
if np.abs(detA)<1e-15:
print("le systeme d'équations n'admet pas de solution")
exit()
Ainv=np.linalg.inv(A)
else:
#calcul de la pseudo inverse
At=np.matrix.transpose(A)
AtA=np.dot(At,A)
Ainv=np.dot(np.linalg.inv(AtA),At)
print("Ainv:"+str(Ainv))
Solution=np.dot(Ainv,np.transpose(B))
print("Solution:"+str(Solution))
a=Solution[0]
b=Solution[1]
c=Solution[2]
listx=[]
listfx=[]
#xmin=-4
#xmax=4
xmin=min(px)-1
xmax=max(px)+1
# https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.html
#https://matplotlib.org/3.4.3/gallery/subplots_axes_and_figures/subplot.html#sphx-glr-gallery-subplots-axes-and-figures-subplot-py
# j'aurai pu faire avec un np.linspace
for i in range(int(xmin*100),int(xmax*100),1):
x=i/100.0
#fx=f(x,a,b,c)
#fx=a*x+b
fx=a*x*x+b*x+c
listx.append(x)
listfx.append(fx)
plt.plot(listx, listfx,'g-')
plt.scatter(px, py)
plt.show()