# [Maxima] Fun with lists

Lester Ingber ingber at ingber.com
Fri Mar 27 17:22:41 CDT 2009

Here are some quick scripts to build an interpolation/extrapolation
function to data points and also retain parameters to be fit by an
optimization routine.  Note that an algebraic language like Maxima is
very well suited to build such functions.

/* linear fit to data -- pick an N */
N:9\$
assume (t[1] > 0)\$
for i: 1 thru N-2 do assume (t[i+1] > t[i])\$
for j: 1 thru N do Line[j](x) := m[j] * x + b[j]\$
for k: 1 thru N-1 do Lcns[k]: Line[k+1](t[k]) - Line[k](t[k])\$
listCns: [Lcns[1]]\$
for k: 2 thru N-1 do listCns: append ([Lcns[k]], listCns)\$
listVar: [b[2]]\$
for k: 3 thru N do listVar: append ([b[k]], listVar)\$
linsolve(listCns, listVar)\$
for k: 1 thru N-1 do solb[k]: part (%, k, 2)\$
display2d: false\$
writefile ("outb")\$
for k: 1 thru N-1 do display (b[k] = solb[k])\$
closefile()\$
quit()\$

In case the generalization to other fitting curves is not obvious,
here is a cubic.  Of course, this is not limited to polynomials.

/* cubic fit to data -- pick an N */
N:9\$
assume (t[1] > 0)\$
for i: 1 thru N-2 do assume (t[i+1] > t[i])\$
for j: 1 thru N do Line[j](x) := m3[j] * x^3 + m2[j] * x^2 + m[j] * x + b[j]\$
for k: 1 thru N-1 do Lcns[k]: Line[k+1](t[k]) - Line[k](t[k])\$
listCns: [Lcns[1]]\$
for k: 2 thru N-1 do listCns: append ([Lcns[k]], listCns)\$
listVar: [b[2]]\$
for k: 3 thru N do listVar: append ([b[k]], listVar)\$
linsolve(listCns, listVar)\$
for k: 1 thru N-1 do solb[k]: part (%, k, 2)\$
display2d: false\$
writefile ("outb")\$
for k: 1 thru N-1 do display (b[k] = solb[k])\$
closefile()\$
quit()\$

Lester