Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <cmath>
- using namespace std;
- class LeastSquareFit{
- double **A, *X, *B, *x, *y;
- double *sum_x, *sum_yx;
- int m, n;
- public:
- LeastSquareFit(int, int);
- ~LeastSquareFit();
- void input();
- void calcSumX();
- void calcSumYX();
- void calcAMatrix();
- void calcBMatrix();
- void performGaussElimination();
- void display();
- };
- void LeastSquareFit::performGaussElimination(){
- int i, j, k;
- double multiplication_factor;
- // Obtaining triangular equations
- for(k = 1; k <= n-1; k++){
- for(i = k + 1; i <= n; i++){
- multiplication_factor = -(A[k][k] / A[i][k]);
- for(j = 1; j <= n; j++){
- A[i][j] = A[k][j] + (multiplication_factor * A[i][j]);
- }
- B[i] = B[k] + (multiplication_factor * B[i]);
- }
- }
- // Applying back substitution to obtain the solution
- X[n] = B[n] / A[n][n];
- double sum;
- for(i = n - 1; i >= 1; i--){
- sum = 0;
- for(j = i + 1; j <= n; j++){
- sum += A[i][j] * X[j];
- }
- X[i] = (B[i] - sum) / A[i][i];
- }
- }
- LeastSquareFit::~LeastSquareFit(){
- delete[] x;
- delete[] y;
- delete[] X;
- delete[] B;
- delete[] sum_x;
- delete[] sum_yx;
- for(int i = 0; i < (LeastSquareFit::n + 1); i++){
- delete[] A[i];
- }
- }
- LeastSquareFit::LeastSquareFit(int m, int n){
- LeastSquareFit::m = m;
- LeastSquareFit::n = n;
- x = new double[LeastSquareFit::m];
- y = new double[LeastSquareFit::m];
- X = new double[LeastSquareFit::n + 1];
- B = new double[LeastSquareFit::n + 1];
- A = new double*[LeastSquareFit::n + 1];
- for(int i = 0; i < (LeastSquareFit::n + 1); i++){
- A[i] = new double[LeastSquareFit::n + 1];
- }
- sum_x = new double[(LeastSquareFit::n * LeastSquareFit::n) + 1];
- sum_yx = new double[LeastSquareFit::n + 1];
- }
- void LeastSquareFit::calcSumX(){
- sum_x[0] = 1;
- int limit = n * n;
- for(int i = 1; i <= limit; i++){
- sum_x[i] = 0;
- for(int j = 0; j < m; j++){
- sum_x[i] += pow(x[j], i);
- }
- }
- }
- void LeastSquareFit::calcSumYX(){
- int limit = n + 1;
- for(int i = 0; i < limit; i++){
- sum_yx[i] = 0;
- for(int k = 0; k < m; k++){
- sum_yx[i] += (y[k] * pow(x[k], i));
- }
- }
- }
- void LeastSquareFit::calcAMatrix(){
- A[0][0] = m;
- int rows = n + 1;
- int cols = n + 1;
- for(int i = 0; i < rows; i++){
- for(int j = 0; j < cols; j++){
- if(i != 0 || j != 0){
- A[i][j] = sum_x[i + j];
- }
- }
- }
- }
- void LeastSquareFit::calcBMatrix(){
- int limit = n + 1;
- for(int i = 0; i < limit; i++){
- B[i] = sum_yx[i];
- }
- }
- void LeastSquareFit::input(){
- for(int i = 0; i < m; i++){
- cout << "x[" << i << "] = ";
- cin >> x[i];
- cout << "y[" << i << "] = ";
- cin >> y[i];
- }
- }
- void LeastSquareFit::display(){
- calcSumX();
- calcSumYX();
- calcAMatrix();
- calcBMatrix();
- cout << "Displaying A matrix\n";
- int rows = n + 1;
- int cols = n + 1;
- for(int i = 0; i < rows; i++){
- for(int j = 0; j < cols; j++){
- cout << A[i][j] << "\t";
- }
- cout << endl;
- }
- cout << "\nDisplaying B matrix\n";
- int limit = n + 1;
- for(int i = 0; i < limit; i++){
- cout << B[i] << "\n";
- }
- cout << "\nDisplaying sum_yx matrix\n";
- for(int i = 0; i < limit; i++){
- cout << sum_yx[i] << ",";
- }
- cout << "\nDisplaying sum_yx matrix\n";
- limit = (n * n) + 1;
- for(int i = 0; i < limit; i++){
- cout << sum_x[i] << ",";
- }
- performGaussElimination();
- cout << "\n\nDisplaying Triangular matrix\n";
- for(int i = 0; i < rows; i++){
- for(int j = 0; j < cols; j++){
- cout << A[i][j] << "\t";
- }
- cout << endl;
- }
- }
- int main(){
- int n, m;
- system("cls");
- cout << "Enter the degree of polynomial: ";
- cin >> n;
- cout << "Enter the number of points in experiment: ";
- cin >> m;
- LeastSquareFit obj(m, n);
- obj.input();
- obj.display();
- cin.ignore();
- cin.get();
- return 0;
- }
Add Comment
Please, Sign In to add comment