Function PolyA(ByVal MatX As Range, ByVal MatY As Range, ByVal N As Long, Optional ByVal I As Variant = 1)
'Calcul du coefficient "Ci" de l'équation polynomiale de degré n
'calculé par les moindres carrés des points donnés Pt(xi,yi)
'Soit xi = matx et yi = maty
'Y = C1*X^n + Ci*X^(n-1) + ... + Cn.

'Traitement de l'index
I = CLng(I)

'Résolution matricielle

'Tailles matrices
Dim l As Long, L2 As Long, C As Long, C2 As Long
l = MatX.Rows.Count
L2 = MatY.Rows.Count
C = MatX.Columns.Count
C2 = MatY.Columns.Count

'Erreur de taille
If C > 1 Or C2 > 1 Then PolyA = "#COLONNE!": Exit Function
If l <> L2 Then PolyA = "#LIGNE!": Exit Function
If l < N - 1 Then PolyA = "#DEGRE!": Exit Function
If I - 1 > N Then PolyA = "#INDICE!": Exit Function

'calcul la matrice rectangulaire en X
ReDim coefa(1 To l, 1 To N + 1)
Dim t As Long, tt As Long, X As Double
For t = 1 To l
    X = MatX.Cells(t)
For tt = 1 To N + 1
    coefa(t, tt) = X ^ (N + 1 - tt)
Next tt, t

'matrice Y
ReDim coefb(l)
For t = 1 To l
    coefb(t) = MatY.Cells(t)
Next t

'Redéfinition matricelle carré selon
'la méthode des moindres carrés
'Matrice X
ReDim MatA(1 To N + 1, 1 To N + 1)
Dim m As Long, S As Double
For tt = 1 To N + 1
    For m = 1 To N + 1
        S = 0
        For t = 1 To l
            S = S + coefa(t, tt) * coefa(t, m)
        Next t
    MatA(tt, m) = S
Next m, tt

'Redéfinition matricielle carré
'Matrice Y
ReDim MatB(N + 1, 1)
For tt = 1 To N + 1
    S = 0
    For t = 1 To l
        S = S + coefa(t, tt) * coefb(t)
    Next t
    MatB(tt, 1) = S
Next tt

'inverse matA et garde la ligne i
ReDim mataa(1, N + 1)
For t = 1 To N + 1
    mataa(1, t) = InverseTabMat(MatA(), I, t)
Next t

'produit deux lignes
PolyA = ProduitTabMat(mataa(), MatB(), 1, 1)

End Function