Professional Documents
Culture Documents
47
Professor Daddel
MAT167
Program 1
3
4
5
6
In figure 1, the least squares regression a non-linear equation. The least squares
regression does not appear to have a pattern. However, by looking at the residuals we can see
that there is some sort of pattern. There is an outlier and by removing the outlier, we can see that
the pattern follows a sin function.
Program 2
3
4
The image with its entire matrices shows a fully colored mandrill with a red nose and
grey hair. The approximation (figure 2) shows that as there’s more information given to the
residual (figure 3), the approximation becomes blurrier. It makes sense since the approximation
and the residual are conjugate to each other. The approximation and the residual are complement
to each other.
%Program 1 title('Least Squares Line Fit: Outlier Removed');
xlabel('t'); ylabel('y')
%Load data
load data.mat %Solve for a+bt+csin(t) with inverse
D = [t1.^0 t1.^1 sin(t1)]
whos sol4 = inv(D'*D)*D'*y1
%Plot the original x and y with grid %Plot the graph
figure(1) p2 = sol4(1)+sol4(2)*t1+sol4(3)*sin(t1)
plot(x,y); grid figure(6);
hold on; plot(t1,y1,'o');
%Solve for a+bt form using inverse hold on; plot(t1,p2,'-');
%Plot the equation with plot and label hold on; plot(t(7),y(7),'*')
A=[x.^0 x.^1]
sol = inv(A'*A)*A'*y %Program 2
hold on; plot(x, sol(1)+sol(2)*x,'-')
title('Least Squares Line Fit'); xlabel('x'); %Load the data
ylabel('y') load mandrill2.mat
%Load data %Plot the image and use colormap to show the color
load data2.mat figure(7)
hold on; image(X); colormap(map)
%Create the transpose of t and y
y = y' %Solve for U S V with svd function
t = t' [U,S,V]=svd(X)
whos %Plot the diagonal
figure(9)
%Plot the t and y on the graph plot(diag(S))
figure(2)
plot(t,y);grid %Solve for left and right singular using svd
function
%Solve for a+bt with inverse singular = svd(X,0)
B=[t.^0 t.^1] left_singular = svd(U*S*V', 'econ')
sol2= inv(B'*B)*B'*y right_singular = svd(U*S*V')
hold on; plot(t, sol2(1)+sol2(2)*t,'-')
title('Least Squares Line Fit: Original Date'); %Using a for loop, create a function to solve for k
xlabel('t'); ylabel('y') %Create the SVD matrix
l = 1
%Solve for a+bt+csin(t) with inverse for i = 1:4
%Create a new variable to account for when we k = 5*(i-1)+l
remove the outlier approx = U(:,1:k)*S(1:k,1:k)*V(:,1:k)'
R=y-(sol2(1)+sol2(2)*t) subplot(2,2,i)
R1 = R image(approx); colormap(map);
t1 = t l = k
y1 = y end
%p1 = polyfit(t,y,1)
%Using a for loop, create a function to solve for k
%Plot the residual %Subtract X from approximation to create residual
figure(5) %Plot residual
hold on; plot(t,R,'o') figure(9)
hold on; refline(0,0) l = 1
hold on; title('Residual Plot with Original Data') for i = 1:4
k = 5*(i-1)+l
%Remove the index where the outlier is at approx = U(:,1:k)*S(1:k,1:k)*V(:,1:k)'
R1(7)=[] residual = X - approx
t1(7)=[] subplot(2,2,i)
y1(7)=[] image(residual); colormap(map);
l = k
%Plot the residual with outlier removed end
figure(3)
plot(t1,R1, '-'); %Using a for loop, create a function to solve for k
hold on; refline(0,0); %Plot approximation
hold on; title('Residual Plot with Outlier figure(10)
Removed') l = 1
for i = 1:4
%Plot the data with the outlier removed k = 5*(i-1)+l
figure(4) approx =
plot(t1,y1);grid U(:,1:(k+1))*S(1:(k+1),1:(k+1))*V(:,1:(k+1))'
subplot(2,2,i)
%Solve for a+bt with inverse image(approx); colormap(map);
%Plot the least squares line l = k
C=[t1.^0 t1.^1] end
sol3=inv(C'*C)*C'*y1
hold on; plot(t1, sol3(1)+sol3(2)*t1,'-')