forum.alglib.net
http://forum.alglib.net/

Least square fitting
http://forum.alglib.net/viewtopic.php?f=2&t=68
Page 1 of 1

Author:  JFT [ Tue Sep 28, 2010 6:32 pm ]
Post subject:  Least square fitting

Hey,

i want to fit Data from a laser scanner to a function like this: a*x^3+b*x^2+c*x^1+d .
(The laser scanner interface gives points(x/y).

I tried to change an example to test fitting data to the function:

Code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "Alglib/lsfit.h"

int main(int argc, char **argv) {
    int m;
    int n;
    int k;
    ap::real_1d_array y;
    ap::real_2d_array x;
    ap::real_1d_array c;
    lsfitreport rep;
    lsfitstate state;
    int info;
    double epsf;
    double epsx;
    int maxits;
    int i;
    int j;
    double a;
    double b;

    printf("Fitting Pointcloud from Laserscanner to ax^3+bx^2+cx+d\n");

    //
    // * without Hessian (gradient only)
    // * using alpha=1 as initial value
    // * using 1000 uniformly distributed points to fit to
    //
    // Notes:
    // * N - number of points
    // * M - dimension of space where points reside
    // * K - number of parameters being fitted
    //
    n = 1000;
    m = 1;
    k = 4;

    //
    // Prepare task matrix
    //
    y.setlength(n);
    x.setlength(n, m);
    c.setlength(k);
    for (i = 0; i <= n - 1; i++) {
        x(i, 0) = i;
        y(i) = 2 * i * i * i + i * i + 5 * i + 4;
    }
    c(0) = 1.0;
    c(1) = 1.0;
    c(2) = 3.0;
    c(3) = 4.0;
    epsf = 0.0;
    epsx = 0.0001;
    maxits = 0;

    //
    // Solve
    //
    lsfitnonlinearfg(x, y, c, n, m, k, true, state);
    lsfitnonlinearsetcond(state, epsf, epsx, maxits);
    while (lsfitnonlineariteration(state)) {

        //
        // F(x) =
        //
        state.f = state.c(0) * ap::sqr(state.x(0)) * state.x(0) + state.c(1)
                * ap::sqr(state.x(1)) + state.c(2) * state.x(2) + state.c(3);

        if (state.needfg ) {

            //
            // F(x)      = Exp(-alpha*x^2)
            // dF/dAlpha = (-x^2)*Exp(-alpha*x^2)
            //
//            state.f = state.c(0) * ap::sqr(state.x(0)) * state.x(0)
//                    + state.c(1) * ap::sqr(state.x(0)) + state.c(2)
//                    * state.x(0) + state.c(3);
            state.g(0) = 3 * state.c(0) * ap::sqr(state.x(0)) + 2 * state.c(1)
                    * state.x(0) + state.c(2);
            state.g(1)=state.c(1)*state.x(0);    //what should i do here?
            state.g(2)=state.c(2);
            state.g(3)=1.00;
        }

    }
    lsfitnonlinearresults(state, info, c, rep);
    printf("a:   %0.3lf\n", double(c(0)));
    printf("b:   %0.3lf\n", double(c(1)));
    printf("c:   %0.3lf\n", double(c(2)));
    printf("d:   %0.3lf\n", double(c(3)));
    printf("rms.err: %0.3lf\n", double(rep.rmserror));
    printf("max.err: %0.3lf\n", double(rep.maxerror));
    printf("Termination type: %0ld\n", long(info));
    printf("\n\n");
    return 0;
}


It doesn't work properly.
Maybe someone can help me with it.


Many thanks,
JFT

Author:  Sergey.Bochkanov [ Wed Sep 29, 2010 12:55 pm ]
Post subject:  Re: Least square fitting

Your inner loop should look like:

Code:
state.g(0) = ap::sqr(state.x(0)) * state.x(0);
state.g(1) = ap::sqr(state.x(0))
state.g(2) =  state.x(0);
state.g(3) = 1


You should calculate derivative of F(x,c) with respect to c, not to x.

And why don't you use lsfitlinear() functions? Your problem is linear with respect to c[], so you can solve it using linear least squares (although nonlinear leasts quares will work too).

Author:  Bart [ Sun Feb 02, 2014 4:12 am ]
Post subject:  Re: Least square fitting

It is possible to fit function such as

\alpha[0] + \alpha[1]*x[1]^{\beta[1]} + \alpha[1]*x[2]^{\beta[2]} + ...

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/