/* Least Square Fit */
#include<stdio.h>
#include<math.h>
int main()
{
int i, n;
float m, c, X[50], Y[50], Sx=0, Sy=0, Sxy=0, Sx2=0;
printf("Enter the no. of input data=");
scanf("%d", &n);
printf("\n Enter the values of x:\n");
for (i=0;i<n;i++)
{
scanf("%f", &X[i]);
}
printf("\n Enter the values of y:\n");
for (i=0;i<n;i++)
{
scanf("%f", &Y[i]);
}
for (i=0;i<n;i++)
{
Sx=Sx+X[i];
Sy=Sy+Y[i];
Sx2=X[i]*X[i];
Sxy=Sxy+X[i]*Y[i];
}
m=(n*Sxy-Sx*Sy)/(n*Sx2-Sx*Sx);
c=(Sx2*Sy-Sx*Sxy)/(n*Sx2-Sx*Sx);
printf("\n The line of best fit is : y=%fx+%f\n", m,c);
return(0);
}
#include<stdio.h>
#include<math.h>
int main()
{
int i, n;
float m, c, X[50], Y[50], Sx=0, Sy=0, Sxy=0, Sx2=0;
printf("Enter the no. of input data=");
scanf("%d", &n);
printf("\n Enter the values of x:\n");
for (i=0;i<n;i++)
{
scanf("%f", &X[i]);
}
printf("\n Enter the values of y:\n");
for (i=0;i<n;i++)
{
scanf("%f", &Y[i]);
}
for (i=0;i<n;i++)
{
Sx=Sx+X[i];
Sy=Sy+Y[i];
Sx2=X[i]*X[i];
Sxy=Sxy+X[i]*Y[i];
}
m=(n*Sxy-Sx*Sy)/(n*Sx2-Sx*Sx);
c=(Sx2*Sy-Sx*Sxy)/(n*Sx2-Sx*Sx);
printf("\n The line of best fit is : y=%fx+%f\n", m,c);
return(0);
}
Comments
Post a Comment