gsl_fit
can be used to fit a several related datasets at the same time.Choose from these 7 fitting methods and algorithms :
MULTIDIM_MINIMIZAION CONJUGATE_FR CONJUGATE_PR VECTOR_BFGS STEEPEST_DESCENT LEAST_SQUARES LMSDR LMDR SIMULATED_ANNEAL
Definition in file test_multiple.c.
Go to the source code of this file.
Functions | |
void | load (gsl_fit *gft, char *filename) |
Load the data. | |
int | main (int argc, char *argv[]) |
Main function. | |
Variables | |
double | d [3][2] |
Use to set the relation between data sets. |
void load | ( | gsl_fit * | gft, | |
char * | filename | |||
) |
Load the data.
Definition at line 147 of file test_multiple.c.
00148 { 00149 int i, j; 00150 double d[3]; 00151 double *x, *y, *s; 00152 char c[120]; 00153 FILE *fp; 00154 00155 /*====================================== 00156 ** open the file for reading 00157 */ 00158 if (!(fp = fopen(filename, "r"))) 00159 { 00160 fprintf(stderr,"Can not open file.\n"); 00161 exit(EXIT_FAILURE); 00162 } 00163 00164 /*====================================== 00165 ** Count the number of records by using 00166 ** the sucessful conversion of three column data 00167 */ 00168 bzero(c, 120); j = 0; 00169 while(fgets(c, 119, fp) != NULL) 00170 { 00171 i = sscanf(c, "%lf %lf %lf", &d[0], &d[1], &d[2]); 00172 if (i == 3) j++; 00173 bzero(c,120); 00174 } 00175 00176 /*====================================== 00177 ** rewind the file pointer to the start of the file 00178 */ 00179 rewind(fp); 00180 00181 /*====================================== 00182 ** allocate the memory to hold the data 00183 */ 00184 x = (double *)malloc(j*sizeof(double)); 00185 y = (double *)malloc(j*sizeof(double)); 00186 s = (double *)malloc(j*sizeof(double)); 00187 00188 /*====================================== 00189 ** read the data into memory 00190 */ 00191 bzero(c, 120); j = 0; 00192 while(fgets(c, 119, fp) != NULL) 00193 { 00194 i = sscanf(c, "%lf %lf %lf", &x[j], &y[j], &s[j]); 00195 if (i == 3) j++; 00196 bzero(c,120); 00197 } 00198 00199 /*====================================== 00200 ** done with the file pointer 00201 */ 00202 fclose(fp); 00203 00204 /*====================================== 00205 ** Add the data set to the gsl container. 00206 */ 00207 gsl_fit_add_dataset(gft, x, y, s, j); 00208 00209 /*====================================== 00210 ** Free the used memory 00211 */ 00212 free(x); 00213 free(y); 00214 free(s); 00215 00216 /*======================================*/ 00217 return; 00218 }
int main | ( | int | argc, | |
char * | argv[] | |||
) |
main
function.
Definition at line 58 of file test_multiple.c.
00059 { 00060 double param[4], error[4]; 00061 unsigned int fixed[4]; 00062 gsl_fit *my_gft=NULL; 00063 00064 /*======================================================== 00065 ** Prototype for how this might work: 00066 */ 00067 00068 /*======================================================== 00069 ** 1. Allocate and pick method from the several types available 00070 */ 00071 //my_gft = gsl_fit_alloc(LEAST_SQUARES, LMSDR, 100, MULTIPLE); 00072 my_gft = gsl_fit_alloc(MULTIDIM_MINIMIZAION, CONJUGATE_FR, 500, MULTIPLE); 00073 //my_gft = gsl_fit_alloc(SIMULATED_ANNEAL, 0, 0, MULTIPLE); 00074 00075 /*======================================================== 00076 ** 2. Load the data. 00077 ** In this example the relationship bewteen the data sets are 00078 ** known beforehand. It can be summarized as: 00079 ** for data set i, multiply the height of peak j by d[i][j] 00080 ** 00081 ** Other examples might be: 00082 ** for data set i, add d[i][j] to the position of peak j 00083 ** for data set i, the width of peak j is divided by d[i][j] 00084 */ 00085 load(my_gft, "test_data/data0.txt"); 00086 load(my_gft, "test_data/data1.txt"); 00087 load(my_gft, "test_data/data2.txt"); 00088 00089 d[0][0] = 1.0; d[0][1] = -1.0; 00090 d[1][0] = 1.0; d[1][1] = 1.0; 00091 d[2][0] = -1.0; d[2][1] = 1.0; 00092 00093 /*======================================================== 00094 ** 4. Make an inital guess. 00095 */ 00096 /* The first function added is function 0 */ 00097 param[0] = 10.472; fixed[0] = 0; 00098 param[1] = 18.627; fixed[1] = 0; 00099 param[2] = 8.150; fixed[2] = 0; 00100 param[3] = 1.150; fixed[3] = 0; 00101 gsl_fit_add_model_multiple(my_gft, mpearson_area, param, fixed, 4); 00102 00103 /* The second function added is function 1 */ 00104 param[0] = 9.323; fixed[0] = 0; 00105 param[1] = 70.033; fixed[1] = 0; 00106 param[2] = 3.704; fixed[2] = 0; 00107 param[3] = 15.150; fixed[3] = 0; 00108 gsl_fit_add_model_multiple(my_gft, mpearson_area, param, fixed, 4); 00109 00110 /* The first function added is function 2 */ 00111 param[0] = 0.0012; fixed[0] = 0; 00112 param[1] = 1.1000; fixed[1] = 0; 00113 gsl_fit_add_model_multiple(my_gft, mline, param, fixed, 2); 00114 00115 /*========================================================*/ 00116 gsl_fit_print(my_gft, "test_data/start.txt"); 00117 00118 /*======================================================== 00119 ** 5. Do the fitting 00120 */ 00121 gsl_fit_do(my_gft); 00122 00123 /*========================================================*/ 00124 gsl_fit_print(my_gft, "test_data/end.txt"); 00125 00126 /*======================================================== 00127 ** 6. Get the results 00128 */ 00129 gsl_fit_get_result(my_gft, 0, param, error, 4); 00130 gsl_fit_get_result(my_gft, 1, param, error, 4); 00131 gsl_fit_get_result(my_gft, 2, param, error, 2); 00132 00133 /*======================================================== 00134 ** 7. Free the container 00135 */ 00136 gsl_fit_free(my_gft); 00137 00138 return EXIT_SUCCESS; 00139 }
double d[3][2] |
This variable is used to set the relationship between data sets. Some examples:
For data set i, multiply the height of peak j by d[i][j] For data set i, add d[i][j] to the position of peak j For data set i, the width of peak j is divided by d[i][j]
Definition at line 46 of file test_multiple.c.