/*****************************************************************************/ /* */ /* L I N R E G */ /* */ /* Program: LINREG */ /* */ /* Programmer: Dr. David G. Simpson */ /* Department of Physical Science */ /* Prince George's Community College */ /* Largo, Maryland 20774 */ /* */ /* Date: January 21, 2002 */ /* */ /* Language: C */ /* */ /* Description: This program performs a linear regression analysis for a */ /* set of data given as (x,y) pairs. The output from the */ /* program is the slope and y-intercept of the least-squares */ /* best fit straight line through the data points. */ /* */ /*****************************************************************************/ /*****************************************************************************/ /* #includes */ /*****************************************************************************/ #include /* standard i/o */ #include /* string functions */ #include /* math functions */ /*****************************************************************************/ /* function prototypes */ /*****************************************************************************/ void chop (char *str); /* remove \n from end of string */ double sqr (double x); /* compute the square of a number*/ /*****************************************************************************/ /* global variables */ /*****************************************************************************/ double b; /* y-intercept of best fit line */ double m; /* slope of best fit line */ double n = 0.0; /* number of data points */ double r; /* correlation coefficient */ char str[81]; /* input string */ double sumx = 0.0; /* sum of x */ double sumx2 = 0.0; /* sum of x**2 */ double sumxy = 0.0; /* sum of x * y */ double sumy = 0.0; /* sum of y */ double sumy2 = 0.0; /* sum of y**2 */ double x; /* input x data */ double y; /* input y data */ /*****************************************************************************/ /* main() */ /*****************************************************************************/ int main (void) { /*---------------------------------------------------------------------------*/ /* Print introductory message. */ /*---------------------------------------------------------------------------*/ printf("LINREG - Perform linear regression\n"); printf(" (Enter END to stop data entry and " "compute linear regression.)\n\n"); /*---------------------------------------------------------------------------*/ /* Enter data and accumulate sums. */ /*---------------------------------------------------------------------------*/ for (;;) /* loop for all data points */ { printf ("Enter x y: "); /* prompt for x and y */ fgets (str, 80, stdin); /* read x and y into string str */ chop (str); /* remove trailing \n */ if (!strcmp(str,"end") || /* if no more data.. */ !strcmp(str,"END")) break; /* ..then go compute least sqrs */ sscanf (str, "%lf %lf", &x, &y); /* else read data from string */ n += 1.0; /* increment num of data points */ sumx += x; /* compute sum of x */ sumx2 += x * x; /* compute sum of x**2 */ sumxy += x * y; /* compute sum of x * y */ sumy += y; /* compute sum of y */ sumy2 += y * y; /* compute sum of y**2 */ } /* loop again for more data */ /*---------------------------------------------------------------------------*/ /* Compute least-squares best fit straight line. */ /*---------------------------------------------------------------------------*/ m = (n * sumxy - sumx * sumy) / /* compute slope */ (n * sumx2 - sqr(sumx)); b = (sumy * sumx2 - sumx * sumxy) / /* compute y-intercept */ (n * sumx2 - sqr(sumx)); r = (sumxy - sumx * sumy / n) / /* compute correlation coeff */ sqrt((sumx2 - sqr(sumx)/n) * (sumy2 - sqr(sumy)/n)); /*---------------------------------------------------------------------------*/ /* Print results and return to operating system. */ /*---------------------------------------------------------------------------*/ printf("\nSlope m = %15.6e\n",m); printf("y-intercept b = %15.6e\n",b); printf("Correlation r = %15.6e\n",r); return 0; /* return to operating system */ } /*****************************************************************************/ /* chop() */ /* */ /* Remove final \n from end of string. */ /*****************************************************************************/ void chop (char *str) { int len; /* length of str (incl \n) */ len = strlen (str); /* get length of str incl \n */ if (str[len-1] == '\n') /* if final char is \n .. */ str[len-1] = '\0'; /* ..then remove it */ } /*****************************************************************************/ /* sqr() */ /* */ /* Returns the square of a number. */ /*****************************************************************************/ double sqr (double x) { return x * x; /* compute square of argument */ }