/* * double poisson 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 DoublePossFunc(double[] p, double[] dy, IList <double>[] dvec, object vars) { int i; CustomUserVariable v = (CustomUserVariable)vars; double[] x, y, ey; double f; x = v.X; y = v.Y; ey = v.Ey; //First I want the tallest point and then for (i = 0; i < dy.Length; i++) { //Value from first poisson Note that the fact will only work for integers double lam1 = p[0]; double y1 = Math.Pow(lam1, x[i]) * Math.Exp(-1.0 * lam1) / (factEval(x[i])); double lam2 = p[1]; double y2 = Math.Pow(lam2, x[i]) * Math.Exp(-1.0 * lam2) / (factEval(x[i])); //Value from second poisson f = y1 + y2 + p[2]; dy[i] = (y[i] - f) / ey[i]; } return(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); }
/* Test harness routine, which contains test gaussian-peak data */ public static double[] TestGaussFit(double[] xinc, double[] yinc, double[] p) { double[] x = xinc; double[] y = yinc; double[] ey = new double[yinc.Length]; //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*/ //double[] perror = new double[4]; /* Returned parameter errors */ mp_par[] pars = new mp_par[4] /* Parameter constraints */ { new mp_par(), new mp_par(), new mp_par(), new mp_par() }; int i; int status; mp_result result = new mp_result(4); //result.xerror = perror; /* No constraints */ for (i = 0; i < yinc.Length; i++) { ey[i] = 0.02; } for (i = 0; i < p.Length; i++) { Console.Write("P" + i + " : " + p[i]); } CustomUserVariable v = new CustomUserVariable { X = x, Y = y, Ey = ey }; /* Call fitting function for 10 data points and 4 parameters (no * parameters fixed) */ status = MPFit.Solve(ForwardModels.GaussFunc, yinc.Length, 4, p, pars, null, v, ref result); Console.Write("*** TestGaussFit status = {0}\n", status); PrintResult(p, pactual, result); double[] retval = { p[0], p[1], p[2], p[3], result.xerror[0], result.xerror[1], result.xerror[2], result.xerror[3] }; return(retval); }
/* * 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); }
/* * 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 */ dy[i] = (y[i] - f) / ey[i]; } return(0); }
/* * gaussian fit function * * m - number of data points * n - number of parameters (4) * 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 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); }