XLPack 6.1
Excel VBA Numerical Library Reference Manual
Loading...
Searching...
No Matches

◆ Covar()

Sub Covar ( N As  Long,
R() As  Double,
Ipvt() As  Long,
Tol As  Double,
Info As  Long 
)

Covariance matrix of nonlinear least squares approximation

Purpose
This routine computes the covariance matrix of nonlinear least squares approximation computed by Lmder, Lmder1, Lmdif, Lmstr or Lmstr1.

Given an M x N matrix A, the N x N symmetric matrix corresponding to A, called as the covariance matrix C, is defined as follows.
C = (A^T*A)^(-1)
This routine completes this equation using the necessary information from the QR factorization of A which is provided by Lmder, Lmder1, Lmdif, Lmstr or Lmstr1.
Parameters
[in]NOrder of covariance matrix. (N > 0)
[in,out]R()Array R(LR1 - 1, LR2 - 1) (LR1 >= N, LR2 >= N)
[in] The upper triangle matrix R obtained from QR factorization AP = QR (P is the permutation matrix given by Ipvt(), and Q is M x N orthogonal matrix)
[out] The square symmetric covariance matrix.
[in]Ipvt()Array Ipvt(LIpvt - 1) (LIpvt >= N)
The pivot indices that define the permutation matrix P.
[in]TolThe tolerance to determine the numerical rank of the matrix A. (Tol > 0)
If some diagonal element of R is less than (the maximum absolute value of diagonal elements of R)*Tol in magnitude, it will be regarded as rank deficient.
[out]Info= 0: Successful exit.
= -1: The argument N had an illegal value. (N <= 0)
= -2: The argument R() is invalid. (Array R() is not big enough)
= -3: The argument Ipvt() is invalid. (Array Ipvt() is not big enough)
= -4: The argument Tol had an illegal value. (Tol <= 0)
Reference
netlib/minpack
Example Program
Approximate the following data with model function f(x) = c1*(1 - exp(-c2*x)).
Determine two parameters c1 and c2 by Lmder1, and compute the covariance matrix by Covar.
f(x) x
10.07 77.6
29.61 239.9
50.76 434.8
81.78 760.0
Sub FLmder(M As Long, N As Long, X() As Double, Fvec() As Double, Fjac() As Double, IFlag As Long)
Dim Xdata(3) As Double, Ydata(3) As Double, I As Long
Ydata(0) = 10.07: Xdata(0) = 77.6
Ydata(1) = 29.61: Xdata(1) = 239.9
Ydata(2) = 50.76: Xdata(2) = 434.8
Ydata(3) = 81.78: Xdata(3) = 760
If IFlag = 1 Then
For I = 0 To M - 1
Fvec(I) = Ydata(I) - X(0) * (1 - Exp(-Xdata(I) * X(1)))
Next
ElseIf IFlag = 2 Then
For I = 0 To M - 1
Fjac(I, 0) = Exp(-Xdata(I) * X(1)) - 1
Fjac(I, 1) = -Xdata(I) * X(0) * Exp(-X(1) * Xdata(I))
Next
End If
End Sub
Sub Ex_Covar()
Const M = 4, N = 2
Dim X(N - 1) As Double, Fvec(M - 1) As Double, R(M - 1, N - 1) As Double
Dim Tol As Double, Ipvt(N - 1) As Long, Info As Long
Dim S As Double, I As Long
Tol = 0.00000001 '1.0e-8
X(0) = 500: X(1) = 0.0001
Call Lmder1(AddressOf FLmder, M, N, X(), Fvec(), R(), Tol, Ipvt(), Info)
Debug.Print "Info =", Info
Call Covar(N, R(), Ipvt(), Tol, Info)
For I = 0 To M - 1
S = S + Fvec(I) ^ 2
Next
S = S / (M - N)
Debug.Print "Cov ="
Debug.Print R(0, 0) * S, R(0, 1) * S
Debug.Print R(1, 0) * S, R(1, 1) * S
Debug.Print "Info =", Info
End Sub
Example Results
Info = 0
Cov =
20.5868644246757 -5.52380489748924E-05
-5.52380489748924E-05 1.48678858325168E-10
Info = 0