Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Let\'s try this again (part 1). IF YOU DON\'T KNOW MATLAB, DON\'T ANSWER THIS IN

ID: 3834525 • Letter: L

Question

Let's try this again (part 1). IF YOU DON'T KNOW MATLAB, DON'T ANSWER THIS IN A DIFFERENT LANGUAGE. PLEASE WRITE THE CODE SO I CAN SIMPLY PUT IT INTO MATLAB AND SEE IF IT WORKS.

6.27 Linear Least Squares Fit Develop a function that will calculate slope m and intercept b of the least-squares line that best fits an input data set. The input data points (x. y will be passed to the function in two input arrays, x and y. The equations describing the slope and intercept of the least-squares line are in Example 5.6 in the previous chapter) Test your function using a test program and the following 20-point input data set: Sample Data to Test Least Squares Fit Routine No. No. 0.21 4.91 8.18 11 0.94 1.73 12 3.84 7.49 0.59 0.69 3.96 7.11 2.41 13 6.15 14 3.04 4.26 2.62 6.75 1.01 3.78 6.62 15 0.52 3.30 16 6.67 3.60 4.53 7.70 1.83 17 2.05 7.31 6.13 2.01 2.83 18 9.05 0.28 4.43 1.16 19 10.95 20 4.12 10 1.08 0.52

Explanation / Answer

Matlab code:

function [ slope, intercept ] = fitLine( X, Y )
average_x = sum(X,2);
average_y = sum(Y,2);
slopeNum = 0.0;
slopeDen = 0.0;
for i=1:size(X,1)
slopeNum = slopeNum + (Y(i,1)- average_y)*(X(i,1)- average_x);
slopeDen = slopeDen + (X(i,1)- average_x)*(X(i,1)- average_x);
end
slope = (slopeNum*1.0)/slopeDen;
intercept = average_y - slope*average_x;
end