Subscribe Us

Responsive Advertisement

Advertisement

8. Curve fitting method (fitting of a straight line)

 

8. Curve fitting method (fitting of a straight line)

by ujjal roy



#include<bits/stdc++.h>

using namespace std;

int main()

{

   double sum_x=0,sum_y=0,sum_x2=0,sum_xy=0;

    int n;

    cout<<"Enter number of element  : ";

    cin>>n;

    cout<<"Enter x and y : ";

    for(int i=0;i<n;i++)

    {

        double x,y;

        cin>>x>>y;

        sum_x+=x;

        sum_y+=y;

        sum_x2+=(x*x);

        sum_xy+=(x*y);

    }


    double a;

    double b;

    b=n*sum_xy-(sum_x*sum_y);

    b=(double)b/(n*sum_x2-sum_x*sum_x);

    a=(sum_y-b*sum_x)/(double)n;

    cout<<"Y = "<<fixed<<setprecision(3)<<a<<" + "<<b<<"*x"<<endl;


    return 0;

}


Post a Comment

0 Comments