Sandbox

rem ************************************************************************************** rem least squares polynomial fitting:- def fn_polyfit(Order,Npoints,z)

local i local o% : o%=Order if o 6 then print "cannot have polynomial order of ";o : end

local N% : N%=Npoints local x, y, xn, xny : dim x(N%), y(N%), xn(N%), xny(N%)

local m : dim m(o%,o%) : rem matrix local v : dim v(o%) : rem vector

for i=1 to N% x(i-1)=z(i,0) : y(i-1)=z(i,1) next

rem all constants, for order 1 to 6, shown here for clarity:- xny = y : _y1 = sum(xny) xn = x : _x1 = sum(xn)

xny = xn * y : _x1y = sum(xny) xn = xn * x : _x2 = sum(xn)

xny = xn * y : _x2y = sum(xny) xn = xn * x : _x3 = sum(xn)

xny = xn * y : _x3y = sum(xny) xn = xn * x : _x4 = sum(xn)

xny = xn * y : _x4y = sum(xny) xn = xn * x : _x5 = sum(xn)

xny = xn * y : _x5y = sum(xny) xn = xn * x : _x6 = sum(xn)

xny = xn * y : _x6y = sum(xny) xn = xn * x : _x7 = sum(xn)

xn = xn * x : _x8 = sum(xn) xn = xn * x : _x9 = sum(xn) xn = xn * x : _x10 = sum(xn) xn = xn * x : _x11 = sum(xn) xn = xn * x : _x12 = sum(xn)

rem 1st order is straight line fit ******** if o%=1 then m = \ \ N%, _x1, \ \ _x1, _x2

v = _y1, _x1y

endif

rem 2nd order is quadratic fit ************ if o%=2 then m = \ \ N%, _x1, _x2, \ \ _x1, _x2, _x3, \ \ _x2, _x3, _x4

v = _y1, _x1y, _x2y

endif

rem 3rd order is cubic fit **************** if o%=3 then m = \ \ N%, _x1, _x2, _x3, \ \ _x1, _x2, _x3, _x4, \ \ _x2, _x3, _x4, _x5, \ \ _x3, _x4, _x5, _x6

v = _y1, _x1y, _x2y, _x3y

endif

rem 4th order ****************************** if o%=4 then m = \ \ N%, _x1, _x2, _x3, _x4, \ \ _x1, _x2, _x3, _x4, _x5, \ \ _x2, _x3, _x4, _x5, _x6, \ \ _x3, _x4, _x5, _x6, _x7, \ \ _x4, _x5, _x6, _x7, _x8

v=_y1, _x1y, _x2y, _x3y, _x4y

endif

rem 5th order ****************************** if o%=5 then m = \ \ N%, _x1, _x2, _x3, _x4, _x5, \ \ _x1, _x2, _x3, _x4, _x5, _x6, \ \ _x2, _x3, _x4, _x5, _x6, _x7, \ \ _x3, _x4, _x5, _x6, _x7, _x8, \ \ _x4, _x5, _x6, _x7, _x8, _x9, \ \ _x5, _x6, _x7, _x8, _x9, _x10

v= _y1, _x1y, _x2y, _x3y, _x4y, _x5y

endif

rem 6th order ****************************** if o%=6 then m = \ \ N%, _x1, _x2, _x3, _x4, _x5, _x6, \ \ _x1, _x2, _x3, _x4, _x5, _x6, _x7, \ \ _x2, _x3, _x4, _x5, _x6, _x7, _x8, \ \ _x3, _x4, _x5, _x6, _x7, _x8, _x9, \ \ _x4, _x5, _x6, _x7, _x8, _x9, _x10, \ \ _x5, _x6, _x7, _x8, _x9, _x10, _x11, \ \ _x6, _x7, _x8, _x9, _x10, _x11, _x12

v= _y1, _x1y, _x2y, _x3y, _x4y, _x5y, _x6y

endif

rem solve the set of simultaneous equations:- proc_invert(m) v=m.v =!^v rem return pointer to vector. (v is array containing coefficents. eg y = v(0)*x^0 + v(1)*x^1 + .... etc

rem