Preparation: Before attempting this lab, you should have made a careful study of the background material available in the folder for this week's lab.
In this assignment, we will be finding least squares fits for selected data points as described in the background. Other than the form of the design matrix D, all the formulae for implementation remain the same!
Questions 1 and 2: Find the parameters and (intercept and slope) in the equation of the line
through the following two points.
Design your code, so it will work for any two points, just by changing the values for and . Use the following technique, which is as easy as 1-2-3!
1. First enter , , and and define the parameter vector and the observation vector y.
Note one of these is symbolic!
2. Define the design matrix:
3. Solve for the parameter vector using the
inverse
of D.
If these two points are on the line then:
or
Thus the parameter vector is:
Questions 1 and 2: Paste your code here.
The intercept for this line is _ _ _ The slope for this line is _ _ _
Question 3: Plot the two points and the line that goes through them in the same figure.
Label both axes, add an appropriate title, and show both points as yellow circles with blue edges.
Place your finished plot here.
Question 4: The above approach will break if you have more than two points. The design matrix D with n points is not even square, so you cannot find its inverse as we did above.
But before looking at the case of three or more points, let's verify that using the least squares matrix
gives the same line we just found in the case of the two points. How can we solve the equation , if D is not even square?
Use either of these two find the transpose of a matrix D.
· 1. D'
· 2. transpose(D)
First multiply both sides by obtaining:
Now multiply by and exchange sides to find:
Implement this alternative approach in MATLAB, and verify you get exactly the same line you found previously. Hint: First find the matrix:
D2 = transpose(D) * D
Question 4: Paste your code here to find the parameter vector using:
Are the intercept and slope the same as before?
The intercept for this line is _ _ _ The slope for this line is _ _ _
Question 5: Case of three collinear points.
These points were carefully constructed so they do in fact, lie on a line. The system of equations is consistent. Find the parameters and (intercept and slope) in the equation of the line through the following
three
points.
A third point!
Use the following technique, which is as easy as 1-2-3!
1. First enter , , , , and and define the parameter vector and the observation vector y. Note one of these is symbolic!
We need a third row in both y and D for the third point!
2. Define the design matrix:
3. This time, D is not square so we can't find its inverse.
Let's use
row reduction
instead. Form the augmented matrix
Now find RAM, the row-reduced form of the augmented matrix using rref(). Finally, extract the parameter vector from RAM using:
>> b = RAM(:, 3)
Question 5: Paste your reduced matrix RAM in here. You should be able to see the parameter vector in the last column.
RAM = _ _ _
The intercept for this line is _ _ _ The slope for this line is _ _ _
Here, for free, is a plot showing the line through our three collinear points.
Now
repeat
the calculation using the least-squares formula .
You should get the same answer. Don't skip this. Confirm they are the same now!
Question 6: Case of three non-collinear points.
Ack! Now the points no longer lie on a line. The system of equations is inconsistent. Notice I just nudged the third point up a little higher.
It's a little higher than before!
Repeat your calculations from the previous exercise, but now the system is
inconsistent
.
You should be able to see a poison pivot in the last column of RAM!
Question 6: Paste your reduced matrix RAM in here.
Circle or highlight the pivot in the last column (poison pivot).
The system is inconsistent. There is no solution.
Question 7: Case of three non-collinear points. Find the best-fit line!
Even though these points do not lie on a line, there is however a best-fit line which minimizes the "square error". To find this line, simply use the very same least square formula we used previously.
Repeat the calculation using the least-squares formula .
Question 7: Record the intercept and slope for the best-fit line through the three non-colinear points.
The intercept for this line is _ _ _ The slope for this line is _ _ _
Questions 8-10: Random Motion
Copy the code on the last page into a new section of your script for today's lab.
This starts off with five points equally spaced along the x-axis. Thus they initially lie on a line with intercept zero and slope zero. Then each point is nudged randomly, as if they were moving under Brownian motion (like pollen grains in water seen under a microscope). Run the file to see the five points as they slowly drift about randomly.
But there is a problem! The animation is supposed to update the best-fit line but it just stays motionless along the x-axis. Study the code to see how it works. Then add one or two lines at the indicated spot, so the best-fit line updates every time the points drift.
Question 8: Paste in just the code you added, to make the best-fit line track the randomly moving points. It's just one or two lines of code.
Question 9: Replace this image with a new image from your animation which clearly shows your best-fit line is tracking the points as they drift randomly.
The best-fit line does
not
update as the points drift randomly! You are asked to fix this problem.
Question 10: Net Drift Direction
The random drift of the points is modeled in the inner for loop:
for k=1:5
angle = 360*rand(1);
dx = radius*cosd(angle); dy = radius*sind(angle);
x(k) = x(k) + dx; y(k) = y(k) + dy;
end
The direction (in degrees) a point moves is determined by the angle: angle = 360*rand(1);
Because of the 360, the particles randomly move in
all
directions, for a net average drift of zero.
10. Match each of these variations, with the average direction the particles drift.
Question 10: Circle the net direction the particles drift for each modified line of code.
(All must be correct to earn the point!)
angle = 180*rand(1); Up Down Left Right
angle = 180*rand(1) + 180; Up Down Left Right
angle = 180*rand(1) - 90; Up Down Left Right
angle = 180*rand(1) + 90; Up Down Left Right
When your lab is complete, be sure to submit it as a PDF by the due date – this Thursday before 11:59PM. You have one more day, to submit the lab (but with a small penalty), and then the window closes for good and your grade will be zero. Only one submission is allowed per student.
Animation of Best-Fit Line as Five Points Move Randomly
%% Animation of best-fit line as points move about randomly.
% Five points start out exactly on the line y = 0.
clear, clc, close all
x1 = -10; x2 = -5; x3 = 0; x4 = 5; x5 = 10;
x=[x1; x2; x3; x4; x5];
y = zeros(size(x)); % The points start off on the x-axis.
b = [0; 0] % The initial slope and intercept are zero.
x_points = -20:0.1:20; % Points for plotting the best-fit line.
y_points = b(1) + b(2)*x_points;
figure
best_line_handle = plot(x_points, y_points, 'b:', 'LineWidth', 3);
xlabel('x-axis')
ylabel('y-axis')
title('Animated Best-Fit Line')
set(gca, 'fontsize', 20)
axis square
grid on
hold on
point_handle = plot(x, y, 'bo', 'MarkerFaceColor', 'yellow', 'MarkerSize', 16);
axis([-20 20 -10 10])
% Move each point in a random direction.
radius = 0.25 % Each point will move this distance.
for i = 1:100
pause(0.1) % We'll make a fun animation.
for k=1:5
angle = 360*rand(1);
dx = radius*cosd(angle); dy = radius*sind(angle);
x(k) = x(k) + dx; y(k) = y(k) + dy;
end
% Update points, the design matrix and obervation vector.
D = [ones(5,1), x];
% Find the new best-fit curve.
% Add code here to update the parameter vector b.
point_handle.delete()
best_line_handle.delete()
y_points = b(1) + b(2)*x_points;
best_line_handle = plot(x_points, y_points, 'b:', 'LineWidth', 3);
point_handle = plot(x, y, 'bo', 'MarkerFaceColor', 'yellow', 'MarkerSize', 16);
end
8
Copyright 2016 Drexel University
-20 -10 0 10 20
x-axis
-10
-5
0
5
10
y-
a
xi
s
Animated Best-Fit Line
-20 -10 0 10 20
x-axis
-10
-5
0
5
10
y
-
a
x
i
s
Animated Best-Fit Line
-10 0 10 20
x-axis
5
10
15
20
25
30
y-
a
xi
s
The Line through Three Colinear Points
y = 1 + 2x
-10 0 10 20
x-axis
5
10
15
20
25
30
y
-
a
x
i
s
The Line through Three Colinear Points
y = 1 + 2x
-10 0 10 20
x-axis
5
10
15
20
25
30
y-
a
xi
s
Three Non-Colinear Points
Best-Fit Line
-10 0 10 20
x-axis
5
10
15
20
25
30
y
-
a
x
i
s
Three Non-Colinear Points
Best-Fit Line