fork(1) download
  1.  
  2. #include <stdio.h>
  3. #include <math.h>
  4. #define g 9.8
  5. #define ndat 20
  6. #define precision 0.5
  7.  
  8. float func(float t, float v0, float y0);
  9. float calc_chi_2(float t[],float x[], float s[], float a, float b);
  10.  
  11. int main(void)
  12. {
  13. FILE*fp;
  14. float t[ndat], exp_x[ndat], model_y[ndat], exp_y[ndat],error_y[ndat],error[ndat];
  15. int ih;
  16. if((fp=fopen("A16.0.csv","r")) !=NULL)
  17. {
  18. for(ih=0;ih<ndat;ih++)
  19. {
  20. fscanf(fp,"%f,%f,%f,%f,%f",&t[ih],&exp_x[ih],&model_y[ih],&exp_y[ih],&error_y[ih]);
  21. error[ih]=precision;
  22. }
  23. fclose(fp);
  24. }
  25. else{
  26. printf("File Open Error\n");
  27. }
  28. float v0=6.0,y0=5.0,chi_2;
  29. chi_2=calc_chi_2(t,exp_y,error,v0,y0);
  30. printf("chi square=%f,at v0=%f,y0=%f\n",chi_2,v0,y0);
  31. return 0;
  32. }
  33.  
  34. //caluculate chi square//
  35. float calc_chi_2(float t[],float x[], float s[], float a, float b)
  36. {
  37. float chi_2=0;
  38. int ii;
  39. for(ii=0;ii<ndat;ii++)
  40. {
  41. chi_2+=(x[ii]-func(t[ii],a,b))*(x[ii]-func(t[ii],a,b))/(s[ii]*s[ii]);
  42. printf("%f,%f,%f,%f,%f\n",chi_2,t[ii],x[ii],func(t[ii],a,b),s[ii]);
  43. }
  44. return chi_2;}
  45.  
  46.  
  47. //function//
  48. float func(float t, float v0, float y0)
  49. {
  50. float y;
  51. y=-(g/2)*t*t+v0*t+y0;
  52. return y;
  53. }
  54.  
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
File Open Error
inf,114147124162541398312091648.000000,0.000000,-inf,0.000000
inf,0.000000,0.000000,5.000000,0.000000
inf,0.000000,0.000000,5.000000,66906511892130383790080.000000
inf,0.000000,0.000000,5.000000,0.000000
inf,0.000000,0.000000,5.000000,95779691864976760913264640.000000
inf,0.000000,0.000000,5.000000,0.000000
inf,112894212081683008711557120.000000,114147493097422872503123968.000000,-inf,0.000000
inf,0.000000,0.000000,5.000000,0.000000
inf,0.000000,0.000000,5.000000,0.000000
inf,0.000000,0.000000,5.000000,0.000000
inf,0.000000,0.000000,5.000000,0.000000
inf,0.000000,0.000000,5.000000,0.000000
inf,0.000000,0.000000,5.000000,0.000000
inf,0.000000,0.000000,5.000000,0.000000
inf,0.000000,66906511892130383790080.000000,5.000000,0.000000
inf,0.000000,0.000000,5.000000,0.000000
inf,0.000000,114155535877839009867628544.000000,5.000000,0.000000
inf,0.000000,0.000000,5.000000,0.000000
inf,114147124162541398312091648.000000,0.000000,-inf,0.000000
inf,0.000000,0.000000,5.000000,0.000000
chi square=inf,at v0=6.000000,y0=5.000000