Ex No: 5 Curve Fitting Using Polynomial Regression: Description
Ex No: 5 Curve Fitting Using Polynomial Regression: Description
Ex No: 5
20.02.2012
DESCRIPTION:
The method of least squares is a standard approach to the approximate solution of the over determined systems, i.e, sets of equations where there are more equations than unknowns. Least Squares means that overall solution minimizes the sum of the squares of the errors made in the results of every single equation. The most important application is in data fitting. The best fit in the leastsquares sense minimizes the sum of squared residuals, a residual being the difference between an observed value and the fitted value provided by a model. When the problem has substantial uncertainties in the independent variable (the x variable), then simple regression and least squares methods have problems; in such cases, the methodology required for fitting errors-in-variables models may be considered instead of that for least squares. Least squares problems fall into two categories: linear or ordinary least squares and non-linear least squares, depending on whether or not the residuals are linear in all unknowns. The linear least-squares problems occur in statistical regression analysis; it has a closed-form solution. A closed-form solution (or closed-form expression) is any formula that can be evaluated in a finite number of standard operations. The non-linear problem has no closed-form solution and is usually solved by iterative refinement; at each iteration the system is approximated by a linear one, thus the core calculation is similar in both cases.
Thondamon V 103109096
AIM:
Find the values of a0, a1 and a2 using least square method to obtain a polynomial of order 2 in terms of x y=a0+a1x+a2x2 x y 0.0 4.7 1.0 2.0 3.0 4.0 5.0 11.3 24.7 47.3 76.7 115.3
PROGRAM:
#include<iostream.h> #include<conio.h> #include<math.h> int main( ) { int m,n,i,j,k; cout<<"Enter the number of coordinates: "; cin>>m; float x[m],y[m]; cout<<endl; for(i=0;i<m;i++) { cout<<"Enter the X coordinate:"; cin>>x[i]; cout<<"Enter the Y coordinate:"; cin>>y[i]; } cout<<"\nEnter the degree of polynomial to be fit: "; cin>>n; float a[n+1][n+1],b[n+1],d[n+1][n+1],c[n+1]; for(i=0;i<n+1;i++) { for(j=0;j<n+1;j++) a[i][j]=0;
for(i=0;i<n+1;i++) { for(j=0;j<n+1;j++) { for(k=0;k<m;k++) a[i][j]+=pow(x[k],i+j); } for(k=0;k<m;k++) b[i]+=(pow(x[k],i))*y[k]; } for(int i=0;i<n+1;i++) { for(int j=0;j<n+1;j++) d[i][j]=a[i][j]; } for(int k=0;k<n;k++) { h: if(a[k][k]!=0) { for(int i=k+1;i<n+1;i++) for(int j=k;j<n+1;j++) { d[i][j]=(a[i][j]*a[k][k])- (a[k][j]*a[i][k]); } b[i]=(b[i]*a[k][k])-(b[k]*a[i][k]); for(int i=0;i<n+1;i++) { for(int j=0;j<n+1;j++) a[i][j]=d[i][j]; } }
Thondamon V 103109096 else { float t[n+1],temp; for(int i=0;i<n;i++) { for(int j=0;j<n+1;j++) { t[j]=d[i][j]; d[i][j]=d[i+1][j]; d[i+1][j]=t[j]; } temp=b[i]; b[i]=b[i+1]; b[i+1]=temp; } for(int i=0;i<n+1;i++) { for(int j=0;j<n+1;j++) a[i][j]=d[i][j]; } goto h;
for(int i=n;i>=0;i--) { float sum=0; for(int j=n;j>i;j--) sum+=(a[i][j]*c[j]); c[i]=(b[i]-sum)/a[i][i]; } cout<<"\nThe solution of the equation y=a0+a1*x+a2*x*x is\n"; for(int i=0;i<n+1;i++) cout<<"a"<<i<<": "<<c[i]<<"\n"; getch( );
Thondamon V 103109096
OUTPUT: