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