test_single.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 /*====================================================================*/
00035 double d[3][2]; /* Ignore this line */
00036 /*====================================================================*/
00037 void load(gsl_fit *gft, char *filename);
00038 
00039 /*====================================================================*/
00040 /*====================================================================*/
00041 /*====================================================================*/
00042 /*====================================================================*/
00047 int main(int argc, char *argv[])
00048 {
00049   double        param[4], error[4];
00050   unsigned int  fixed[4];
00051   gsl_fit *my_gft=NULL;
00052 
00053   /*========================================================
00054   **  Prototype for how this might work: 
00055   */
00056   
00057   /*========================================================
00058   **  1. Allocate and pick method from the several types available 
00059   */
00060   //my_gft = gsl_fit_alloc(LEAST_SQUARES, LMSDR, 100, SINGLE);
00061   my_gft = gsl_fit_alloc(MULTIDIM_MINIMIZAION, CONJUGATE_FR, 500, SINGLE);
00062   //my_gft = gsl_fit_alloc(SIMULATED_ANNEAL, 0, 0, SINGLE);
00063 
00064   /*========================================================
00065   **  2. Load the data.
00066   */
00067   load(my_gft, "test_data/data1.txt");
00068    
00069   /*========================================================
00070   **  4. Make an inital guess.
00071   */
00072   param[0] = 10.472;   fixed[0] =    0;
00073   param[1] = 20.627;   fixed[1] =    0;
00074   param[2] =  6.150;   fixed[2] =    0;
00075   param[3] =  1.150;   fixed[3] =    0;
00076   gsl_fit_add_model_single(my_gft, spearson_area, param, fixed, 4); 
00077 
00078   param[0] =  9.323;   fixed[0] =    0;
00079   param[1] = 70.033;   fixed[1] =    0;
00080   param[2] =  3.704;   fixed[2] =    0;
00081   param[3] = 15.150;   fixed[3] =    0;
00082   gsl_fit_add_model_single(my_gft, spearson_area, param, fixed, 4);
00083 
00084   param[0] =  0.0012;   fixed[0] =    0;
00085   param[1] =  1.1000;   fixed[1] =    0;
00086   gsl_fit_add_model_single(my_gft, sline, param, fixed, 2);
00087   
00088   /*========================================================*/
00089   gsl_fit_print(my_gft, "test_data/start.txt");
00090 
00091   /*========================================================
00092   ** 5. Do the fitting
00093   */
00094   gsl_fit_do(my_gft);
00095 
00096   /*========================================================*/
00097   gsl_fit_print(my_gft, "test_data/end.txt");
00098 
00099   /*========================================================
00100   **  6. Get the results
00101   */
00102   gsl_fit_get_result(my_gft, 0, param, error, 4);
00103   gsl_fit_get_result(my_gft, 1, param, error, 4);
00104   gsl_fit_get_result(my_gft, 2, param, error, 2);
00105 
00106   /*========================================================
00107   **  7. Free the container
00108   */
00109   gsl_fit_free(my_gft);
00110   
00111   return EXIT_SUCCESS;
00112 }
00113 /*====================================================================*/
00114 /*====================================================================*/
00115 /*====================================================================*/
00121 void load(gsl_fit *gft, char *filename)
00122 {
00123   int                i, j;
00124   double             d[3];
00125   double            *x, *y, *s;
00126   char               c[120];
00127   FILE              *fp;
00128 
00129   /*======================================  
00130   ** open the file for reading 
00131   */
00132   if (!(fp = fopen(filename, "r")))
00133   {
00134     fprintf(stderr,"Can not open file.\n");
00135     exit(EXIT_FAILURE);
00136   }
00137 
00138   /*======================================  
00139   ** Count the number of records by using
00140   ** the sucessful conversion of three column data
00141   */
00142   bzero(c, 120); j = 0;
00143   while(fgets(c, 119, fp) != NULL)
00144   {
00145     i = sscanf(c, "%lf %lf %lf", &d[0], &d[1], &d[2]);
00146     if (i == 3) j++;
00147     bzero(c,120);
00148   }
00149   
00150   /*======================================  
00151   ** rewind the file pointer to the start of the file 
00152   */
00153   rewind(fp);
00154   
00155   /*======================================  
00156   ** allocate the memory to hold the data  
00157   */
00158   x = (double *)malloc(j*sizeof(double));
00159   y = (double *)malloc(j*sizeof(double));
00160   s = (double *)malloc(j*sizeof(double));
00161   
00162   /*======================================  
00163   ** read the data into memory  
00164   */
00165   bzero(c, 120); j = 0;
00166   while(fgets(c, 119, fp) != NULL)
00167   {
00168     i = sscanf(c, "%lf %lf %lf", &x[j], &y[j], &s[j]);
00169     if (i == 3) j++;
00170     bzero(c,120);
00171   }
00172   
00173   /*======================================  
00174   ** done with the file pointer 
00175   */
00176   fclose(fp); 
00177   
00178   /*====================================== 
00179   ** Add the data set to the gsl container.
00180   */
00181   gsl_fit_add_dataset(gft, x, y, s, j);
00182   
00183   /*====================================== 
00184   ** Free the used memory
00185   */
00186   free(x);
00187   free(y);
00188   free(s);
00189   
00190   /*======================================*/
00191   return;
00192 }
00193 /*====================================================================*/
00194 /*====================================================================*/
00195 /*====================================================================*/
00196 #include "test_functions.c"

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