You are on page 1of 5

CURVE FITTING USING POLYNOMIAL REGRESSION

ALGORITHM: 1. Start the program. 2. Declare the input variables. 3. Print the statement to enter the order of polynomial. 4. Read the value op. 5. Print the statement to enter the number of data points to be entered for each variable. 6. Read the value n. 7. Print the statement to enter the independent and dependent variables. 8. If (n<= op) print regression is not possible. 9. print the values of polynomial coefficients.

PROGRAM:

//C program to use polynomial regression for curve fitting// #include <stdio.h> #include <conio.h> #include <math.h> void normal(float x[10], float y[10], float c[10][10], float b[10], int n, int m); void gauss(int n, float a[10][10], float b[10], float x[10]); main() { intn,op,m,i; float x[10],y[10],c[10][10],a[10],b[10];

printf("\nEnter the order of polynomial required,op="); scanf("%d",&op); printf("\n Enter the no.of data points to be entered for each variable,n= "); scanf("%d",&n); printf("\nInput data values, X and y \n"); printf("\n enter the data valus for independent and dependent variables respectively\n"); for(i=1;i<=n;i++) scanf("%f %f",&x[i],&y[i]);

if(n<=op) { printf("\n\n REGRESSION IS NOT POSSIBLE \n");

goto Stop; } m=op+1; normal(x,y,c,b,n,m); gauss(m,c,b,a); printf("\n\n Polynomial coefficients \n\n"); for(i=1;i<=m;i++) printf("%15.6f",a[i]); Stop: getch(); }

void normal(float x[10], float y[10], float c[10][10], float b[10], int n, int m) { int i,j,k,l1,l2;

for(j=1;j<=m;j++) { for(k=1;k<=m;k++) { c[j][k]=0.0; l1=k+j-2; for(i=1;i<=n;i++) c[j][k]=c[j][k]+ pow(x[i],l1); }

} for(j=1;j<=m;j++) { b[j]=0.0; l2=j-1; for(i=1;i<=n;i++) b[j]=b[j]+y[i]*pow(x[i],l2); }

} void gauss(int n, float a[10][10], float b[10], float x[10]) { inti,j,k; floatpivot,factor,sum; for(k=1;k<=n-1;k++) { pivot=a[k][k]; for(i=k+1;i<=n;i++) { factor=a[i][k]/pivot; for(j=k+1;j<=n;j++) a[i][j]=a[i][j]-factor*a[k][j]; b[i]=b[i]-factor*b[k]; } }

x[n]=b[n]/a[n][n]; for(k=n-1;k>=1;k--) { sum=0.0; for(j=k+1;j<=n;j++) sum=sum+a[k][j]*x[j]; x[k]=(b[k]-sum)/a[k][k]; }

You might also like