TUdataLSq2D TCL UTILITIES USER MANUAL TUdataLSq2D
SYNOPSIS
Perform a 2D least squares fit to a set of data.
PACKAGE
TCLUTILS
NAME
TUdataLSq2D
USAGE
set rV  [TUdataLSq2D  X Y V nP nOrder Mode CoeF {Var} {SigV}]
INPUT DEFINITIONS
X - Input array of X values
Y - Input array of Y values
V - Input array of V values
nP - The number of input (X,Y,V) data values
nOrder - The order of the fitting polynomial
Mode - The weighting mode to use
-N : Weight data as (X*X + Y*Y)-N/2.0. (X*X + 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 SigV.
CoeF - The return fit coefficients.
Var - Optional input which indicated if the varience to fit should be computed and returned. Default is Yes.
0 : No
1 : Yes
SigV - Optional array of weighting values to use in the fit. Used only if wMode is set to 1.
RETURN DEFINITION
rV - This is a list containing the number of coefficients used in the fit and the varience of the fit to the data. The latter is included in the list only if Var is 1.
DESCRIPTION
TUdataLSq2D performs a least squares 2D fit to a set of data set using the polynomial expression
          Z = A(0) + A(1)*X + A(2)*Y + A(3)*X2 + A(4)*X*Y + A(5)*Y2 ... 
      
returning the coefficients A. Both the number of coefficients and the varience (if computed) are returned through the function.
ERRORS
None Generated
C BACKING
Yes
EXAMPLE(S)
EXAMPLE 1:
# FIT a noisy circular data set to a 2nd order 2D polynomial.  The data 
#   is scattered about a ring centered on 0 with a radius of 4

# GENERATE the data.  Rx and Ry are x and y radii respectively.  X and
#   Y are used in the fit while Xp and Yp are used when plotting the data.

for { set I 0 } { $I < 360 } { incr I } {
   set Rx [expr 4.0 + [TUdataRnd1 V 1 PN .5]] 
   set Ry [expr 4.0 + [TUdataRnd1 V 1 PN .5]] 
   set Ag [expr $I + 2]
   set X($I) [expr cos($Ag / $RtoD)]
   set Y($I) [expr sin($Ag / $RtoD)]
   set Xp($I) [expr $Rx * cos($Ag / $RtoD)]
   set Yp($I) [expr $Ry * sin($Ag / $RtoD)]
   set Z($I) [expr $Xp($I) * $Xp($I) + $Yp($I) * $Yp($I)]
}

# SOLVE for the best fit to the data using a second order 2D polynomial.
                                                                                
set rV [TUdataLSq2D X Y Z 360 2 0 A 1]

# PRINT out the number of coefficients generated, the varience of the fit,
#    and the coefficients   

puts stderr "Number Of Coefficients: [lindex $rV 0]"
puts stderr "Varience   : [format "%.4f" [lindex $rV 1]]"
puts stderr "A(0) Const : [format "%.4f" $A(0)]"
puts stderr "A(1) X     : [format "%.4f" $A(1)]"
puts stderr "A(2) Y     : [format "%.4f" $A(2)]"
puts stderr "A(3) X**2  : [format "%.4f" $A(3)]"
puts stderr "A(4) XY    : [format "%.4f" $A(4)]"
puts stderr "A(5) Y**2  : [format "%.4f" $A(5)]"

# PLOT the input data set in white and the fit to the data in red.

GraphicsOn TK ETones SWAPON
SqWindow 1 0.15 0.15 0.0 0.70 -6. -6. 0. 6.0 6.0 0.
DefinePlot 1 1 -1 -1

set oP [list $GphInfo(White) <= -20.0 HOLD 8 +]
Plot2D 1 Xb Yb Xp Yp 0 359 SCATTER $oP

set oP [list $GphInfo(Red) <= -20.0 HOLD 8 *]
for { set I 0 } { $I < 360 } { incr I } {
   set R [TUpolyExp2D $X($I) $Y($I) 6 A 2]
   set Xp($I) [expr sqrt($R) * $X($I)]
   set Yp($I) [expr sqrt($R) * $Y($I)]
}
Plot2D 1 Xb Yb Xp Yp 0 359 SCATTER $oP

PlotOutlines 1 [list "X" 1.0 "Y" 1.0] [list BOT BOT BOTH BOTH]

update


>Number Of Coefficients: 6
>Varience   : 2.0132
>A(0) Const : 33.2120
>A(1) X     : -0.1289
>A(2) Y     : -0.1750
>A(3) X**2  : -16.9860
>A(4) XY    : -0.2740
>A(5) Y**2  : -17.1719

      
Sept 17, 2006