#include #include #include "matrix.cpp" void read_datafile(char *filename, int n, long double *x, long double *y); void write_results(char *filename, int n, long double *x, long double *y, long double *dydx, long double *d2ydx2); void fit_poly(int n, int degree, long double *x, long double *y, long double *coeff); void create_output(int outpnts, int degree, long double *dydx, long double *d2ydx2, long double *coeff, long double *xnu, long double *ynu, long double xo, long double xf); void fit_poly2(int n, int degree, long double *x, long double *y, long double *coeff); void main(void) { char output_file[50]; char input_file[50]; int n, outpnts, degree; long double *coeff,*x,*y,*dydx,*d2ydx2,*xnu,*ynu; cout << "Enter input filename: "; cin >> input_file; cout << "Enter output filename: "; cin >> output_file; cout << "Enter the number of data points to read: "; cin >> n; cout << "Enter the number of points to output: "; cin >> outpnts; cout << "Enter degree of polynomial: "; cin >> degree; x = new long double[n]; y = new long double[n]; dydx = new long double[outpnts]; d2ydx2 = new long double[outpnts]; xnu = new long double[outpnts]; ynu = new long double[outpnts]; coeff = new long double[degree+1]; read_datafile(input_file,n,x,y); fit_poly2(n,degree,x,y,coeff); for (int i=0; i<=degree; i++) cout << "Coefficient " << i << ": " << coeff[i] << endl; create_output(outpnts, degree, dydx, d2ydx2,coeff, xnu, ynu, x[0], x[n-1]); write_results(output_file, outpnts, xnu, ynu, dydx, d2ydx2); delete []x; delete []y; delete []dydx; delete []d2ydx2; delete []xnu; delete []ynu; delete []coeff; cout << "Pausing. Enter integer to exit: "; int wait; cin >> wait; } /* n = number data points degree = degree of polynomial to fit x = array of x values y = array of y values coeff = array for coefficients of fit polynomial (degree + 1 coefficients are returned) This function fits a polynomial to the given data with a least squares method, using Gauss-Jordan elimination to solve the linear equations. */ void fit_poly2(int n, int degree, long double *x, long double *y, long double *coeff) { // Consider sums a row matrix of degree+1 elements containing yk * xk^i long double *sums; sums = new long double[degree+1]; for (int i=0; i<=degree; i++) // Sum over degree of term { sums[i] = 0.0; for (int k=0; kequals(&delta); } // Create the sums long double *sums; sums = new long double[degree+1]; for (int i=0; i<=degree; i++) // create sums { sums[i] = 0.0; for (int k=0; kset(j,i,sums[j]); } } long double det_delta; // Compute determinant of delata a single time det_delta = delta.det(); // Compute coeficients... display a message since the algorithm can increase // in computational time to unreasonable lengths very quickly. for (int i=0; i<=degree; i++) { coeff[i] = co_mats[i]->det() / det_delta; cout << "Finsihed coeff[" << i << "]" << endl; delete co_mats[i]; } delete [] sums; delete [] co_mats; } /* Opens the text file named "filename" and reads n number of x,y data points where the input is tab delimited. If the end of the file is reached it will not read the full amount of data and display an error message. */ void read_datafile(char *filename, int n, long double *x, long double *y) { fstream input_file; long double xpoint,ypoint; input_file.open(filename, ios::in); for(int i=0; ( i> xpoint) ; i++) { input_file >> ypoint; x[i] = xpoint; y[i] = ypoint; } input_file.close(); } /* Opens text file named "filename" and outputs n datapoints in the format: x tab y tab dydx tab d2ydx2 endline. */ void write_results(char *filename, int n, long double *x, long double *y, long double *dydx, long double *d2ydx2) { fstream output_file; output_file.open(filename, ios::out); if(!output_file) {cerr << "Error opening output file."; return;} for(int i=0; i