-
Notifications
You must be signed in to change notification settings - Fork 0
/
testmpfit.c
416 lines (350 loc) · 11.3 KB
/
testmpfit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
* MINPACK-1 Least Squares Fitting Library
*
* Test routines
*
* These test routines provide examples for users to familiarize
* themselves with the mpfit library. They also provide a baseline
* test data set for users to be sure that the library is functioning
* properly on their platform.
*
* By default, testmpfit is built by the distribution Makefile.
*
* To test the function of the mpfit library,
* 1. Build testmpfit ("make testmpfit")
* 2. Run testmpfit ("./testmpfit")
* 3. Compare results of your run with the distributed file testmpfit.log
*
* This file contains several test user functions:
* 1. linfunc() linear fit function, y = f(x) = a - b*x
* - Driver is testlinfit()
* 2. quadfunc() quadratic polynomial function, y = f(x) = a + b*x + c*x^2
* - Driver is testquadfit() - all parameters free
* - Driver is testquadfix() - linear parameter fixed
* 3. gaussfunc() gaussian peak
* - Driver is testgaussfit() - all parameters free
* - Driver is testgaussfix() - constant & centroid fixed
* (this routine demonstrates in comments how to impose parameter limits)
* 4. main() routine calls all five driver functions
*
* Copyright (C) 2003,2006,2009,2010, Craig Markwardt
*
*/
/* Test routines for mpfit library
$Id$
*/
#include "mpfit.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/* This is the private data structure which contains the data points
and their uncertainties */
struct vars_struct {
double *x;
double *y;
double *ey;
};
/* Simple routine to print the fit results */
void printresult(double *x, double *xact, mp_result *result)
{
int i;
if ((x == 0) || (result == 0)) return;
printf(" CHI-SQUARE = %f (%d DOF)\n",
result->bestnorm, result->nfunc-result->nfree);
printf(" NPAR = %d\n", result->npar);
printf(" NFREE = %d\n", result->nfree);
printf(" NPEGGED = %d\n", result->npegged);
printf(" NITER = %d\n", result->niter);
printf(" NFEV = %d\n", result->nfev);
printf("\n");
if (xact) {
for (i=0; i<result->npar; i++) {
printf(" P[%d] = %f +/- %f (ACTUAL %f)\n",
i, x[i], result->xerror[i], xact[i]);
}
} else {
for (i=0; i<result->npar; i++) {
printf(" P[%d] = %f +/- %f\n",
i, x[i], result->xerror[i]);
}
}
}
/*
* 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
* vars - private data (struct vars_struct *)
*
* RETURNS: error code (0 = success)
*/
int linfunc(int m, int n, double *p, double *dy, double *dvec, void *vars)
{
int i;
struct vars_struct *v = (struct vars_struct *) vars;
double *x, *y, *ey, f;
x = v->x;
y = v->y;
ey = v->ey;
for (i=0; i<m; 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;
}
/* Test harness routine, which contains test data, invokes mpfit() */
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[10];
/* y = a - b*x */
/* a b */
double p[2] = {1.0, 1.0}; /* Parameter initial conditions */
double pactual[2] = {3.20, 1.78}; /* Actual values used to make data */
double perror[2]; /* Returned parameter errors */
int i;
struct vars_struct v;
int status;
mp_result result;
memset(&result,0,sizeof(result)); /* Zero results structure */
result.xerror = perror;
for (i=0; i<10; i++) ey[i] = 0.07; /* Data errors */
v.x = x;
v.y = y;
v.ey = ey;
/* Call fitting function for 10 data points and 2 parameters */
status = mpfit(linfunc, 10, 2, p, 0, 0, (void *) &v, &result);
printf("*** testlinfit status = %d\n", status);
printresult(p, pactual, &result);
return 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
* vars - private data (struct vars_struct *)
*
* RETURNS: error code (0 = success)
*/
int quadfunc(int m, int n, double *p, double *dy, double *dvec, void *vars)
{
int i;
struct vars_struct *v = (struct vars_struct *) vars;
double *x, *y, *ey;
x = v->x;
y = v->y;
ey = v->ey;
/* printf ("quadfunc %f %f %f\n", p[0], p[1], p[2]); */
for (i=0; i<m; i++) {
dy[i] = (y[i] - p[0] - p[1]*x[i] - p[2]*x[i]*x[i])/ey[i];
}
return 0;
}
/* Test harness routine, which contains test quadratic data, invokes
mpfit() */
int testquadfit()
{
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[10];
double p[] = {1.0, 1.0, 1.0}; /* Initial conditions */
double pactual[] = {4.7, 0.0, 6.2}; /* Actual values used to make data */
double perror[3]; /* Returned parameter errors */
int i;
struct vars_struct v;
int status;
mp_result result;
memset(&result,0,sizeof(result)); /* Zero results structure */
result.xerror = perror;
for (i=0; i<10; i++) ey[i] = 0.2; /* Data errors */
v.x = x;
v.y = y;
v.ey = ey;
/* Call fitting function for 10 data points and 3 parameters */
status = mpfit(quadfunc, 10, 3, p, 0, 0, (void *) &v, &result);
printf("*** testquadfit status = %d\n", status);
printresult(p, pactual, &result);
return 0;
}
/* Test harness routine, which contains test quadratic data;
Example of how to fix a parameter
*/
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[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[3]; /* Returned parameter errors */
mp_par pars[3]; /* Parameter constraints */
int i;
struct vars_struct v;
int status;
mp_result result;
memset(&result,0,sizeof(result)); /* Zero results structure */
result.xerror = perror;
memset(pars, 0, sizeof(pars)); /* Initialize constraint structure */
pars[1].fixed = 1; /* Fix parameter 1 */
for (i=0; i<10; i++) ey[i] = 0.2;
v.x = x;
v.y = y;
v.ey = ey;
/* Call fitting function for 10 data points and 3 parameters (1
parameter fixed) */
status = mpfit(quadfunc, 10, 3, p, pars, 0, (void *) &v, &result);
printf("*** testquadfix status = %d\n", status);
printresult(p, pactual, &result);
return 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
* vars - private data (struct vars_struct *)
*
* RETURNS: error code (0 = success)
*/
int gaussfunc(int m, int n, double *p, double *dy, double *dvec, void *vars)
{
int i;
struct vars_struct *v = (struct vars_struct *) 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<m; i++) {
xc = x[i]-p[2];
dy[i] = (y[i] - p[1]*exp(-0.5*xc*xc/sig2) - p[0])/ey[i];
}
return 0;
}
/* Test harness routine, which contains test gaussian-peak data */
int testgaussfit()
{
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[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*/
double perror[4]; /* Returned parameter errors */
mp_par pars[4]; /* Parameter constraints */
int i;
struct vars_struct v;
int status;
mp_result result;
memset(&result,0,sizeof(result)); /* Zero results structure */
result.xerror = perror;
memset(pars,0,sizeof(pars)); /* Initialize constraint structure */
/* No constraints */
for (i=0; i<10; i++) ey[i] = 0.5;
v.x = x;
v.y = y;
v.ey = ey;
/* Call fitting function for 10 data points and 4 parameters (no
parameters fixed) */
status = mpfit(gaussfunc, 10, 4, p, pars, 0, (void *) &v, &result);
printf("*** testgaussfit status = %d\n", status);
printresult(p, pactual, &result);
return 0;
}
/* Test harness routine, which contains test gaussian-peak data
Example of fixing two parameter
Commented example of how to put boundary constraints
*/
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[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[4]; /* Returned parameter errors */
mp_par pars[4]; /* Parameter constraints */
int i;
struct vars_struct v;
int status;
mp_result result;
memset(&result,0,sizeof(result)); /* Zero results structure */
result.xerror = perror;
memset(pars,0,sizeof(pars)); /* Initialize constraint structure */
pars[0].fixed = 1; /* Fix parameters 0 and 2 */
pars[2].fixed = 1;
/* 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;
v.x = x;
v.y = y;
v.ey = ey;
/* Call fitting function for 10 data points and 4 parameters (2
parameters fixed) */
status = mpfit(gaussfunc, 10, 4, p, pars, 0, (void *) &v, &result);
printf("*** testgaussfix status = %d\n", status);
printresult(p, pactual, &result);
return 0;
}
/* Main function which drives the whole thing */
int main(int argc, char *argv[])
{
int i;
int niter = 1;
for (i=0; i<niter; i++) {
testlinfit();
testquadfit();
testquadfix();
testgaussfit();
testgaussfix();
}
exit(0);
}