Varying parameters in a cosine fit and calculating chi squared for each set

This is a discussion on Varying parameters in a cosine fit and calculating chi squared for each set within the C Programming forums, part of the General Programming Boards category; Hi There. I'm trying to fit a function Acos(wt + theta) to my data that I have binned and need ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    20

    Varying parameters in a cosine fit and calculating chi squared for each set

    Hi There. I'm trying to fit a function Acos(wt + theta) to my data that I have binned and need to find the best values for the variable parameters A, w and theta so that the cos function fits to my data. I then evaluate chi squared for my data with this fit to assess the fit. However I've tried making a loop for this but it crashes every time I run it. I'm not sure weather this is because my programme uses a ridiculous amount of memory as I try each combination or I'm doing something wrong with my code? I am pretty new to programming so I haven't done this before so have no idea if there is a more efficient method of doing it?
    Code:
    #include <stdio.h>
    #include<math.h>
    #define N_HITS 1800
    #define N_TIME_BINS 78
       int main()
         {
         int i, time_bins[N_TIME_BINS],j,k;
    	 float event,time_error[N_TIME_BINS];
         float hit[N_HITS],value;
         FILE *fp, *fw, *fy;
         fp = fopen("damadata.txt" , "r");
         fw = fopen("bindata.txt", "w");
    	 fy = fopen("chidata.txt", "w");
                
     for (i=0; i<N_HITS ; i++)  /* enter loop to set array elements to zero*/
         {
           hit[i] = 0;
         }
     for (i=0; i<N_TIME_BINS ; i++)
         {
           time_bins[i] = 0;
         }
                
     if (fp != NULL)
         {                                 
            for(i=0; i<N_HITS; i++)                      /*Enter loop to read all lines of data into hit array*/
              {
              fscanf(fp,"%f\n", &event);                 /*Each line is read into a seperate array element*/
              hit[i]=event; 
              }                                 
         for (i = 0; i < N_HITS; i++)                  /*Now cycle through array containing data elements*/
             {                                          /*Open Loop 1*/                          
                for (j = 0; j < N_TIME_BINS; j++)
                {
                   value = hit[i];                       /*Value now equal to element i of array*/                     /*enter loop to bin data into 77 bins of group of 60*/
                if (value >= (j*60) && value < ((j+1)*60))    /*Tests if the value lies within a particular boundary*/
                    {
                    time_bins[j] = (time_bins[j] + 1);                       /*If it is add 1 element into bin j*/
                    }                                  
                else
                    { 
                    time_bins[j] = (time_bins[j]);                       /*If not element j remains the same*/
                    }
               }                                             /*Close Loop 1*/
            }                                             /*Close Loop 2*/
        for (j = 0; j <N_TIME_BINS; j++)                       /*Loop to print the number of data points in each bin*/
              {
    		  k=time_bins[j];
              time_error[j]=sqrt(k);
              fprintf(fw, "%d\t%d\t%f\n" , (j*60), k, time_error[j]);       /*Prints the bin number and the number of elements in this bin*/
    	      }
       }
       else {
             printf("Error: The file you specified could not be found!");
            }
    	int theta;
    	double w,a,chi_squ_tot,chi_squ_end[10000000],chi,chi_squ,ideal[N_TIME_BINS];
    	j=0;
    	chi_squ_tot=0;
    	for (a=7; a<14; a=a+0.1){                                        /* Varies my 'a' variable through set of sensible values */
    		for(w=0.002;w<0.001; w=w+0.005){                             /* Varies my 'w' variable through set of sensible values */
    			for(theta=150; theta<250; theta=theta+1){                 /* Varies my 'a' variable through set of sensible values */
                   for(i=0; i<N_TIME_BINS; i++){
    					ideal[i]=0;                                       
                       ideal[i]=a*cos((w*(i*60))+theta)+11.11;           /*Calcualtes the flux at t given this set of variables*/
    				   chi= (time_bins[i]-ideal[i])/time_error[i];       /*Set of setps to calculate chi squared to evaluate fit*/
    					chi_squ=chi*chi; 
    					chi_squ_tot=chi_squ_tot + chi_squ;
    
    				}
    				chi_squ_end[j]=chi_squ_tot;                          /*Passes final value of chi squared to an external variable*/
    				j=j+1;
    				chi_squ_tot=0;
    			}
    		}
    	}
    	for (j=0; j<10000000; j++){
    	fprintf(fy, "%d\t%f\n" , j,chi_squ_end[j] );                     /*Prints all values of chi squared so can see which produces best fit*/
    		}
       
              fclose(fp);
              fclose(fw);
    	      fclose(fy);
         return(0);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Well given that your chi_squ_end array is close to 80MB in size, and the default stack allocation is between 1 and 8MB, then yeah - it's gonna crash.

    Try something like
    static double chi_squ_end[10000000];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    20
    I've edited the programme to now read

    Code:
    #include <stdio.h>
    #include<math.h>
    #define N_HITS 1800
    #define N_TIME_BINS 78
       int main()
         {
         int i, time_bins[N_TIME_BINS],j,k;
    	 float event,time_error[N_TIME_BINS];
         float hit[N_HITS],value;
         FILE *fp, *fw, *fy;
         fp = fopen("damadata.txt" , "r");
         fw = fopen("bindata.txt", "w");
    	 fy = fopen("chidata.txt", "w");
                
     for (i=0; i<N_HITS ; i++)  /* enter loop to set array elements to zero*/
         {
           hit[i] = 0;
         }
     for (i=0; i<N_TIME_BINS ; i++)
         {
           time_bins[i] = 0;
         }
     if (fy == NULL) {
             printf("Error: The file you specified could not be found!");
            } 
     if (fp != NULL)
         {                                 
            for(i=0; i<N_HITS; i++)                      /*Enter loop to read all lines of data into hit array*/
              {
              fscanf(fp,"%f\n", &event);                 /*Each line is read into a seperate array element*/
              hit[i]=event; 
              }                                 
         for (i = 0; i < N_HITS; i++)                  /*Now cycle through array containing data elements*/
             {                                          /*Open Loop 1*/                          
                for (j = 0; j < N_TIME_BINS; j++)
                {
                   value = hit[i];                       /*Value now equal to element i of array*/                     /*enter loop to bin data into 77 bins of group of 60*/
                if (value >= (j*60) && value < ((j+1)*60))    /*Tests if the value lies within a particular boundary*/
                    {
                    time_bins[j] = (time_bins[j] + 1);                       /*If it is add 1 element into bin j*/
                    }                                  
                else
                    { 
                    time_bins[j] = (time_bins[j]);                       /*If not element j remains the same*/
                    }
               }                                             /*Close Loop 1*/
            }                                             /*Close Loop 2*/
        for (j = 0; j <N_TIME_BINS; j++)                       /*Loop to print the number of data points in each bin*/
              {
    		  k=time_bins[j];
              time_error[j]=sqrt(k);
              fprintf(fw, "%d\t%d\t%f\n" , (j*60), k, time_error[j]);       /*Prints the bin number and the number of elements in this bin*/
    	      }
       }
       else {
             printf("Error: The file you specified could not be found!");
            }
    	int theta;
    	double w,a,chi_squ_tot,chi,chi_squ,ideal;
    	j=0;
    	chi_squ_tot=0;
    	for (a=7; a<14; a=a+0.1){                                        /* Varies my 'a' variable through set of sensible values */
    		for(w=0.002;w<0.001; w=w+0.005){                             /* Varies my 'w' variable through set of sensible values */
    			for(theta=150; theta<250; theta=theta+1){                 /* Varies my 'a' variable through set of sensible values */
                   for(i=0; i<N_TIME_BINS; i++){                                      
                       ideal=a*cos((w*(i*60))+theta)+11.11;           /*Calcualtes the flux at t given this set of variables*/
    				   chi= (time_bins[i]-ideal)/time_error[i];       /*Set of setps to calculate chi squared to evaluate fit*/
    					chi_squ=chi*chi; 
    					chi_squ_tot=chi_squ_tot + chi_squ;
    
    				}
    				fprintf(fy, "%f\t%f\t%d\t%f\n" , a, w, theta, chi_squ_tot );
    				chi_squ_tot=0;
    			}
    		}
    	}
       
              fclose(fp);
              fclose(fw);
    	      fclose(fy);
         return(0);
    }
    So I've got rid of the huge array, but now I am getting no output written to my chi.txt file when I should be getting the values of a, w, theta and chi squared for each combination and I have no idea why!?!?

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    63
    Did you even try to debug this yourself before posting? Do your program even reach the line where you write your output to the file?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sqareroot and squared
    By farligefinn in forum C++ Programming
    Replies: 17
    Last Post: 08-03-2006, 08:12 PM
  2. Replies: 6
    Last Post: 01-08-2006, 01:49 PM
  3. Reference parameters and calculating change
    By Cstudent2121 in forum C Programming
    Replies: 6
    Last Post: 11-04-2005, 02:19 PM
  4. The sum of squared digits
    By wiz23 in forum C++ Programming
    Replies: 4
    Last Post: 04-10-2005, 09:05 AM
  5. Calculating Sin/Cosine
    By Josh in forum C Programming
    Replies: 2
    Last Post: 09-11-2001, 09:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21