lua-matrix/tests/test_fit.lua
David Manura fe6837f6dc import version 0.2.9, 2008-08-26
- decoupled symbol class from matrix class:
	-   matrix.replace has new semantics, applying a function to each element.
	      For old behavior: mtx = mtx:replace(matrix.symbol.makereplacer(...))
	-   replaced mtx:gsub(a,b) with mtx = mtx:replace(symbol.gsub,a,b)
	-   replaced matrix.tosymbol(mtx) with mtx = mtx:replace(symbol)
	- eliminated dependency on complex:
	-   replaced matrix.tocomplex(mtx) with mtx = mtx:replace(complex)
	-   replaced matrix.conjugate(mtx) with mtx = mtx:replace(complex.conjugate)
	-   mulnum and divnum no longer can take num of type string
	-   complex table no longer returned on module load
	- renamed remcomplex to elementstostrings and changed it to return new
	    matrix rather than doing in-place modification
	- fixed matrix.numround (numround variable mispelled).
	    Reported by Goeff Richards.
2010-09-22 21:41:53 -04:00

26 lines
774 B
Lua

-- require fit
package.path = "samples/?.lua;" .. package.path
local fit = require "fit"
-- Fit a straight line
-- x(i) = 2 | 3 | 4 | 5
-- y(i) = 5 | 9 | 15 | 21
-- model = y = a + b * x
-- r(i) = y(i) - ( a + b * x(i) )
local a,b = fit.linear( { 2,3, 4, 5 }, { 5,9,15,21 } )
assert(math.abs(a - -6.4) < 0.001)
assert(math.abs(b - 5.4) < 0.001)
-- Fit a parabola
local a, b, c = fit.parabola( { 0,1,2,4,6 }, { 3,1,0,1,4 } )
assert(math.abs(a - 2.8251599147122) < 0.001)
assert(math.abs(b - -2.0490405117271) < 0.001)
assert(math.abs(c - 0.3773987206823) < 0.001)
-- Fit exponential
local a, b = fit.exponential( {1, 2, 3, 4, 5}, {1,3.1,5.6,9.1,12.9} )
assert(math.abs(a - 1.0077958966968) < 0.001)
assert(math.abs(b - 1.5834684450364) < 0.001)
print 'PASSED'