Chapter 15 fitab.h Results Interpretation


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

davekw7x
09-23-2009, 11:50 AM
...The graph of the 2nd order polynomial produces a straight line.... Really? Is it a straight line or does it "kind of look like" a straight line. A polynomial of degree 2 that has a relatively small coefficient of x^2 "kind of looks like" a straight line. For example the test polynomial in your program is 0.362224 + 3.14145e-4 * x - 4.03263e-8 * x^2, right?

See attachment for a comparison of this function with data points generated from your program.


I don't know...[lots of stuff]

If you are given a bunch of data points, using fitab can create a linear approximation that minimizes the sum of the squares of differences for those points. I'm not sure what else you can expect to get out of it. I mean, if you actually knew something about errors in the data values, maybe you could use that to get a more reasonable estimate of "goodness of fit" that could be related back to the underlying process, but...
Does this make sense...

When people want some help getting some numbers from a bunch of data points, questions are usually asked about the provenance of the data (where did it come from, what is known about error statistics, etc.) as well as questions about the nature of the underlying process that we want to approximate with our simpler model so that we can believe that the type of simplification being proposed is appropriate.


The question that sometimes doesn't get asked may be more important than any of this: What are we going to do with the answer?

That may raise additional questions that should have been asked. Such as:

What is the "best thing/worst thing" that can happen with our approximation and what are the consequences. Stuff like that.


Regards,

Dave

MPD78
09-23-2009, 12:33 PM
Thanks Dave.

The general opinion is that the initial data and curve fitting was suspect from 30 years ago.

I am going to just use new data and interpolation. That way I can know when the program starts to extrapolate and have a decent error estimate.

Thanks
Matt