MPD78
09-23-2009, 09:31 AM
Hello all,
I am trying to produce a straight line equation to replace a 2nd order polynomial. The graph of the 2nd order polynomial produces a straight line. I don't know why a straight line fit was not produced in the first place. Also, I do not have access to the original data used to create the polynomial fit. Therefore, I don't know when the equation starts to extrapolate and I don't know what the error on the data was.
Course of action.
1. Use the 2nd order polynomial to create a set of data points (500 of them).
2. Use the fitab.h routine to calculated the y=A+Bx equation, the Chi squared value, and the uncertainty of the A and B values.
Questions.
1. Do this make sense?
2. Since I don't know what the error on the original data was, should I impose and error on the data generated by the polynomial?
Information on the program.
The simple program below will generate 500 evenly spaced points that represent the mean specific heat of methane in units of kCal/nm^3-C as a function of temperature in units of F (Farenheit). Then fit this data using fitab.h routine.
Here is my simple main program.
#include "nr3.h"
#include "fitab.h"
#include "poly.h"
ofstream CH4MCPDATA; // Name of the output
string filename; // user entered filename
char szInput[256];
Doub array_x[500]; // array to store the temperatures (F)
Doub array_y[500]; // array to store the specific heat KCAL/NM3-DEG C
Doub a; // calculated value of A
Doub b; // calculated value of B
Doub val; // user input value for the value to calculate the
Doub una; // uncertainty of the value of A
Doub unb; // uncertainty of the value of B
Doub c; // calculated Chi squared value
Doub myCH4MCP; // calculated mean specific heat of CH4 (KCAL/NM3-DEG C)
Doub temp = 32.0; // initial temperature of 32 F
Doub vol = 1.0; // Constant volume of 100%
Doub pause; // dummy variable to keep the output screen visible
// Function to create the needed data points to be used in the fitab.h routine.
// This is the original 2rd order polynomial from FORTRAN for CH4 (Methane)
Doub CH4MCP(Doub vol,Doub temp){
temp=(temp-32)*0.555;
Doub coeff[] = {0.362224,3.14145e-4,-4.03263e-8};
Int n=3;
VecDoub c(n,coeff);
return (Poly(c)(temp))*vol;
}
// Begin main function
int main()
{
printf("Enter the name of the data file ");
gets(szInput);
filename=szInput;
CH4MCPDATA.open(filename.c_str());
// Begin data generation with 2nd order polynomial
for(Int j=0;j<500;j++){
myCH4MCP = CH4MCP(vol,temp);
array_x[j] = temp;
array_y[j] = myCH4MCP;
CH4MCPDATA << myCH4MCP << "," << temp << endl;
temp += 5;
}
Int n = sizeof(array_x)/sizeof(array_x[0]); // Determine the size of the array
VecDoub xx(n,array_x);
VecDoub yy(n,array_y);
// Call the linear fitting routine
Fitab linear(xx,yy);
a=linear.a; // value of A
b=linear.b; // value of B
c=linear.chi2; // value of C
una=linear.siga; // value of the uncertainty of the value of A
unb=linear.sigb; // value of the uncertainty of the value of B
// Send results to output window
cout << endl << endl << "The value of A is " << a << endl << endl;
cout << "The value of b is " << b << endl << endl;
cout << "The equation is in the form y = A + bx " << endl << endl;
cout << "y = " << a << " + " << b << "x" << endl << endl;
cout << "The Chi Squared value is " << c << endl << endl;
cout << "Uncertanty of A " << una*100 << "%" << endl << endl;
cout << "Uncertanty of b " << unb*100 << "%" << endl << endl;
cout << "Enter a value for x " << endl;
cin >> val;
cout << "Result is " << a+b*val << endl;
cin >> pause;
return 0;
}
Here are my results.
The value of A is 0.370498
The value of b is 0.000143359
The equation is in the form y = A + bx
y = 0.370498 + 0.000143359x
The Chi Squared value is 0.0167416
Uncertanty of A 0.0527802%
Uncertanty of b 3.59295e-005%
Any help on this would be great.
Thanks
Matt
I am trying to produce a straight line equation to replace a 2nd order polynomial. The graph of the 2nd order polynomial produces a straight line. I don't know why a straight line fit was not produced in the first place. Also, I do not have access to the original data used to create the polynomial fit. Therefore, I don't know when the equation starts to extrapolate and I don't know what the error on the data was.
Course of action.
1. Use the 2nd order polynomial to create a set of data points (500 of them).
2. Use the fitab.h routine to calculated the y=A+Bx equation, the Chi squared value, and the uncertainty of the A and B values.
Questions.
1. Do this make sense?
2. Since I don't know what the error on the original data was, should I impose and error on the data generated by the polynomial?
Information on the program.
The simple program below will generate 500 evenly spaced points that represent the mean specific heat of methane in units of kCal/nm^3-C as a function of temperature in units of F (Farenheit). Then fit this data using fitab.h routine.
Here is my simple main program.
#include "nr3.h"
#include "fitab.h"
#include "poly.h"
ofstream CH4MCPDATA; // Name of the output
string filename; // user entered filename
char szInput[256];
Doub array_x[500]; // array to store the temperatures (F)
Doub array_y[500]; // array to store the specific heat KCAL/NM3-DEG C
Doub a; // calculated value of A
Doub b; // calculated value of B
Doub val; // user input value for the value to calculate the
Doub una; // uncertainty of the value of A
Doub unb; // uncertainty of the value of B
Doub c; // calculated Chi squared value
Doub myCH4MCP; // calculated mean specific heat of CH4 (KCAL/NM3-DEG C)
Doub temp = 32.0; // initial temperature of 32 F
Doub vol = 1.0; // Constant volume of 100%
Doub pause; // dummy variable to keep the output screen visible
// Function to create the needed data points to be used in the fitab.h routine.
// This is the original 2rd order polynomial from FORTRAN for CH4 (Methane)
Doub CH4MCP(Doub vol,Doub temp){
temp=(temp-32)*0.555;
Doub coeff[] = {0.362224,3.14145e-4,-4.03263e-8};
Int n=3;
VecDoub c(n,coeff);
return (Poly(c)(temp))*vol;
}
// Begin main function
int main()
{
printf("Enter the name of the data file ");
gets(szInput);
filename=szInput;
CH4MCPDATA.open(filename.c_str());
// Begin data generation with 2nd order polynomial
for(Int j=0;j<500;j++){
myCH4MCP = CH4MCP(vol,temp);
array_x[j] = temp;
array_y[j] = myCH4MCP;
CH4MCPDATA << myCH4MCP << "," << temp << endl;
temp += 5;
}
Int n = sizeof(array_x)/sizeof(array_x[0]); // Determine the size of the array
VecDoub xx(n,array_x);
VecDoub yy(n,array_y);
// Call the linear fitting routine
Fitab linear(xx,yy);
a=linear.a; // value of A
b=linear.b; // value of B
c=linear.chi2; // value of C
una=linear.siga; // value of the uncertainty of the value of A
unb=linear.sigb; // value of the uncertainty of the value of B
// Send results to output window
cout << endl << endl << "The value of A is " << a << endl << endl;
cout << "The value of b is " << b << endl << endl;
cout << "The equation is in the form y = A + bx " << endl << endl;
cout << "y = " << a << " + " << b << "x" << endl << endl;
cout << "The Chi Squared value is " << c << endl << endl;
cout << "Uncertanty of A " << una*100 << "%" << endl << endl;
cout << "Uncertanty of b " << unb*100 << "%" << endl << endl;
cout << "Enter a value for x " << endl;
cin >> val;
cout << "Result is " << a+b*val << endl;
cin >> pause;
return 0;
}
Here are my results.
The value of A is 0.370498
The value of b is 0.000143359
The equation is in the form y = A + bx
y = 0.370498 + 0.000143359x
The Chi Squared value is 0.0167416
Uncertanty of A 0.0527802%
Uncertanty of b 3.59295e-005%
Any help on this would be great.
Thanks
Matt