From 7b7d0182d316d4ee2ec664b28d7da23e0210f3c3 Mon Sep 17 00:00:00 2001 From: tlatorre Date: Fri, 31 Aug 2018 11:51:42 -0500 Subject: fit in a do while loop until the fit converges to the same likelihood value --- src/fit.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/fit.c b/src/fit.c index 5733ee8..a8af643 100644 --- a/src/fit.c +++ b/src/fit.c @@ -18,11 +18,12 @@ #define EV_RECORD 0x45562020 // 'EV ' (as written to ZDAB file) +static size_t iter; + double nll(unsigned int n, const double *x, double *grad, void *params) { double T, theta, phi, t0; double pos[3], dir[3]; - static size_t iter; double fval; double z1[1], z2[1]; struct timeval tv_start, tv_stop; @@ -69,7 +70,7 @@ double nll(unsigned int n, const double *x, double *grad, void *params) int fit_event(event *ev, double *xopt, double *fmin) { - double x[9], ss[9], lb[9], ub[9]; + double x[9], ss[9], lb[9], ub[9], fval; int rv; nlopt_opt opt = nlopt_create(NLOPT_LN_BOBYQA, 9); @@ -121,7 +122,13 @@ int fit_event(event *ev, double *xopt, double *fmin) nlopt_set_initial_step(opt, ss); - rv = nlopt_optimize(opt,x,fmin); + iter = 0; + rv = nlopt_optimize(opt,x,&fval); + + do { + *fmin = fval; + rv = nlopt_optimize(opt,x,&fval); + } while (fabs(fval-*fmin) > 1e-9); memcpy(xopt,x,sizeof(x)); -- cgit