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. 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?

```
#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);
}
```