Given: x[64] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23, 24,
ID: 3680927 • Letter: G
Question
Given:
x[64] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,
24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,
46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64}
y[64] = {44.75, 45.54, 46.28, 48.53, 47.86, 49.46, 49.67, 47.09,
46.70, 46.63, 46.38, 47.30, 45.91, 45.84, 45.22, 44.90, 43.91, 43.19,
43.21, 45.93, 46.02, 46.60, 46.12, 47.88, 46.32, 45.27, 44.32, 43.87,
44.23,42.95, 41.74, 40.69, 41.68, 40.73, 40.75, 40.55, 39.39, 39.27, 40.89,
41.22, 40.57, 40.43, 40.58, 39.93, 41.08, 40.00, 37.64, 37.46, 37.16,
36.76, 35.65, 36.31, 37.32, 35.55, 34.98, 34.72, 34.55, 36.12, 36.76,
37.62, 36.36, 37.88, 36.59, 37.13}
Write a code in C to:
(a) Find the least square fit equations for linear and quadratic regression.
(b) Find the average error between the actual value and regression for both linear and quadratic case.
Explanation / Answer
a)
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n,i,x[64],y[64],x=0,y=0,xy=0,x2=0;
float a,b;
printf(" Enter the value of number of terms n:");
scanf("%d",&n);
printf(" Enter the values of x: ");
for(i=0;i<=n-1;i++)
{
scanf(" %d",&x[i]);
}
printf(" Enter the values of y:");
for(i=0;i<=n-1;i++)
{
scanf("%d",&y[i]);
}
for(i=0;i<=n-1;i++)
{
x=x +x[i];
x2=x2 +x[i]*x[i];
y=y +y[i];
xy=xy +x[i]*y[i];
}
a=((x2*y -x*xy)*1.0/(n*x2-x*x)*1.0);
b=((n*xy-x*y)*1.0/(n*x2-x*x)*1.0);
printf(" The line is Y=%3.3f +%3.3f X",a,b);
return(0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.