Data Fit Commands

From Flooxs
Jump to navigation Jump to search

Data Fit Commands

Data Fit Commands - commands used to extract data

Synopsis

Bestline
Use linear regression to fit a line to a data set.
Decay
Extract an exponential decay curve.
EaCalc
Extract an Arrhenius expression.

Description

These commands allow data analysis of lists. The lists can be generated from various [../Post.html#TCLReturn TCL return] commands or can be compiled during other processing. All the fitting commands use linear regression and operate on the data slightly differently.

All of these commands are tcl scripts. Therefore, they all have similar structure. Each operates on a list of data, similar to that generated by the [slice.html slice] command.

Bestline

This command does linear regression to extract the best fit line for the data set. The command takes a list of data, which consists of x,y pairs. The number of elements in the list is always even, therefore. This is implemented as a tcl script, and the script is:


proc Bestline {l} {
    set sumx 0.0
    set sumy 0.0
    set sum2x 0.0
    set sum2y 0.0
    set sumxy 0.0
    set num 0

    for {set i 0} {$i < [llength $l]/2} {incr i} {
	set x [lindex $l [expr 2*$i]]

	set y [lindex $l [expr 2*$i 1]]
	set sumx [expr $x   $sumx]
	set sum2x [expr $x * $x   $sum2x]
	set sumy [expr $y   $sumy]

	set sum2y [expr $y * $y   $sum2y]
	set sumxy [expr $x * $y   $sumxy]
	incr num
    }

    set denx [expr $num * $sum2x - $sumx * $sumx]
    set deny [expr $num * $sum2y - $sumy * $sumy]
    set top  [expr $num * $sumxy - $sumx * $sumy]

    set corr [expr $top / sqrt($denx * $deny)]
    set slp [expr $top / $denx]
    set inter [expr ($sumy - $sumx * $slp) / $num]

    return "$slp $inter $corr"
}

The command returns a three member list. The first member is the line slope, and the second is the y-intercept. The final member is the correlation of the fit, which is a measure of how good the fit is to the data. Absolute values of correlation close to one are desirable.

Decay

This command does linear regression to extract the best fit exponential for the data set. The command takes a list of data, which consists of x,y pairs. The number of elements in the list is always even, therefore. This is implemented as a tcl script, and the is very similar to the script for Bestline. The only difference is in the data manipulation. The y values when extracted are processed after taking the log. The best fit line then is computed on a semilog plot. The main accumulation loop becomes:


    for {set i 0} {$i < [llength $l]/2} {incr i} {
        set x [lindex $l [expr 2*$i]]

        set y [lindex $l [expr 2*$i 1]]
        set y [expr log($y)]
        set sumx [expr $x   $sumx]
        set sum2x [expr $x * $x   $sum2x]

        set sumy [expr $y   $sumy]
        set sum2y [expr $y * $y   $sum2y]
        set sumxy [expr $x * $y   $sumxy]
        incr num
    }

The return value is also changed based on the expectation of an exponential decay. The return statement is:


    return "[expr 1.0/$slp] [expr exp($inter)] $corr"

The command returns a three member list. The first member is the decay coefficient, and the second is the preexponential. The final member is the correlation of the fit, which is a measure of how good the fit is to the data. Absolute values of correlation close to one are desirable.

EaCalc

This command does linear regression to extract the best fit arrhenius expression for the data set. The command takes a list of data, which consists of x,y pairs. The number of elements in the list is always even. The first value is assumed to be a temperature in degrees centrigrade and the second is the value of the function at that temperature. This is implemented as a tcl script, and the is very similar to the script for Bestline. The only difference is in the data manipulation. The y values when extracted are processed after taking the log. The best fit line then is computed on a semilog plot. The main accumulation loop becomes:


    for {set i 0} {$i < [llength $l]/2} {incr i} {
	set x [lindex $l [expr 2*$i]]
	set y [lindex $l [expr 2*$i 1]]

	set x [expr 1.0 / (8.62e-5*($x 273.0))]
	set y [expr log($y)]
	set sumx [expr $x   $sumx]
	set sum2x [expr $x * $x   $sum2x]
	set sumy [expr $y   $sumy]

	set sum2y [expr $y * $y   $sum2y]
	set sumxy [expr $x * $y   $sumxy]
	incr num
    }

The return value is also changed based on the expectation of an Arrhenius expression. The return statement is:


    return "[expr -$slp] [expr exp($inter)] $corr"

The command returns a three member list. The first member is the activation energy, and the second is the preexponential. The final member is the correlation of the fit, which is a measure of how good the fit is to the data. Absolute values of correlation close to one are desirable.

Examples

Bestline {0 0 1 1 2 2.5 3 4}
Finds the best fit line to the above data. It returns "1.35 -0.15 0.99591", which means the best fit line has slope of 1.35 and y-intercept of -0.15. The correlation coefficient is 0.99591.

Decay {0 1.0e14 60 5.0e13 120 1.0e13 240 1.0e12}
Finds the best fit exponential decay to the above data. It returns "-50.3903 1.20143e14 -0.995084", which means the best fit decay constant is 50.39 and the value at time zero is 1.2e14. The correlation coefficient is -0.995084.