Example #1
0
        public static void TestNonlinearLeastSquareFit()
        {
            PZMath_multifit_fdfsolver_type T = new PZMath_multifit_fdfsolver_type();
            PZMath_multifit_fdfsolver s = new PZMath_multifit_fdfsolver();

            int N = 40;
            int status;
            int i, iter = 0;

            int n = N;
            int p = 3;

            PZMath_matrix covar = new PZMath_matrix(p, p);

            double[] y = new double[N];
            double[] sigma = new double[N];
            double h = 1e-2; // step-size
            TestNonlinearLeastSquareFitParam d = new TestNonlinearLeastSquareFitParam(n, p, y, sigma, h);
            PZMath_multifit_function_fdf f = new PZMath_multifit_function_fdf();

            double[] x_init = new double[3];
            x_init[0] = 1.0;
            x_init[1] = 0.0;
            x_init[2] = 0.0;
            PZMath_vector x = new PZMath_vector(x_init);

            f.f = new multifit_delegate_f(TestPZMathMultifit.expb_f);
            f.df = new multifit_delegate_df(TestPZMathMultifit.expb_df);
            f.fdf = new multifit_delegate_fdf(TestPZMathMultifit.expb_fdf);
            f.n = n;
            f.p = p;
            f.parms = d;

            ParkMillerNormal r = new ParkMillerNormal();
            // this is the data to be fiitted
            for (i = 0; i < n; i++)
            {
                double t = i;
                double tt = System.Math.Exp(-0.1 * t);
                d.y[i] = 1.0 + 5 * System.Math.Exp(-0.1 * t);// + r.NextVariate();
                d.sigma[i] = 0.1;
                System.Diagnostics.Debug.WriteLine("data: " + i + " " + d.y[i] + " " + d.sigma[i]);
            }

            T.Init("PZMath_multifit_fdfsolver_lmsder");
            s.Alloc(T, n, p);
            s.Init(f, x);

            s.PrintState(iter);

            do
            {
                iter++;
                status = s.Iterate();
                System.Diagnostics.Debug.WriteLine("status = " + status);

                //printf ("status = %s \n", gsl_strerror(status));

                s.PrintState(iter);

                if (status > 0)
                    break;

                status = s.TestDelta(s.dx, s.x, 1e-4, 1e-4);
            }
            while (status == PZMath_errno.PZMath_CONTINUE && iter < 500);

            s.Covar(0.0, covar);
            covar.DebugWriteLine();

            System.Console.WriteLine("A = " + s.FIT(0) + " +/- " + s.ERR(0, covar));
            System.Console.WriteLine("lambda = " + s.FIT(1) + " +/- " + s.ERR(1, covar));
            System.Console.WriteLine("b = " + s.FIT(2) + " +/- " + s.ERR(2, covar));

            double chi = s.Chi();
            System.Console.WriteLine("chisq/dof = " + (System.Math.Pow(chi, 2.0) / (double)(n - p)));
            System.Console.WriteLine("state = " + status);

            //printf ("status = %s \n", gsl_strerror(status));
        }
 public int Init(PZMath_multifit_function_fdf f, PZMath_vector x)
 {
     if (this.f.Size != f.n)
     {
         PZMath_errno.ERROR("function size does not match solver", PZMath_errno.PZMath_EBADLEN);
     }
     if (this.x.Size != x.Size)
     {
         PZMath_errno.ERROR("vector length does not match solver", PZMath_errno.PZMath_EBADLEN);
     }
     this.fdf = f;
     this.x = x;
     return (this.type.init) (this.state, this.fdf, this.x, this.f, this.J, this.dx);
 }