C++ PROGRAMS FOR MECHANICAL
Find the correlation co-efficient, correlation and fitted line.
#include <stdio.h>
#include <math.h>
main()
{
int n, i;
float y, x, Sx, Sy, SSx, SSy, Sxy, corr, a, b;
printf(" Number of observations ( Pairwise ) ");
scanf("%d", &n);
printf("\n Enter the values pair-wise\n");
Sx = 0; Sy = 0; SSx = 0; SSy = 0; Sxy = 0;
for ( i = 1; i <=n; i++ )
{
scanf("%f%f", &y, &x);
Sx = Sx + x;
Sy = Sy + y;
SSx = SSx + x*x;
SSy = SSy + y*y;
Sxy = Sxy + x*y;
}
printf(" The sum of x is %f\n", Sx);
printf(" The sum of y is %f\n", Sy);
printf(" The sum of x^2 is %f\n", SSx);
printf(" The sum of y^2 is %f\n", SSy);
printf(" The sum of xy is %f\n", Sxy);
corr = (n*Sxy-Sx*Sy)/sqrt((n*SSx-Sx*Sx)*(n*SSy-Sy*Sy));
b = (n*Sxy-Sx*Sy)/(n*SSx-Sx*Sx);
a = (Sy - b*Sx)/n;
printf("\n The correlation coefficient, a = %f and b = %f\n The correlation is %f\n", a, b, corr);
printf(" Fitted line is y = %5.5f + %5.5fx", a, b);
return 0;
}
#include <stdio.h>
#include <math.h>
main()
{
int n, i;
float y, x, Sx, Sy, SSx, SSy, Sxy, corr, a, b;
printf(" Number of observations ( Pairwise ) ");
scanf("%d", &n);
printf("\n Enter the values pair-wise\n");
Sx = 0; Sy = 0; SSx = 0; SSy = 0; Sxy = 0;
for ( i = 1; i <=n; i++ )
{
scanf("%f%f", &y, &x);
Sx = Sx + x;
Sy = Sy + y;
SSx = SSx + x*x;
SSy = SSy + y*y;
Sxy = Sxy + x*y;
}
printf(" The sum of x is %f\n", Sx);
printf(" The sum of y is %f\n", Sy);
printf(" The sum of x^2 is %f\n", SSx);
printf(" The sum of y^2 is %f\n", SSy);
printf(" The sum of xy is %f\n", Sxy);
corr = (n*Sxy-Sx*Sy)/sqrt((n*SSx-Sx*Sx)*(n*SSy-Sy*Sy));
b = (n*Sxy-Sx*Sy)/(n*SSx-Sx*Sx);
a = (Sy - b*Sx)/n;
printf("\n The correlation coefficient, a = %f and b = %f\n The correlation is %f\n", a, b, corr);
printf(" Fitted line is y = %5.5f + %5.5fx", a, b);
return 0;
}
Comments
Post a Comment