CivEng 2E03 Fall 2020 – Lab #8 Instructions November 17/20, 2020 – Regression
– Download the CE2E03_Lab8_F2020.m AND data.txt files from Avenue under Labs → Lab 8
– NOTE: Make sure you save these files in the same folder!
PART 1: LOAD DATA
– Load the data provided in the data.txt file into a MATLAB variable named data using the dlmread() function
– Remember the dlmread() syntax is: data = dlmread(‘data.txt’)
– Create a new vector x, which contains the entire first column of data, i.e. x = data(:,1)
– Create a new vector y, which contains the entire second column of data
– Create a new variable n, which is the length of x
PART 2: LINEAR REGRESSION
– Write your own code to calculate the linear regression coefficients for the provided data, a0 and a1
– You CAN’T use polyfit() for this part
– The equations are as follows:
𝑎1 = 𝑛 ∑ 𝑥𝑖 𝑦𝑖 − ∑ 𝑥𝑖 ∑ 𝑦𝑖
𝑛∑𝑥2 −(∑𝑥𝑖)2 𝑖
𝑎 0 = 𝑦̅ − 𝑎 1 𝑥 ̅
– NOTE: you are encouraged to use the built in MATLAB functions sum() and mean() for calculating these coefficients.
– HINT: recall that if you want to do element by element operations on a matrix, you
include a period, i.e. xiyi = x.*y in MATLAB
– Using the a0 and a1 values you find, create a new vector y2 which represents the points
on the linear regression line for the given x values (i.e. y2 = a0 + a1x)
PART 3: POLYNOMIAL REGRESSION
– Using the built in MATLAB function polyfit(), generate a vector a which represents the coefficients of a quadratic polynomial (order 2) fit to the given data
– NOTE: recall from the prelab that polyfit() gives the coefficient output in DECREASING order (a(1) in MATLAB will be the coefficient of the x2 term)
– Use the coefficients found by polyfit() and create a new vector y3 which represents the points on the quadratic regression curve for the given x values (i.e. y3 = a0 + a1x + a2x^2)
PART 4: PLOT RESULTS
– Create a plot showing the original data, linear, and quadratic regressions
– Plot the original data as individual points (i.e. no line connecting)
– Remember the hold on command will tell MATLAB to plot multiple data series
– Plot the linear regression as a solid line (colour of your choice)
– Plot the quadratic regression as a solid line (colour of your choice)
– Include a legend to label the three data sets
– Turn on the grid (grid on)
Hand-In Submission
– Go to Avenue → Assignments
– Find the correct folder for Lab Assignment 8
– Submit ONLY the CE2E03_Lab8_F2020.m MATLAB file
– There is no required naming convention