Thursday, January 11, 2018

Least Square Linear Model Program

#include<stdio.h>
#include<stdlib.h>
int main(){
   int n,i;
   float a,b,x[10],y[10],sx,sy,sx2,sxy;
   printf("Saroj Rana\nRoll:37\tSem:3rd\n");
   printf("Enter number of points:");
   scanf("%d",&n);
   for(i=0;i<n;i++){
       printf("Enter value for x and y at i=%d\n",i);
       scanf("%f%f",&x[i],&y[i]);
   }
   sx=0;
   sy=0;
   sx2=0;
   sxy=0;
   for(i=0;i<n;i++){
       sx+=x[i];
       sy+=y[i];
       sxy+=(x[i]*y[i]);
       sx2+=(x[i]*x[i]);
   }
   b=((n*sxy)-(sx*sy))/((n*sx2)-(sx*sx));
   a=(sy/n)-(b*sx/n);
   printf("Equation of form:  y=ax+b");
   printf("\nStraight line equation:  y=%0.3fx+%0.3f",b,a);
   return 0;

}

No comments:

Post a Comment