The purpose of this program is to use the method of least
squares (linear regression) to calculate the m, b and r values for a given set
of data points. The given data represents information about the volume of water
through a culvert, as a function of the height of the water in the culvert.
Program requirements:
-
Use a main program and a Subroutine subprogram.
-
In the main program, read from the given file
(LeastSqrs.txt): on line 1 of the file, read a number which equals the number
of data pairs, n (n will never be more than 100); then starting on line 2 of
the file the data pairs are listed, (x, y), one per line. Read the data pairs into one-dimensional
arrays (one array will contain the x values and the other the y values).
-
In the main program, ask the user if they want you to
calculate the regression equation for a linear, exponential or power situation.
-
Based on the users’ response, calculate and send to the
Subroutine the correct form of the x and y values (you may need Lny, etc.).
-
In the main program, output to the screen the m, b and
r values that are returned from the Subroutine, with the m and b values
displayed in the form of the correct general equation (Linear, Exponential or
Power).
-
Use only ONE
Subroutine to calculate the m, b and r values using the Method of Least Squares
to do Linear Regression (equations on back of sheet).
-
In the main program output the Linear, Exponential or
Power equation with the calculated values of m and b inserted in the
equations. Also output the r values.
Notes about the LeastSqrs.txt input file:
-
On line one, read the value of n (the count of data
pair in the file).
-
Starting on line two, the data is given as X, Y
Linear Regression
m=(n(Σxy)-(Σx)(Σy))/(n(Σx2)-(Σx)2
b=Σy-m(Σx)/(n)
r=n(Σxy)-(Σx)(Σy)/(√n(Σx2)-(Σx)2)*((√nΣy2)-(Σy)2)
Exponential Regression:
y=b*e^(mx)
x values = x
y values = Ln(y)
b = Ln(b)
Power Regression:
y=b*x^(m)
x values = Ln(x)
y values = Ln(y)
b = Ln(b)
This is the LeastSqrs.Txt referred to above:
11
1.2, 0.06
1.4, 0.08
1.9, 0.15
2.3, 0.28
2.8, 0.40
3.5, 0.48
4.9, 1.25
6.7, 2.44
12.3, 5.15
15.8, 10.55
24.8, 15.28