Source Code:
/* The following C program implements Curve Fitting by the Method of LeastSquares for some given tabular values. The tabular values (abscissa and
ordinate) are read as input. */
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 10
#define minvalue 0.0005
void main()
{
int degree, count, i,j,k, noofequations, temp;
float xtable[max][max], ytable[max][max], augmentedmatrix[max][max], temporary, r, solutions[max];
printf("\n How many points are to be entered:-");
scanf("%d",&count);
printf("\n Enter x & y values in tabular format:-\n");
for(i=0; i<count; i++)
for(j=0; j<=1; j++)
if(j%2==0)
scanf("%f",&xtable[i][1]);
else
scanf("%f",&ytable[i][0]);
printf("\n Degree of polynomial that is to be fitted:- ");
scanf("%d",°ree);
/* Constructing table with powers of x */
for(i=0; i<count; i++)
for(j=2; j<=2*degree; j++)
xtable[i][j]=pow(xtable[i][1],j);
/* Constructing table with combined terms of x and y */
for(i=0; i<count; i++)
for(j=1; j<=degree; j++)
ytable[i][j]=ytable[i][0]*xtable[i][j];
/* Calculating sum */
for(j=0;j<=2*degree;j++)
{
xtable[count][j]=ytable[count][j]=0;
for(i=0;i<count;i++)
{
if(j!=0)
xtable[count][j]+=xtable[i][j];
if(j<=degree)
ytable[count][j]+=ytable[i][j];
}
}
/* printing tables */
printf("\n\nTABLE WITH POWERS OF X \n");
for(i=0;i<count;i++)
{
printf("\n ");
for(j=1;j<=2*degree;j++)
printf("%.3f ",xtable[i][j]);
}
printf("\n\n TABLE WITH X-Y TERMS \n");
for(i=0;i<count;i++)
{
printf("\n ");
for(j=0;j<=degree;j++)
printf("%.3f ",ytable[i][j]);
}
/* Using GAUSS-JORDAN METHOD OF ELIMINATION
to find solutions of simultaneous equations */
noofequations=degree+1;
xtable[count][0]=count;
/* storing augmented co-efficient matrix as a matrix of dimension
(noofequations)x(noofequations+1) in 2D array */
for(i=0;i<noofequations;i++)
{
for(j=i,k=0;j<i+noofequations;j++)
augmentedmatrix[i][k++]=xtable[count][j];
augmentedmatrix[i][k]=ytable[count][i];
}
/* converting augmented matrix into upper triangular form */
for(j=0; j<noofequations; j++)
{
temp=j;
/* finding maximum coefficient of Xj in last (noofequations-j) equations */
for(i=j+1; i<noofequations; i++)
if(augmentedmatrix[i][j]>augmentedmatrix[temp][j])
temp=i;
if(fabs(augmentedmatrix[temp][j])<minvalue)
{
printf("\n Coefficients are too small to deal with !!!");
goto end;
}
/* swapping row which has maximum coefficient of Xj */
if(temp!=j)
for(k=0; k<=noofequations; k++)
{
temporary=augmentedmatrix[j][k] ;
augmentedmatrix[j][k]=augmentedmatrix[temp][k] ;
augmentedmatrix[temp][k]=temporary ;
}
/* performing row operations to form required diagonal matrix */
for(i=0; i<noofequations; i++)
if(i!=j)
{
r=augmentedmatrix[i][j];
for(k=0; k<=noofequations; k++)
augmentedmatrix[i][k]-=(augmentedmatrix[j][k]/augmentedmatrix[j][j])*r ;
}
}
/* storing solutions */
for(i=0;i<noofequations; i++)
solutions[i]=augmentedmatrix[i][noofequations]/augmentedmatrix[i][i] ;
/* printing solutions */
printf("\n\n\n Polynomial to be fitted is :- \n");
for(i=0; i<noofequations; i++)
if(i>1)
printf("(%f)*x^%d + ",solutions[i],i);
else if(i==1)
printf("(%f)*x + ",solutions[1]);
else
printf(" %f + ",solutions[0]);
end: getch() ;
}
Output:
Fitting the following tabular values by a 2nd degree polynomial:
x | -2 | -1 | 0 | 1 | 2 |
y | -9 | -4 | 1 | 6 | 10 |
No comments:
Post a Comment
Do you think this information useful or was not up to the mark? Comment if you have any advices or suggestions about the post.