Monday, 20 December 2010

Program to Implement the linear regression algorithm

Program:

#include<stdio.h>
#include<math.h>
void main()
{
int  n,I;
float sumx, sumxsq, sumy, sumxy, x, y, a0, a1, denom; printf(“enter the n value”);
scanf(“%d”,&n);
sumx=0;
sumsq=0;
sumy=0;
sumxy=0;
for(i=0;i<n;i++)
{
scanf(“%f %f”,&x, &y);
sumx +=x;
sumsq += pow(x, 2);
sumy +=y;
sumxy +=x * y;
}
denom = n * sumxsq – pow(sumx, 2);
a0 = (sumy * sumxsq –sumx *sumxy)/denom;
a1 = (n * sumxy –sumx *sumy)/denom; printf(“y= %fx + %f”,a1,a0);
}



Input/Output:


Enter the n value 7
1 2
2 5
4 7
5 10
6 12
8 15
9 19
Y = 1.980769x + 0.096154





No comments:

Post a Comment