The coefficients of the least-squares linear y = a x + b that fits to the follow
ID: 3807999 • Letter: T
Question
The coefficients of the least-squares linear y = a x + b that fits to the following data (this is generic, no numerical values are specified) are obtained by solving the system of equations Write a MATLAB code based on the formula above to determine a and b. Paste the code here. (No need to submit the m-file.) Use this code to fit the data points (total of 13) X: 1.0000 1.0667 1.1333 1.2000 1.2667 1.3333 1.4000 1.4667 1.5333 1.6000 1.6667 1.7333 1.8000 Y: 0.5000 0.5875 0.6837 0.7887 0.9029 1.0264 1.1596 1.3026 1.4557 1.6191 1.7930 1.9778 2.1735 to the curve y = c_1 x^, i.e., determine c_1 and c_2.Explanation / Answer
a.
function [a b errorAmount] = findCoefficients(x, y)
sumX = 0; sumY = 0; sumSqX = 0; sumXY = 0;
dataSize = length(x);
for i = 1:dataSize
sumX += x(i);
sumY += y(i);
sumXY += (x(i) * y(i));
sumSqX += (x(i) * x(i));
end
denominator = dataSize * sumSqX - (sumX * sumX);
a = (sumY * sumSqX - sumX * sumXY) / denominator;
b = (dataSize * sumXY - sumX * sumY) / denominator;
errorAmount = 0.0;
for i=1:dataSize
temp = y(i) - a - (b * x(i));
errorAmount = errorAmount + (temp * temp);
end
end
% Testing the above method
x = [-7 -5 -1 0 2 5 6];
y = [15 12 5 2 0 -5 -9];
[a b errorAmount] = findCoefficients(x,y);
printf("a = %f, b = %f, error = %f ", a, b, errorAmount);
b. Once you transform the equation to the v=au+b form, just call the above function (as i did in test data)or let me know, I will find it out for you.