#!perl # ********************************************************************************************************************************* # # 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: February 16, 2002 # # Language: Perl # # 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. # # ********************************************************************************************************************************* # --------------------------------------------------------------------------------------------------------------------------------- # sqr() - Return the square of a number. # --------------------------------------------------------------------------------------------------------------------------------- sub sqr { $_[0] * $_[0]; } # --------------------------------------------------------------------------------------------------------------------------------- # Main program # --------------------------------------------------------------------------------------------------------------------------------- print "LINREG - Perform linear regression\n"; # print introductory message print " (Enter 'end' to stop data entry and compute linear regression.)\n"; # --------------------------------------------------------------------------------------------------------------------------------- # Read in x and y data and accumulate sums. # --------------------------------------------------------------------------------------------------------------------------------- while (1) { # loop for all data points print "\nEnter x: "; # prompt for x chomp($line = ); # read in line if ($line eq 'end') { # check for 'end' last; # if so, break out of loop } $x = $line; # else assign input to x print "Enter y: "; # prompt for y chomp($line = ); # read in line $y = $line; # assign input to line $n++; # increment number of data points by 1 $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 } # --------------------------------------------------------------------------------------------------------------------------------- # Compute and print results. # --------------------------------------------------------------------------------------------------------------------------------- $m = ($n * $sumxy - $sumx * $sumy) / ($n * $sumx2 - sqr($sumx)); # compute slope $b = ($sumy * $sumx2 - $sumx * $sumxy) / ($n * $sumx2 - sqr($sumx)); # compute y-intercept $r = ($sumxy - $sumx * $sumy / $n) / # compute correlation coefficient sqrt(($sumx2 - sqr($sumx)/$n) * ($sumy2 - sqr($sumy)/$n)); printf "\nSlope m = %15.6e\n", $m; # print results printf "y-intercept b = %15.6e\n", $b; printf "Correlation r = %15.6e\n", $r;