#include <stdio.h>
#include <math.h>
#define g 9.8
#define ndat 20
#define precision 0.5
float func(float t, float v0, float y0);
float calc_chi_2(float t[],float x[], float s[], float a, float b);
int main(void)
{
FILE*fp;
float t[ndat], exp_x[ndat], model_y[ndat], exp_y[ndat],error_y[ndat],error[ndat];
int ih;
if((fp
=fopen("A16.0.csv","r")) !=NULL
) {
for(ih=0;ih<ndat;ih++)
{
fscanf(fp
,"%f,%f,%f,%f,%f",&t
[ih
],&exp_x
[ih
],&model_y
[ih
],&exp_y
[ih
],&error_y
[ih
]); error[ih]=precision;
}
}
else{
}
float v0=6.0,y0=5.0,chi_2;
chi_2=calc_chi_2(t,exp_y,error,v0,y0);
printf("chi square=%f,at v0=%f,y0=%f\n",chi_2
,v0
,y0
); return 0;
}
//caluculate chi square//
float calc_chi_2(float t[],float x[], float s[], float a, float b)
{
float chi_2=0;
int ii;
for(ii=0;ii<ndat;ii++)
{
chi_2+=(x[ii]-func(t[ii],a,b))*(x[ii]-func(t[ii],a,b))/(s[ii]*s[ii]);
printf("%f,%f,%f,%f,%f\n",chi_2
,t
[ii
],x
[ii
],func
(t
[ii
],a
,b
),s
[ii
]); }
return chi_2;}
//function//
float func(float t, float v0, float y0)
{
float y;
y=-(g/2)*t*t+v0*t+y0;
return y;
}
CiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8bWF0aC5oPgojZGVmaW5lIGcgOS44CiNkZWZpbmUgbmRhdCAyMAojZGVmaW5lIHByZWNpc2lvbiAwLjUKCmZsb2F0IGZ1bmMoZmxvYXQgdCwgZmxvYXQgdjAsIGZsb2F0IHkwKTsKZmxvYXQgY2FsY19jaGlfMihmbG9hdCB0W10sZmxvYXQgeFtdLCBmbG9hdCBzW10sIGZsb2F0IGEsIGZsb2F0IGIpOwoKaW50IG1haW4odm9pZCkKewogIEZJTEUqZnA7CiAgZmxvYXQgdFtuZGF0XSwgZXhwX3hbbmRhdF0sIG1vZGVsX3lbbmRhdF0sIGV4cF95W25kYXRdLGV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CwgZmxvYXQgeTApCnsKICBmbG9hdCB5OwogIHk9LShnLzIpKnQqdCt2MCp0K3kwOwogIHJldHVybiB5Owp9Cg==
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