Math 232 – Computing Assignment 4Due Date: Dec 2, at 11:00pm.You must upload to Crowdmark both your code (as a .pdf file) (to Code - Computing Assignment3) and your report (to Report - Computing...

1 answer below »
MATLAb assignment and i need 2 pages , one with the matlab code and other with the report in a word file . strictly just 2 pages


Math 232 – Computing Assignment 4 Due Date: Dec 2, at 11:00pm. You must upload to Crowdmark both your code (as a .pdf file) (to Code - Computing Assignment 3) and your report (to Report - Computing Assignment 3). The assignment is due at 11:00pm. If Crowdmark indicates that you submitted late, you will be given 0 on the assignment. Your computing report must be exactly 1 page. There will be a penalty given if your report is longer than one page. • Please read the Guidelines for Computing Assignments in Canvas first. • Keep in mind that Canvas discussions are open forums. • Acknowledge any collaborations and assistance from colleagues/TAs/instructor. Programming Preamble: Matlab: x=[1 1 1]’ produces a column vector. The ’ indicates transpose. Matlab: help plot gives you all the information you need to make your plots look good - labelling the axes, putting on a title etc. Computing Assignment Required submission: 1 page PDF report and Matlab or Python code (.m or .py respectively, exported as a .pdf) uploaded to Canvas. Before you begin, consult the ’Least Squares preamble’ file posted on Canvas Machine Learning This assignment will give you a glimpse into one of the main areas of Machine Learning (Data Science); that of regression and classification. We study two modelling examples that use historical data on the behaviour of a specified group of people to make predictions on their future behaviour. In particular, we study student success in Math 232 based on their study habits (number of hours spent studying Math 232) and their grades in previous mathematics courses. (Note: all the data here is purely hypothetical.) 1. Our first approach to this problem will use (hypothetical) historical data on the number of hours per week students spent studying math 232 in a previous semester, and their subsequent grade in the course. What we want to obtain from this data is a criteria for “success” in math 232 (i.e., grade > 50%) based solely on how much time a student spends studying the course materials per week. 1 Data for this assignment is contained in the excel file CA4data.xslx that is posted on Canvas. This (hypothetical) data set contains the number of hours per week students spent studying math 232 during a past semester, their grade in a previously taken math course (math 152, taken before they took math 232), and their final grade in math 232 in that semester. In Q1 below, we will use only the studying hours, and Q2 below will use both the studying hours and the grades in math 152 to build a model from which we can make predictions about student ‘success’ in math 232. Read in the data file CA4data.xslx that is available for download on Canvas. Open the file in excel and remove the first line (which labels the columns). Use these commands to create (column) vectors with data taken from the columns in the file; >> data = ’CA4data.xlsx’; >> D=xlsread(data); >> h=D(:,1); >> k=D(:,2); >> g=D(:,3) h,k,g are column vectors with the data #hours studied per week, grade in math 152, and the grade received in math 232, respectively. The data set for the first question is D1 = {(hi, gi), i = 1, . . . , 45}. We look for a linear model of the form g = a+bh. That is, we look for the best fitting (least squares) line through the data points D1 in R2. 1a. Let the function gg=a+bx (Matlab notation) be your best fit linear model through the data (here you have to first compute what those parameters a, b are using our theory of least squares approximation). The Matlab command >> x=0:0.2:12; >> gg=a+b*x; plot(x,gg) will plot this line. Plot your best fit linear line along with the data set using these commands; >> plot(h,g,’k.’) >> hold on >> plot(x,gg) >> hold on >> axis([0 15 20 100]) >> hold on >> xlabel(’hours per week’); ylabel(’grade’) From your linear model of hours spent studying vs final grade in math 232, what do you predict is the “success” point; that is, the (minimum) number of hours a student should 2 spend per week studying math 232 in order to pass the course? (this will be a point ho on the h−line (‘hours per week’) where h ≥ ho indicates a grade of at least 50% will be obtained). 1b. Now find the best fit quadratic model to the data ; g = a+ b1h+ b2h 2. That is, the least squares quadratic that fits the data. Plot this quadratic along with the data points. From this quadratic find the predicted success rate. (Notice here that although we are fitting a nonlinear function to the data set, the least squares algorithm is still a linear problem!) 2. In this second approach we include another ’diagnostic’ from the group of students; that of their previously taken math 152 grade. So now we use both the number of hours they spent studying math 232 and their grade in math 152 to make a prediction on student success in math 232. Thus, our data set isD2 = {(hi, ki, gi), i = 1, . . . , 45}. You can take a look at this 3-dimension scatter plot with the commands; >> plot3(h,k,g,’k.’) >> grid on (Note that you can drag the mouse on the figure to rotate it and obtain different perspectives). Our linear model for this data set is g = a + bh + ck. That is, we look for the best fitting plane through the data points in R3. Compute (using the algorithm discussed in class) the least squares plane that fits the data points. You can plot the plane with the data points using these commands; >> plot3(h,k,g,’k.’) >> axis([0 15 20 100 20 100]) >> grid on >> hold on >> [X,Y]=meshgrid(x,y); >> ggg=a+b*X+c*Y; >> mesh(ggg) where a+b*X+c*Y is your least squares fitting plane to the data. From this determine the ‘success line’ in the h, k-plane; one side of this line predicts the student will receive a grade of ≥ 50%. Plot this line in the h, k-plane indicating the ‘success’ side of it. (Hint: Recall that the intersection of two planes is a line (in 3 dimensions); then project this line down into the h, k-plane.) (Remark: In a similar way as one fitted the least squares quadratic curve through the 2 dimensional scatter plot as in Q1, one could here find the least squares quadratic surface g = ao + a1h+ a2k+ a3hk+ a4h 2 + a5k 2 through the 3 dimensional scatter plot. Again, it is a linear problem determining the parameters a1, . . . , a5. We will NOT be doing that as part of this assignment.) 3 Math 232 Preamble for CA4 - Least squares fitting We know that the system Ax = b is consistent if only if b is in the span of the columns of A (b ∈ col(A)). We can re-write the system Ax = b as b− Ax = 0, so that if the system is consistent and x is a solution, then ∥b− Ax∥ = ∥0∥ = 0. If b /∈ col(A) then there is no solution; there is no vector x such that Ax = b (the system is inconsistent). When the system is inconsistent Ax is never equal to b so ∥b− Ax∥ > 0 for all x In this case we look for a vector x̂ that makes ∥b−Ax̂∥ as small as possible (so Ax̂ is ‘close’ to b). In this sense we have an ‘approximate’ solution to Ax = b; ∥b− Ax̂∥ ≤ ∥b− Ax∥ for all x This x̂ will be our best approximate solution to the inconsistent system Ax = b. To summarize, we determined a way to ‘solve’ the system Ax = b when it is inconsistent (b /∈ col(A)) by solving instead the (consistent) system Ax = b̂ where b̂ is the closest vector to b in col(A). This gives us the least squares solution of Ax = b (this is the definition of least squares solution to a linear system of equations.) Let’s observe also that if the system Ax = b is consistent, this method finds the exact solution (right? because then b̂ = b). It remains to solve Ax = b̂. Recall that x = projWx + projW⊥x = x1 + x2 for any vector x ∈ Rn and any subspace W ⊂ Rn. So we write b = b̂ + b2 = projWb + projW⊥b where W = col(A). And then we observe that W⊥ = col(A)⊥ = null(AT ). Since b2 ∈ W⊥ (right?), we have that ATb2 = 0. Writing b2 = b− b̂ = b− projcol(A)b, we obtain AT (b− projcol(A)b) = A T (b− Ax̂) = 0 and re-arranging this we obtain ATAx̂ = ATb (1) Equation (1) is called the normal equation for x̂. Note that ATA and ATb are ‘easy’ to compute, so solving this system (via row reduction) would be easier than solving Ax̂ = b̂. 1 Now, if A is m× n, then ATA is n× n so it may be invertible. If it is then we can solve (1) for x̂; x̂ = (ATA)−1ATb (2) and we have a unique least squares solution x̂. If ATA is singular (non-invertible), then there are many least squares solutions. There are many, many applications of least squares solutions (statistics, engineering, finance, ....). Example 1: A =  1 −13 2 −2 4  , b =  41 3 . Check that b /∈ col(A), so the system Ax = b is inconsistent. Let’s find the least squares solution(s). Step 1: Compute ATA; ATA = [ 1 3 −2 −1 2 4 ]  1 −13 2 −2 4  = [ 14 −3−3 21 ] . Step 2: det(ATA) ̸= 0 so (ATA) invertible and therefore there is only one least squares solution. Compute (ATA)−1; (ATA)−1 = 1 285 [ 21 3 2 14 ] Step 3: Compute x̂; x̂ = (ATA)−1ATb = 1 285 [ 21 3 2 14 ] [ 1 3 −2 −1 2 4 ]  41 3  = [ 17/95 143/285 ] This is the least squares solution. b̂ = Ax̂ = (−0.323, 1.540, 1.65). It may seem like b̂ is far from b, ∥b̂−b∥ = 4.56, but b̂ is still the closest vector to b in col(A), and we used it, b̂, instead of b to solve the consistent system Ax = b̂ and found the solution x̂. 2 Example 2: A =  3 2 −11 −4 3 1 10 −7  , b =  2−2 1 . Again, b /∈ col(A). ATA =  11 12 −712 120 −84 −7 −84 59  , ATb =  522 −15  Here, det(ATA) = 0 so we have to solve the normal equations ATAx = ATb by row reduction to find x̂;  11 12 −7 512 120 −84 22 −7 −84 59 −15  −→  1 0 1 7 2 7 0 1 −5 7 13 84 0 0 0 0  The solution set is x̂ = v0 + tv1, v0 = ( 2 7 , 13 84 , 0), v1 = (− 1 7 , 5 7 , 1) and so there are many least squares solutions. For each of them we have that Ax̂ = b̂ = (5, 22,−15). Examples for Computing Assignment 4; Start here! Example 3: Finding the least squares curve to a set of data points in R2. We are given a set of data points (x1, y1), (x2, y2), . . . , (xn, yn) where we are considering the x as inputs and the y as outputs; so, one input and one output. We ask: What is the relation between the inputs and outputs? That is, can we find a function f(x) such that the data set is represented by y = f(x)? To begin addressing that question, we first look for a linear function y = f(x) = a+ bx, whose graph (a striaght line) passes close to all the data points. That is, we want to determine a and b so that the total (vertical) ‘error’ between the line y = a+ bx and the data points is smallest; residual error = [y1 − (a+ bx1)]2 + [y2 − (a+ bx2)]2 + · · · + [yn − (a+ bxn)]2 3 Notice that if all the data points did lie on the same line, y = a+ bx, then y1 = a+ bx1 y2 = a+ bx2 ... ... yn = a+ xn which in matrix form would be a system of equations; 1 x1 1 x2 ... ... 1 xn  [ a b ] =  y1 y2 ... yn  ; Mv = y In other words, this system of equations, Mv = y, would be consistent since y ∈ col(M), and we could solve it to determine v = (a, b) and get the equation for the straight line. However, if the data points do not lie along a straight line, then the system Mv = y is inconsistent, and so we look for a least squares solution. The line y = a + bx we find this way is called the least squares line of best fit (linear regression) and if you plot this line with the date, it appears to be the ‘best’ fitting straight
Answered 1 days AfterNov 30, 2022

Answer To: Math 232 – Computing Assignment 4Due Date: Dec 2, at 11:00pm.You must upload to Crowdmark both...

Baljit answered on Dec 01 2022
42 Votes
C:\...Downloads\a114990.m
12/1/22 8:39 AM C:\Users\User\Downloads\a114990.m 1 of 2
clc;
clear all

close all
format long
data='CA4DATA.xlsx'
D=xlsread(data);
h=D(:,1);
k=D(:,2);
g=D(:,3);
%for part...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here