Пример #1
0
        /* Test harness routine, which contains test gaussian-peak data
         *
         * Example of fixing two parameter
         *
         * Commented example of how to put boundary constraints
         */
        private static int TestGaussFix()
        {
            double[] x = { -1.7237128E+00, 1.8712276E+00, -9.6608055E-01,
                           -2.8394297E-01, 1.3416969E+00,  1.3757038E+00,
                           -1.3703436E+00, 4.2581975E-02, -1.4970151E-01,
                           8.2065094E-01 };
            double[] y = { -4.4494256E-02, 8.7324673E-01, 7.4443483E-01,
                           4.7631559E+00,  1.7187297E-01, 1.1639182E-01,
                           1.5646480E+00,  5.2322268E+00, 4.2543168E+00,
                           6.2792623E-01 };
            double[] ey      = new double[10];
            double[] p       = { 0.0, 1.0, 0.0, 0.1 };  /* Initial conditions */
            double[] pactual = { 0.0, 4.70, 0.0, 0.5 }; /* Actual values used to make data*/
            //double[] perror = new double[4];			   /* Returned parameter errors */
            int i;
            int status;

            mp_result result = new mp_result(4);

            //result.xerror = perror;

            mp_par[] pars = new mp_par[4]/* Parameter constraints */
            {
                new mp_par {
                    isFixed = 1
                },                                           /* Fix parameters 0 and 2 */
                new mp_par(),
                new mp_par {
                    isFixed = 1
                },
                new mp_par()
            };

            /* How to put limits on a parameter.  In this case, parameter 3 is
             * limited to be between -0.3 and +0.2.
             * pars[3].limited[0] = 0;
             * pars[3].limited[1] = 1;
             * pars[3].limits[0] = -0.3;
             * pars[3].limits[1] = +0.2;
             */

            for (i = 0; i < 10; i++)
            {
                ey[i] = 0.5;
            }

            CustomUserVariable v = new CustomUserVariable {
                X = x, Y = y, Ey = ey
            };

            /* Call fitting function for 10 data points and 4 parameters (2
             * parameters fixed) */
            status = MPFit.Solve(ForwardModels.GaussFunc, 10, 4, p, pars, null, v, ref result);

            Console.Write("*** TestGaussFix status = {0}\n", status);
            PrintResult(p, pactual, result);

            return(0);
        }
Пример #2
0
        private static void TestGaussianWithDerivs()
        {
            double[] x =
            {
                -1.7237128E+00, 1.8712276E+00, -9.6608055E-01,
                -2.8394297E-01, 1.3416969E+00,  1.3757038E+00,
                -1.3703436E+00, 4.2581975E-02, -1.4970151E-01,
                8.2065094E-01
            };
            double[] y =
            {
                -4.4494256E-02, 8.7324673E-01, 7.4443483E-01,
                4.7631559E+00,  1.7187297E-01, 1.1639182E-01,
                1.5646480E+00,  5.2322268E+00, 4.2543168E+00,
                6.2792623E-01
            };
            double[] ey      = new double[10];
            double[] p       = { 0.0, 1.0, 1.0, 1.0 };  // Initial conditions
            double[] pactual = { 0.0, 4.70, 0.0, 0.5 }; // Actual values used to make data
            mp_par[] pars    =
            {
                new mp_par {
                    side = 3, deriv_debug = 0
                },
                new mp_par {
                    side = 1, deriv_debug = 0
                },
                new mp_par {
                    side = 3, deriv_debug = 0
                },
                new mp_par {
                    side = 1, deriv_debug = 0
                }
            };

            mp_result result = new mp_result(4);

            /* No constraints */

            for (uint i = 0; i < 10; i++)
            {
                ey[i] = 0.5;
            }

            CustomUserVariable v = new CustomUserVariable {
                X = x, Y = y, Ey = ey
            };

            /* Call fitting function for 10 data points and 4 parameters (no
             * parameters fixed) */

            var logger = new System.IO.StringWriter();
            int status = MPFit.Solve(ForwardModels.GaussianFuncAndDerivs, 10, 4, p, pars, null, v, ref result, logger);

            Console.Write("*** TestGaussFitWithDerivs status = {0}\n", status);
            PrintResult(p, pactual, result);
            Console.WriteLine(logger.ToString());
        }
Пример #3
0
        public static int GaussianFuncAndDerivs(double[] p, double[] dy, IList <double>[] dvec, object vars)
        {
            CustomUserVariable v = (CustomUserVariable)vars;

            double[] x, y, ey;
            double   sig2;

            x  = v.X;
            y  = v.Y;
            ey = v.Ey;

            sig2 = p[3] * p[3];
            //var dvec1 = new double[dy.Length];
            //var residuals = new double[dy.Length];

            for (int i = 0; i < dy.Length; i++)
            {
                double xc  = x[i] - p[2];
                double exp = Math.Exp(-0.5 * xc * xc / sig2);
                //residuals[i] = (y[i] - p[1] * exp - p[0]) / ey[i];
                dy[i] = (y[i] - p[1] * exp - p[0]) / ey[i];

                // NOTE: it would make sense to store the first 2 derivatives in vars since they don't change.
                if (dvec != null)
                {
                    if (dvec[0] != null)
                    {
                        dvec[0][i] = -1.0 / ey[i];
                    }
                    if (dvec[1] != null)
                    {
                        //dvec1[i] = -exp / ey[i];
                        dvec[1][i] = -exp / ey[i];
                    }
                    if (dvec[2] != null)
                    {
                        dvec[2][i] = -p[1] * xc * exp / (ey[i] * sig2);
                    }
                    if (dvec[3] != null)
                    {
                        dvec[3][i] = -p[1] * xc * xc * exp / (ey[i] * p[3] * sig2);
                    }
                }
            }

            // Array assignment rather than element-wise causes failure.
            //dy = residuals;

            // Array mismatch exception
            //if (dvec != null)
            //{
            //  if (dvec[1] != null)
            //  {
            //      dvec[1] = dvec1;
            //  }
            //}
            return(0);
        }
Пример #4
0
        /* Test harness routine, which contains test quadratic data;
         * Example of how to fix a parameter
         */
        private static int TestQuadFix()
        {
            double[] x = { -1.7237128E+00, 1.8712276E+00, -9.6608055E-01,
                           -2.8394297E-01, 1.3416969E+00,  1.3757038E+00,
                           -1.3703436E+00, 4.2581975E-02, -1.4970151E-01,
                           8.2065094E-01 };
            double[] y = { 2.3095947E+01, 2.6449392E+01, 1.0204468E+01,
                           5.40507,       1.5787588E+01, 1.6520903E+01,
                           1.5971818E+01, 4.7668524E+00, 4.9337711E+00,
                           8.7348375E+00 };

            double[] ey      = new double[10];
            double[] p       = { 1.0, 0.0, 1.0 };  /* Initial conditions */
            double[] pactual = { 4.7, 0.0, 6.2 };  /* Actual values used to make data */
            //double[] perror = new double[3];		       /* Returned parameter errors */
            int i;
            int status;

            mp_result result = new mp_result(3);

            //result.xerror = perror;

            mp_par[] pars = new mp_par[3] /* Parameter constraints */
            {
                new mp_par(),
                new mp_par()
                {
                    isFixed = 1
                },                                              /* Fix parameter 1 */
                new mp_par()
            };

            for (i = 0; i < 10; i++)
            {
                ey[i] = 0.2;
            }

            CustomUserVariable v = new CustomUserVariable()
            {
                X = x, Y = y, Ey = ey
            };

            /* Call fitting function for 10 data points and 3 parameters (1
             * parameter fixed) */
            status = MPFit.Solve(ForwardModels.QuadFunc, 10, 3, p, pars, null, v, ref result);

            Console.Write("*** TestQuadFix status = {0}\n", status);

            PrintResult(p, pactual, result);

            return(0);
        }
Пример #5
0
        /* Test harness routine, which contains test data, invokes mpfit() */
        private static int TestLinFit()
        {
            double[] x = { -1.7237128E+00, 1.8712276E+00, -9.6608055E-01,
                           -2.8394297E-01, 1.3416969E+00,  1.3757038E+00,
                           -1.3703436E+00, 4.2581975E-02, -1.4970151E-01,
                           8.2065094E-01 };
            double[] y = { 1.9000429E-01, 6.5807428E+00, 1.4582725E+00,
                           2.7270851E+00, 5.5969253E+00, 5.6249280E+00,
                           0.787615,      3.2599759E+00, 2.9771762E+00,
                           4.5936475E+00 };

            double[] ey = new double[10];
            /*        y = a + b * x */
            /*               a    b */
            double[] p       = { 1.0, 1.0 };     /* Parameter initial conditions */
            double[] pactual = { 3.20, 1.78 };   /* Actual values used to make data */
            //double[] perror = { 0.0, 0.0 };                   /* Returned parameter errors */
            int i;
            int status;

            mp_result result = new mp_result(2);

            //result.xerror = perror;

            for (i = 0; i < 10; i++)
            {
                ey[i] = 0.07; /* Data errors */
            }

            CustomUserVariable v = new CustomUserVariable();

            v.X  = x;
            v.Y  = y;
            v.Ey = ey;

            /* Call fitting function for 10 data points and 2 parameters */
            status = MPFit.Solve(ForwardModels.LinFunc, 10, 2, p, null, null, v, ref result);

            Console.Write("*** TestLinFit status = {0}\n", status);
            PrintResult(p, pactual, result);

            return(0);
        }
Пример #6
0
        /*
         * quadratic fit function
         *
         * m - number of data points
         * n - number of parameters (2)
         * p - array of fit parameters
         * dy - array of residuals to be returned
         * CustomUserVariable - private data (struct vars_struct *)
         *
         * RETURNS: error code (0 = success)
         */
        public static int QuadFunc(double[] p, double[] dy, IList <double>[] dvec, object vars)
        {
            int i;

            double[] x, y, ey;

            CustomUserVariable v = (CustomUserVariable)vars;

            x  = v.X;
            y  = v.Y;
            ey = v.Ey;

            /* Console.Write ("QuadFunc %f %f %f\n", p[0], p[1], p[2]); */

            for (i = 0; i < dy.Length; i++)
            {
                dy[i] = (y[i] - p[0] - p[1] * x[i] - p[2] * x[i] * x[i]) / ey[i];
            }

            return(0);
        }
Пример #7
0
        /*
         * linear fit function
         *
         * m - number of data points
         * n - number of parameters (2)
         * p - array of fit parameters
         * dy - array of residuals to be returned
         * CustomUserVariable - private data (struct vars_struct *)
         *
         * RETURNS: error code (0 = success)
         */
        public static int LinFunc(double[] p, double[] dy, IList <double>[] dvec, object vars)
        {
            int i;

            double[] x, y, ey;
            double   f;

            CustomUserVariable v = (CustomUserVariable)vars;

            x  = v.X;
            y  = v.Y;
            ey = v.Ey;

            for (i = 0; i < dy.Length; i++)
            {
                f     = p[0] + p[1] * x[i]; /* Linear fit function; note f = a + b*x */
                dy[i] = (y[i] - f) / ey[i];
            }

            return(0);
        }
Пример #8
0
        /*
         * gaussian fit function
         *
         * m - number of data points
         * n - number of parameters (4)
         * p - array of fit parameters
         *     p[0] = constant offset
         *     p[1] = peak y value
         *     p[2] = x centroid position
         *     p[3] = gaussian sigma width
         * dy - array of residuals to be returned
         * CustomUserVariable - private data (struct vars_struct *)
         *
         * RETURNS: error code (0 = success)
         */
        public static int GaussFunc(double[] p, double[] dy, IList <double>[] dvec, object vars)
        {
            int i;
            CustomUserVariable v = (CustomUserVariable)vars;

            double[] x, y, ey;
            double   xc, sig2;

            x  = v.X;
            y  = v.Y;
            ey = v.Ey;

            sig2 = p[3] * p[3];

            for (i = 0; i < dy.Length; i++)
            {
                xc    = x[i] - p[2];
                dy[i] = (y[i] - p[1] * Math.Exp(-0.5 * xc * xc / sig2) - p[0]) / ey[i];
            }

            return(0);
        }