Function ResolMatA(ByVal MatX As Range, ByVal MatY As Range, Optional ByVal I As Variant = 1)
'Résolution matricielle par la méthode des moindres carrés

'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 C2 > 1 Then ResolMatA = "#COLONNE!": Exit Function
If l <> L2 Then ResolMatA = "#LIGNE!": Exit Function
If I > C Then ResolMatA = "#INDICE!": Exit Function

'calcul la matrice rectangulaire en X
ReDim coefa(1 To l, 1 To C)
Dim t As Long, tt As Long
For t = 1 To l
    For tt = 1 To C
        coefa(t, tt) = MatX.Cells(t, 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 C, 1 To C)
Dim m As Long, S As Double
For tt = 1 To C
    For m = 1 To C
        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(C, 1)
For tt = 1 To C
    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, C)
For t = 1 To C
    mataa(1, t) = InverseTabMat(MatA(), I, t)
Next t

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

End Function