test_multiple.c

Go to the documentation of this file.
00001 /*====================================================================*/
00023 /*====================================================================*/
00024 #include <stdlib.h>
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include <strings.h>
00028 #include <limits.h>
00029 #include <math.h>
00030 #include "../gsl_fit.h"
00031 
00032 /*====================================================================*/
00033 #include "test_functions.h"
00034 /*====================================================================*/
00046 double d[3][2];
00047 /*====================================================================*/
00048 void load(gsl_fit *gft, char *filename);
00049 
00050 /*====================================================================*/
00051 /*====================================================================*/
00052 /*====================================================================*/
00053 /*====================================================================*/
00058 int main(int argc, char *argv[])
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 }
00140 /*====================================================================*/
00141 /*====================================================================*/
00142 /*====================================================================*/
00147 void load(gsl_fit *gft, char *filename)
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 }
00219 /*====================================================================*/
00220 /*====================================================================*/
00221 /*====================================================================*/
00222 #include "test_functions.c"

Generated on Fri Jan 19 14:54:26 2007 for gsl_fit by  doxygen 1.4.7