Robert,
> is there a package/code-snippet for fitting nonlinear functions to datasets?
The lbfgs function implements a numerical unconstrained minimization
algorithm. To use lbfgs to fit a function to data, you can let the
figure of merit be the sum of squares for some function with free
parameters. lbfgs differentiates the FOM wrt the free parameters
to get the gradient and then takes steps based on that. (Don't worry,
lbfgs is not gradient descent -- it is a quasi-Newton method.)
Here is an example using lbfgs to fit a function to some data.
By the way, I find that this works OK with Maxima + GCL (e.g.
default Windows installation) but with Maxima + Clisp I get an
error message about attempting to write on a closed stream.
I'll try to figure out the origin of that error.
load (lbfgs);
FOM : '((1/length(X))*sum((F(X[i]) - Y[i])^2, i, 1, length(X)));
X : [1, 2, 3, 4, 5];
Y : [0, 0.5, 1, 1.25, 1.5];
F(x) := A/(1 + exp(-B*(x - C)));
estimates : lbfgs (FOM, '[A, B, C], [1, 1, 1], 1e-4, [1, 3]);
plot2d ([F(x), [discrete, X, Y]], [x, -1, 6]), ''estimates;
In the final line, that is two single quotes (not one double quote).
lbfgs is packaged with Maxima 5.10.0 (not any earlier version).
Hope this helps
Robert Dodier