#include <iostream> #include <math.h> using namespace std; class LeastSquare { public: LeastSquare() { Ex = Ey = Exy = Exx = 0; } void askN(); void askX(); void askY(); void findXY(); void findXX(); void findA(); void findB(); void solve(); private: double a, b , x[10],y[10],xy[10],xx[10]; int num; double Ex,Ey,Exy,Exx; }; void LeastSquare::askN() { cout << "Enter the no of values: "; cin >> num ; } void LeastSquare::askX() { cout << "Enter the values of X[] : \n"; for(int i = 0 ; i <num;i++) { cout << "X[" << i << "]: "; cin >>x[i]; Ex += x[i]; } } void LeastSquare::askY() { cout << "Enter the values of Y[] : \n"; for(int i = 0 ; i <num;i++) { cout << "Y[" << i << "]: "; cin >>y[i]; Ey += y[i]; } } void LeastSquare::findXY() { for(int i = 0; i < num; i++) { xy[i] = x[i] * y[i]; Exy += xy[i]; } } void LeastSquare::findXX() { for(int i = 0; i < num; i++) { xx[i] = x[i] * x[i]; Exx += xx[i]; } } void LeastSquare::findA() { a = Ey/num - b * Ex / num; } void LeastSquare::findB() { b = (num * Exy - Ex * Ey) / (num * Exx - pow(Ex,2.0)); } void LeastSquare::solve() { askN(); askX(); askY(); findXX(); findXY(); findB(); findA(); cout <<endl << "The Required eqn of straight line is: "; cout << endl << "y = " << a << " + " << b << "x"; } int main() { LeastSquare l; l.solve(); return 0; }
C and C++ programs, games, softwares. Easy programming guide. Learn to code and enjoy coding in CoderNepal
Home » Least Square Method
Least Square Method
This is the solution for Least Square Method
Subscribe to:
Posts (Atom)
No comments:
Post a Comment