-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurvefit(liner).cpp
37 lines (37 loc) · 1.63 KB
/
curvefit(liner).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//Linear Fit
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
int i,n;
cout<<"\nEnter the no. of data pairs:\n"; //To find the size of arrays
cin>>n;
float x[n],y[n],a,b;
cout<<"\nEnter the x-axis values:\n"; //Input x-values
for (i=0;i<n;i++)
cin>>x[i];
cout<<"\nEnter the y-axis values:\n"; //Input y-values
for (i=0;i<n;i++)
cin>>y[i];
float xsum=0,x2sum=0,ysum=0,xysum=0; //variables for sums/sigma of xi,yi,xi^2,xiyi etc
for (i=0;i<n;i++)
{
xsum=xsum+x[i]; //calculate sigma(xi)
ysum=ysum+y[i]; //calculate sigma(yi)
x2sum=x2sum+pow(x[i],2); //calculate sigma(x^2i)
xysum=xysum+x[i]*y[i]; //calculate sigma(xi*yi)
}
a=(n*xysum-xsum*ysum)/(n*x2sum-xsum*xsum); //calculate slope
b=(x2sum*ysum-xsum*xysum)/(x2sum*n-xsum*xsum); //calculate intercept
float y_fit[n]; //an array to store the new fitted values of y
for (i=0;i<n;i++)
y_fit[i]=a*x[i]+b; //to calculate y(fitted) at given x points
cout<<"S.no"<<setw(5)<<"x"<<setw(19)<<"y(observed)"<<setw(19)<<"y(fitted)"<<endl;
cout<<"-----------------------------------------------------------------\n";
for (i=0;i<n;i++)
cout<<i+1<<"."<<setw(8)<<x[i]<<setw(15)<<y[i]<<setw(18)<<y_fit[i]<<endl;//print a table of x,y(obs.) and y(fit.)
cout<<"\nThe linear fit line is:\n"<<a<<"x + "<<b<<endl; //print the best fit line
return 0;
}