Curve Fitting

► Algorithm Of Different Numerical Curve Fitting Methods Including line fitting and parabola fitting:- 1. Line Fitting Linefitting(a) { Here a[] is a structure array to hold the points of the coordinate axes. for(i=0 to i<n0) { xi=xi+a[i].x yi=yi+a[i].y xi2=xi2+(a[i].x)*(a[i].x) xyi=xyi+(a[i].x)*(a[i].y) } b[0][0]=xi2 b[0][1]=xi b[0][2]=xyi b[1][0]=xi b[1][1]=n0 b[1][2]=yi //Creates the required coordinate matrix coordinate matrix Gauss(b,2) //It calls to the Gauss function to solve the coordinate matrix } 2.Parabola fitting parabolafit(a) { Here a[] is a structure array to hold the points of the coordinate axes. And b[][] is the required co-ordinate matrix to solve which is to be calculated by this algorithm. yi=xyi=x2yi=0 for(i=0 to 4) xi[i]=0 for(i=0 to n0) { xi[0]=xi[0]+a[i].x yi=yi+a[i].y xi[3]=xi[3]+(a[i].x)*(a[i].x)*(a[i].x)*(a[i].x) xi[2]=xi[2]+(a[i].x)*(a[i].x)*(a[i].x) xi[1]=xi[1]+(a[i].x)*(a[i].x) xyi=xyi+(a[i].x)*(a[i].y) x2yi=x2yi+(a[i].x)*(a[i].x)*(a[i].y) } for(i=0 to 3) for(j=0 to 4) { if(j=3) { if(i≠0) b[i][j]=(i=1)?xyi:yi //adjusting value of the b[][] matrix else b[i][j]=x2yi continue } else if(j=2 && i=2) { b[i][j]=n0 continue } b[i][j]=xi[(4-i-j)-1] //sending the b[][] matrix to gauss elimination fn for solving it. } gauss(b,3) } C Program For Curve Fitting Source Code: #include<stdio.h> #include<conio.h> struct coordinate { int x,y; }; int n0,i; float x[5]; void gauss(float **a,int n) { int row,col,k,j; float m,sum; for(k=0;k<n-1;k++) { for(row=k+1;row<n;row++) { m=a[row][k]/a[k][k]; for(col=0;col<=n;col++) { a[row][col]=a[row][col]-m*a[k][col]; } } } x[n-1]=a[n-1][n]/a[n-1][n-1]; for(i=n-2;i>=0;i--) { sum=0; for(j=i+1;j<n;j++) { sum=sum+a[i][j]*x[j]; } x[i]=(a[i][n]-sum)/a[i][i]; } if(n==2) printf("\n\nRequired straight line:-\n\ty=(%g)x+(%g)",x[0],x[1]); else printf("\n\nRequired Parabola:-\n\ty=(%g)x^2+(%g)x+(%g)",x[0],x[1],x[2]); } void linef(struct coordinate *a) { float **b,xi,xi2,yi,xyi; int j; xi=xi2=yi=xyi=0; b=(float **)malloc(sizeof(float *)*2); for(i=0;i<2;i++) { b[i]=(float *)malloc(sizeof(float )*(3)); } for(i=0;i<n0;i++) { xi=xi+a[i].x; yi=yi+a[i].y; xi2=xi2+(a[i].x)*(a[i].x); xyi=xyi+(a[i].x)*(a[i].y); } b[0][0]=xi2; b[0][1]=xi; b[0][2]=xyi; b[1][0]=xi; b[1][1]=n0; b[1][2]=yi; printf("\nRequired matrix to solve "); for(i=0;i<2;i++) { printf("\n"); for(j=0;j<3;j++) printf("\t%g",b[i][j]); } gauss(b,2); } void parabola(struct coordinate *a) { float **b,xi[4],yi,xyi,x2yi; int j; yi=xyi=x2yi=0; for(i=0;i<4;i++) xi[i]=0; b=(float **)malloc(sizeof(float *)*3); for(i=0;i<3;i++) { b[i]=(float *)malloc(sizeof(float )*(4)); } for(i=0;i<n0;i++) { xi[0]=xi[0]+a[i].x; yi=yi+a[i].y; xi[3]=xi[3]+(a[i].x)*(a[i].x)*(a[i].x)*(a[i].x); xi[2]=xi[2]+(a[i].x)*(a[i].x)*(a[i].x); xi[1]=xi[1]+(a[i].x)*(a[i].x); xyi=xyi+(a[i].x)*(a[i].y); x2yi=x2yi+(a[i].x)*(a[i].x)*(a[i].y); } for(i=0;i<3;i++) for(j=0;j<4;j++) { if(j==3) { if(i!=0) b[i][j]=(i==1)?xyi:yi; else b[i][j]=x2yi; continue; } else if(j==2 && i==2) { b[i][j]=n0; continue; } b[i][j]=xi[(4-i-j)-1]; } printf("\nRequired matrix to solve "); for(i=0;i<3;i++) { printf("\n"); for(j=0;j<4;j++) printf("\t%g",b[i][j]); } gauss(b,3); } void main() { int ch; struct coordinate *a; clrscr(); printf("\nWhich type of curve fitting is to be done.?\n1.Line Fitting\n2.Parabola Fitting \n\nYours choice "); scanf("%d",&ch); printf("\nEnter the no. of co-ordinates you will insert "); scanf("%d",&n0); a=(struct coordinate *)malloc(sizeof(struct coordinate)*n0); for(i=0;i<n0;i++) { printf("Enter co-ordinate %d ",i+1); scanf("%d %d",&a[i].x,&a[i].y); } switch(ch) { case 1: linef(a); break; case 2: parabola(a); break; default:printf("\nThis wasn't a valid choice.. :(") } getch(); }

Share:

0 comments