Linear Regression Fit to Data
Purpose: Fit data using linear regression least squares method for both a Linear function and a Power Law function.
Algorithm. Write a program named that will enable the user to fit data to both a linear and a power law function.
Here is a data set for CO2 levels in the World over a range of years.
year | 1960 | 1965 | 1970 | 1975 | 1980 | 1985 | 1990 | 1995 | 2000 | 2005 |
CO2 level ppm | 316.5 | 320 | 325 | 331 | 338 | 346 | 353 | 360 | 368.5 | 378 |
The supplied data file you will use for the fits, CO2.txt shown below, is the same data as above but 1960 has been subtracted from each of the years and 316.5 has been subtracted from each CO2 level. In other words, 1960 is the start year and levels are measured relative to what it was in that year. This is done to reduce the size of products and sums that occur in the least-squares formulas and increase to accuracy of the results. CO2.txt
year | 5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 |
CO2 level ppm | 3.5 | 8.5 | 14.5 | 21.5 | 29.5 | 36.5 | 43.5 | 52 | 61.5 |
- Fit to a Linear function y = mx + c
In the linear case you are fitting the CO2.txt data to the straight line y = mx + c, where y represents the CO2 level and x represents the year in the file CO2.txt and m and c are constants. Solve these equations using the least-squares linear regression formulas in the lecture notes to get to get solutions for m and c. Using the fit, offer the user the option of interpolating/extrapolating the data by including the (1960, 316.5) offsets to the formula you calculate.
- Fit to a power law y = axb.
In this case you are fitting the data to the power law y = axb where y represents the CO2 level and x represents the year in the file CO2.txt and a and b are constants. To do a linear regression the data need to be transformed as dicussed in class notes. With the transformed data solve the least-squares linear regression formulas in the lecture notes to get solutions for a and b. Then using the power law formula, offer the user the option of interpolating/ extrapolating the data by including the (1960, 316.5) offsets to the formula you calculate.
Set up a Win32 console project in Visual Studio 2015 with the name ass1. In a file named ass1.cpp using the C (or/C++) programming language, write the code to implement the application, as described above,. Example output is given at the end. Yours should be the same. Note than your assignment might be
tested with a different data file with a different number of entries and with different interpolation/extrapolation parameters than those shown.
See the Marking Sheet for how you can lose marks, but you definitely lose 60% or more if:
- your application wont build in Visual Studio 2015
- your application crashes in normal operation or with a different data file
- I cant build it because you submitted the wrong files or the files are missing, even if its an honest mistake 100% deduction.
1
CST 8233 F18 Assignment #2 A.Tyler
What to Submit : Use Brightspace to submit this assignment as a zip file (not RAR, not 9zip, not 7 zip) containing only the source code file (ass2.cpp). The name of the zipped folder must contain your name as a prefix so that I can identify it, for example using my name the file would be tyleraAss2CST8233.zip. It is also vital that you include the Cover Information (as specified in the Submission Standard) as a file header (you dont need to include an additional separate file header) in your source file so the file can be identified as yours. Use comment lines in the file to include the information.
There is a late penalty of 25% per day even one minute is counted late. Dont send me the file as an email attachment it will get 0.
Example Output
LEAST_SQUARES LINEAR REGRESSION
MENU
- Linear Fit
- Power Law Fit
- Quit
2
Please enter the name of the file to open: CO2.txt
There are 9 records.
y = 0.43x^1.31
MENU
- Interpolate/Extrapolate
- Main Menu
1
Please enter the year to interpolate/extrapolate to: 2020 The power law interpolated/extrapolated CO2 level in the year 2020.0 is 405.99 MENU
- Interpolate/Extrapolate
- Main Menu
1
Please enter the year to interpolate/extrapolate to: 1968 The power law interpolated/extrapolated CO2 level in the year 1968.0 is 322.94 MENU
- Interpolate/Extrapolate
- Main Menu
2
LEAST_SQUARES LINEAR REGRESSION
MENU
- Linear Fit
- Power Law Fit
- Quit
1
Please enter the name of the file to open: CO2.txt
There are 9 records. y = 1.45*x + -6.18
MENU
- Interpolate/Extrapolate
- Main Menu
1
Please enter the year to interpolate/extrapolate to: 2020 The linear interpolated/extrapolated CO2 level in the year 2020.0 is 397.42
MENU
- Interpolate/Extrapolate
- Main Menu
1
Please enter the year to interpolate/extrapolate to: 1968 The linear interpolated/extrapolated CO2 level in the year 1968.0 is 321.93
MENU
- Interpolate/Extrapolate 2. Main Menu
2
Reviews
There are no reviews yet.