1. Least Squares Fit to a Data Set by a Linear Function The following nine data
ID: 3858541 • Letter: 1
Question
1. Least Squares Fit to a Data Set by a Linear Function
The following nine data points are nearly linear and can be approximated by a linear function .
X
-1.0
0.0
2.1
2.3
2.4
5.3
6.0
6.5
8.0
Y
-1.02
-0.52
0.55
0.70
0.70
2.13
2.52
2.82
3.54
Enter the x and y coordinates of the data points as column vectors and respectively.
Set
Compute the least squares solution to the linear system using the method developed in class.
Suggestion: Set and . then will equal the last column of .
and then
Now try out the MATLAB “” operation which will do all the work for you and return the least squares solution automatically.
To see your least squares line graphically, set
and
then plot the original data points and the least squares linear fit, using the MATLAB command
‘x’ .
X
-1.0
0.0
2.1
2.3
2.4
5.3
6.0
6.5
8.0
Y
-1.02
-0.52
0.55
0.70
0.70
2.13
2.52
2.82
3.54
Explanation / Answer
deg = 1; % degree of polynomial is 1 which is linear x = [-1.0, 0.0, 2.1, 2.3, 2.4, 5.3, 6.0, 6.5, 8.0] y = [-1.02, -0.52, 0.55, 0.70, 0.70, 2.13, 2.52, 2.82, 3.54] pp = [0:1:deg] for i=1:length(y) for j=1:length(pp) A1(i,j)=x(i)^pp(j); end end a1 a2 = a1' b0=y for k=1:length(y)-1 b0=cat(1,b0,y); end b0 b1=b0' a3 = a2*b1 c1 = a2*a1 c2 = inv(c1) c3 = c2*a3 c4 = c3' pol = c4(1,1:length(pp)) c5 = a1*c4 yreg = sum(c5,2)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.