TUdataLSq1D TCL UTILITIES USER MANUAL TUdataLSq1D
SYNOPSIS
Perform a generalized least squares fit to a set of 1D data.
PACKAGE
TCLUTILS
NAME
TUdataLSq1D
USAGE
set rV  [TUdataLSq1D  X Y nP wMode CoeF sCoeF nC CoCar FunC {SigY}]
INPUT DEFINITIONS
X - Input array of X values
Y - Input array Y values
nP - The number of input (X,Y) data pairs
wMode - The weighting mode to use
-1 : Weight data as 1/Y. Y = 0 has a weighting function of 1.
0 : No weighting applied. All data have weighting function of 1.
1 : Used the supplied weighting function SigY.
CoeF - The return fit coefficients. Can be an empty array if all coefficients are to be fit. If not, the fixed (known) coefficients must be supplied in the array.
sCoeF - The coefficient status array. If an undefined array then all coefficients are marked as needing to be fit.
0 : Use existing coefficient in fit.
1 : Coefficient needs to be fit.
nC - Number of coefficients to use in fit.
CoVar - The covarience matrix produced in the fit.
FunC - A procedure returns the multiplier for each coefficient in the function being fit. When fitting to a nC - 1 order polynomial the built in function TUpolyFunc can be used.
SigY - Optional array of weighting values to use in the fit. Used only if wMode is set to 1.
RETURN DEFINITION
rV - ChiSq value of the fit.
DESCRIPTION
TUdataLSq1D performs a least squares fit to a set of data using an arbitrary polynomial expression of the form:
          Y = A(0) + A(1)Xa + A(2)Xb + ... + A(nC -1)Xz
      
returning the coefficients with their assocaited covarience matrix. The Χ2 to the fit is returned through the function. A function of the form FunC X CoeFv nC is required to return the multiplier (Xj for each of the coefficients in the solution where X is the input X value, nC is the number of coefficients, and CoeFv is an array of multipliers. If the function being fit is a simple polynomial of the form
          Y = A(0) + A(1)X + A(2)X2 + ... + A(nC-1)XnC-1
      
then the builtin function TUpolyFunc can be used. The routine will solve for all or a subset of the coefficients. Coefficients to is solved for have a 1 in their position in the sCoeF array. if sCoeF is an undefined array then all coefficients are solved for. Any coeffieicnt which is not being solved for must have its value given in the CoeF array.
ERRORS
None Generated
C BACKING
Yes
EXAMPLE(S)
EXAMPLE 1:
# FIT a data set to a polynomial of the form A0 + A1*X + A2*X^2
#
# FORM a noisey data set

for { set I 0 } { $I < 40 } { incr I } {
   set X($I) [expr double($I)]
   set Noise [expr $I * [TUdataRnd1 dV 1 PN 0.1]]
   set Y($I) [expr $Noise + 3.0 + 5.0 * double($I) - 7.0 * $I * $I]
}

# SOLVE for all coefficients

set iA(0) 1 ; set iA(1) 1 ; set iA(2) 1
set ChiSq [TUdataLSq1D X Y 40 0 A iA 3 CoVar TUpolyFunc ]
 
puts stderr [format "A0 is %.3e with CoVarience %.3e" $A(0) $CoVar(0)] 
puts stderr [format "A1 is %.3e with CoVarience %.3e" $A(1) $CoVar(4)] 
puts stderr [format "A2 is %.3e with CoVarience %.3e" $A(2) $CoVar(8)] 
puts stderr [format "Chi-Square: %.3e" $ChiSq]

> A0 is 2.554e+00 with CoVarience 2.039e-01
> A1 is 5.120e+00 with CoVarience 2.870e-03
> A2 is -7.004e+00 with CoVarience 1.763e-06
> Chi-Square: 6.828e+01
      
EXAMPLE 2:
# FIT a data set to a polynomial of the form A0 + A1*X + A2*X^2
#
# FORM a noisey data set

for { set I 0 } { $I < 40 } { incr I } {
   set X($I) [expr double($I)]
   set Noise [expr $I * [TUdataRnd1 dV 1 PN 0.1]]
   set Y($I) [expr $Noise + 3.0 + 5.0 * double($I) - 7.0 * $I * $I]
}

# SOLVE for all coefficients but first which we will fix at 3.0

set A(0) 3.0
set iA(0) 0 ; set iA(1) 1 ; set iA(2) 1
set ChiSq [TUdataLSq1D X Y 40 0 A iA 3 CoVar TUpolyFunc ]
 
puts stderr [format "A0 is %.3e with CoVarience %.3e" $A(0) $CoVar(0)] 
puts stderr [format "A1 is %.3e with CoVarience %.3e" $A(1) $CoVar(4)] 
puts stderr [format "A2 is %.3e with CoVarience %.3e" $A(2) $CoVar(8)] 
puts stderr [format "Chi-Square: %.3e" $ChiSq]

> A0 is 3.000e+00 with CoVarience 0.000e+00
> A1 is 5.075e+00 with CoVarience 7.796e-04
> A2 is -7.003e+00 with CoVarience -2.468e-05
> Chi-Square: 6.925e+01
      
EXAMPLE 3:
# FIT a data set to a polynomial of the form A0 + A1*sqrt(X) + A2*X
#
# FORM the function to return the coefficient multipliers

proc FunC { X cV nC} {
   upvar $cV A
                                                                                
   set A(0) 1.0
   set A(1) [expr sqrt($X)]
   set A(2) $X
}

for { set I 0 } { $I < 40 } { incr I } {
   set X($I) [expr double($I)]
   set Rv [expr $I * [TUdataRnd1 dV 1 PN 0.05]]
   set Y($I) [expr $Rv + 3.0 + 5.0 * sqrt(double($I)) - 7.0 * $I]
}

# SOLVE for all coefficients

set iA(0) 1 ; set iA(1) 1 ; set iA(2) 1
set ChiSq [TUdataLSq1D X Y 40 0 A iA 3 CoVar FunC ]
 
puts stderr [format "A0 is %.3e with CoVarience %.3e" $A(0) $CoVar(0)] 
puts stderr [format "A1 is %.3e with CoVarience %.3e" $A(1) $CoVar(4)] 
puts stderr [format "A2 is %.3e with CoVarience %.3e" $A(2) $CoVar(8)] 
puts stderr [format "Chi-Square: %.3e" $ChiSq]

> A0 is 2.553e+00 with CoVarience 5.604e-01
> A1 is 5.454e+00 with CoVarience 1.869e-01
> A2 is -7.077e+00 with CoVarience 3.401e-03
> Chi-Square: 1.821e+01

      
Sept 14, 2006