1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
#include <vector>
#include <stack>
using namespace std;
double fitfunction(double a, double b, double c, double d, double x, double y)
{
double fct = a + b*x + c*y + d*y*y;
return fct;
}
double chisquarecalculation(double a, double b, double c, double d, double x, double y, double z)
{
double chisquare = ( (z - fitfunction(a,b,c,d,x,y) )*(z - fitfunction(a,b,c,d,x,y) ) ) / (z*z);
return chisquare;
}
int main()
{
//definition of the input and output files
ifstream input1("dataset1_usedtofit.txt");
ifstream input2("dataset2_usedtofit.txt");
ifstream input3("dataset3_usedtofit.txt");
ifstream input4("dataset4firstpart_usedtofit.txt");
ifstream input5("dataset4secondpart_usedtofit.txt");
ifstream input6("dataset4thirdpart_usedtofit.txt");
ifstream input7("dataset4lastpart_usedtofit.txt");
ofstream output1("chisquare1.txt");
ofstream output2("chisquare2.txt");
ofstream output3("chisquare3.txt");
ofstream output4("chisquare4.txt");
ofstream output5("chisquare5.txt");
ofstream output6("chisquare6.txt");
ofstream output7("chisquare7.txt");
//definition of the chisquares function as a function of a,b,c,d
double a=1; //fit parameters which I would like to use as theoretical entities, uninitialized
double b=1;
double c=1;
double d=1;
double x,y,z;
double sum(0.);
while(input1) //we didn't reach the end of the file
{
input1>> x >> y >> z; //we take out values from the dataset
if(input1) //if we can read something
{
output1<<chisquarecalculation(a,b,c,d,x,y,z)<<" "<<endl;
sum+=chisquarecalculation(a,b,c,d,x,y,z);
}
}
cout<< "chisquare for the 1st dataset by direct method: "<<sum<<endl;
sum=0;
while(input2) //we didn't reach the end of the file
{
input2>> x >> y >> z; //we take out values from the dataset
if(input2) //if we can read something
{
output2<<chisquarecalculation(a,b,c,d,x,y,z)<<" "<<endl;
sum+=chisquarecalculation(a,b,c,d,x,y,z);
}
}
cout<< "chisquare for the 2nd dataset by direct method: "<<sum<<endl;
sum=0;
while(input3) //we didn't reach the end of the file
{
input3>> x >> y >> z; //we take out values from the dataset
if(input3) //if we can read something
{
output3<<chisquarecalculation(a,b,c,d,x,y,z)<<" "<<endl;
sum+=chisquarecalculation(a,b,c,d,x,y,z);
}
}
cout<< "chisquare for the 3rd dataset by direct method: "<<sum<<endl;
sum=0;
while(input4) //we didn't reach the end of the file
{
input4>> x >> y >> z; //we take out values from the dataset
if(input4) //if we can read something
{
output4<<chisquarecalculation(a,b,c,d,x,y,z)<<" "<<endl;
sum+=chisquarecalculation(a,b,c,d,x,y,z);
}
}
cout<< "chisquare for the 4th dataset by direct method: "<<sum<<endl;
sum=0;
while(input5) //we didn't reach the end of the file
{
input5>> x >> y >> z; //we take out values from the dataset
if(input5) //if we can read something
{
output5<<chisquarecalculation(a,b,c,d,x,y,z)<<" "<<endl;
sum+=chisquarecalculation(a,b,c,d,x,y,z);
}
}
cout<< "chisquare for the 5th dataset by direct method: "<<sum<<endl;
sum=0;
while(input6) //we didn't reach the end of the file
{
input6>> x >> y >> z; //we take out values from the dataset
if(input6) //if we can read something
{
output6<<chisquarecalculation(a,b,c,d,x,y,z)<<" "<<endl;
sum+=chisquarecalculation(a,b,c,d,x,y,z);
}
}
cout<< "chisquare for the 6th dataset by direct method: "<<sum<<endl;
sum=0;
while(input7) //we didn't reach the end of the file
{
input7>> x >> y >> z; //we take out values from the dataset
if(input7) //if we can read something
{
output7<<chisquarecalculation(a,b,c,d,x,y,z)<<" "<<endl;
sum+=chisquarecalculation(a,b,c,d,x,y,z);
}
}
cout<< "chisquare for the last dataset by direct method: "<<sum<<endl;
sum=0;
}
|