| TUdataLSq1D | TCL UTILITIES USER MANUAL | TUdataLSq1D |
|---|
| set |
[TUdataLSq1D  |
X
Y
|
| 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
|
|||||||||
| 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.
|
|||||||||
| 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. |
| rV | - | ChiSq value of the fit. |
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.
# 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
# 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
# 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 |
|---|