Fit A Straight Line Of The Form y=ax+b
Write A Program To Fit A Straight Line Of The Form y=ax+b Source Code: #include<stdio.h> #include<conio.h> void main() { //declaring variables float x[100],y[100],xy[100],xx[100],a,b; float sx=0,sy=0,sxy=0,sxx=0,a1,b1,c1,a2,b2,c2; int n,i; clrscr(); printf("This is a program to fit a straight line of the form y=ax+b\n"); printf("enter the no of observation:"); scanf("%d",&n); //scanning of points for(i=0;i<n;i++) { printf("enter point no %d:",i+1); scanf("%g%g",&x[i],&y[i]); xy[i]=x[i]*y[i]; xx[i]=x[i]*x[i]; sx=sx+x[i]; sy=sy+y[i]; sxy=sxy+xy[i]; sxx=sxx+xx[i]; } printf("the computational table is\n"); printf("\nx\ty\txy\tx^2\n"); printf("\n-----\t-----\t-----\t-----\n"); for(i=0;i<n;i++) { printf("\n%g\t%g\t%g\t%g\n",x[i],y[i],xy[i],xx[i]); } printf("\n-----\t-----\t-----\t-----\n"); printf("\n%g\t%g\t%g\t%g\n",sx,sy,sxy,sxx); printf("%g=a.%g+b.%d\n",sy,sx,n); printf("%g=a.%g+b.%g\n",sxy,sxx,sx); a1=sx; b1=n; c1=-sy; a2=sxx; b2=sx; c2=-sxy; //checking for validation if((a1*b2-a2*b1)==0) { printf("the given points doesnot form a straight line\n:"); } else { a=(b1*c2-b2*c1)/(a1*b2-a2*b1); b=(a2*c1-a1*c2)/(a1*b2-a2*b1); } //the straight line is printf("\n the straight line is y=%gx+%g",a,b); getch(); } }
Tags:
C Program
0 comments