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

117 lines
2.4 KiB
Lua

--///////////////////--
--// Curve Fitting //--
--///////////////////--
-- v 0.2
-- Lua 5.1 compatible
-- little add-on to the matrix module, to show some curve fitting
-- http://luamatrix.luaforge.net
-- Licensed under the same terms as Lua itself.
-- requires matrix module
local matrix = require "matrix"
-- The Fit Table
local fit = {}
-- Note all these Algos use the Gauss-Jordan Method to caculate equation systems
-- function to get the results
local function getresults( mtx )
assert( #mtx+1 == #mtx[1], "Cannot calculate Results" )
mtx:dogauss()
-- tresults
local cols = #mtx[1]
local tres = {}
for i = 1,#mtx do
tres[i] = mtx[i][cols]
end
return unpack( tres )
end
-- fit.linear ( x_values, y_values )
-- fit a straight line
-- model ( y = a + b * x )
-- returns a, b
function fit.linear( x_values,y_values )
-- x_values = { x1,x2,x3,...,xn }
-- y_values = { y1,y2,y3,...,yn }
-- values for A matrix
local a_vals = {}
-- values for Y vector
local y_vals = {}
for i,v in ipairs( x_values ) do
a_vals[i] = { 1, v }
y_vals[i] = { y_values[i] }
end
-- create both Matrixes
local A = matrix:new( a_vals )
local Y = matrix:new( y_vals )
local ATA = matrix.mul( matrix.transpose(A), A )
local ATY = matrix.mul( matrix.transpose(A), Y )
local ATAATY = matrix.concath(ATA,ATY)
return getresults( ATAATY )
end
-- fit.parabola ( x_values, y_values )
-- Fit a parabola
-- model ( y = a + b * x + c * x² )
-- returns a, b, c
function fit.parabola( x_values,y_values )
-- x_values = { x1,x2,x3,...,xn }
-- y_values = { y1,y2,y3,...,yn }
-- values for A matrix
local a_vals = {}
-- values for Y vector
local y_vals = {}
for i,v in ipairs( x_values ) do
a_vals[i] = { 1, v, v*v }
y_vals[i] = { y_values[i] }
end
-- create both Matrixes
local A = matrix:new( a_vals )
local Y = matrix:new( y_vals )
local ATA = matrix.mul( matrix.transpose(A), A )
local ATY = matrix.mul( matrix.transpose(A), Y )
local ATAATY = matrix.concath(ATA,ATY)
return getresults( ATAATY )
end
-- fit.exponential ( x_values, y_values )
-- Fit exponential
-- model ( y = a * x^b )
-- returns a, b
function fit.exponential( x_values,y_values )
-- convert to linear problem
-- ln(y) = ln(a) + b * ln(x)
for i,v in ipairs( x_values ) do
x_values[i] = math.log( v )
y_values[i] = math.log( y_values[i] )
end
local a,b = fit.linear( x_values,y_values )
return math.exp(a), b
end
return fit
--///////////////--
--// chillcode //--
--///////////////--