Objective : - To write a C++ Program for Curve Fitting using Method of Least Squares
Theory:
In many branches of Engineering, it is required to express a given data, obtained from observations, in the form of a law connecting two variables involved, such law inferred from some scheme, is known as the empirical law. Then this empirical law can be used to predict the behaviour of the system under study.
Scatter Diagram
To find a relationship between the set of paired observations x and y, we plot their corresponding values on the graph, taking one of the variables along the x-axis and other along the y-axis i.e. (x1,y1), (x2,y2)…….(xn,yn). The resulting diagram showing a collection of dots is called a Scatter Diagram. A Smooth curve that approximates the above set of points is known as the Approximating Curve.
Curve Fitting
Several equations of different types can be obtained to express the given data approximately, but the problem is to find the equation of the curve of ‘best fit’ which may be most suitable for predicting the unknown values. The process of finding such an equation of ‘best fit’ is known as curve fitting.
If there are n pairs of observed values then it is possible to fit the given data to an equation that contains n arbitrary constants for we can solve n simultaneous equations for n unknowns. If it is desired to obtain an equation representing these data but having less than n arbitrary constants, then we can do it through any of these four methods,
1. Graphical Method
2. Method of Least Squares
3. Method of Group Averages, and
4. Method of Moments.
The graphical method & Method of averages fail to give the values of the unknown constants uniquely and accurately. The method of least curve is probably the best to fit a unique curve to a given data. It is widely used in applications and can be implemented on a computer.
Given data {(x1, y1), . . . ,(xN , yN )}, we may define the error associated to saying y = ax + b,
Program:
//Program for Parabolic Fit by Least Square Method
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main( )
{
clrscr( );
float augm [3][4] = { {0,0,0,0},{0,0,0,0},{0,0,0,0}};
float t,a,b,c,x,y,xsq;
int i,j,k,n;
cout<<"Enter the No. of Pairs of Observed Values :"<<endl;
cin>>n;
cout<<setprecision(4);
augm [0][0] = n;
for (i=0;i<n;i++)
{
cout<<" Pair No. "<<i+1<<endl;
cin>>x>>y;
xsq=x*x;
augm [0][1] += x;
augm [0][2] += xsq;
augm [1][2] += x*xsq;
augm [2][2] += xsq*xsq;
augm [0][3] += y;
augm [1][3] += x*y;
augm [2][3] += xsq*y;
}
augm [1][1] = augm [0][2];
augm [2][1] = augm [1][2];
augm [1][0] = augm [0][1];
augm [2][0] = augm [1][1];
cout<<"The Augmented Matrix is :-"<<endl;
for(i=0;i<3;i++)
{
cout<<endl;
for(j=0;j<4;j++)
{
cout<<augm[i][j];
cout<<"\t";
}
}
// Solving for a,b,c by using Gauss Jordan Method
for(j=0;j<3;j++)
{
for(i=0;i<3;i++)
{
if(i!=j)
{
t = augm [i][j]/augm[j][j];
for(k=0;k<4;k++)
{
augm[i][k] -= augm[j][k]*t;
}
}
}
}
a=augm[0][3]/augm[0][0];
b=augm[1][3]/augm[1][1];
c=augm[2][3]/augm[2][2];
cout<<setprecision(4)<<"a= "<<a<<" b ="<<b<<" c = "<<c<<endl;
getch( );
}