Você está na página 1de 9

4/25/2016

assignment_matrix_decomposition-Copy2

[WS14/15]MathematicsforRoboticsandControl:
Assignment004Matrixdecomposition
Firstwewillsetupthisnotebooksothatfiguresandplotscanbeshowninthenotebookpage.
In[93]:
try:
shell = get_ipython()
shell.enable_pylab("inline")
except NameError:
pass
import numpy.linalg as LA
import numpy
import matplotlib
from matplotlib import pylab, mlab, pyplot
np = numpy
plt = pyplot
from IPython.display import display
from IPython.core.pylabtools import figsize, getfigs
import IPython
from pylab import *
from numpy import *

Hint:Beforeyoustartsolvingtheassignment,youmightwanttocheckthefollowingnumpyfunctions:
numpy.linalg.svd

Covariancematrixproperties
Writeamathematicallysoundproofforeachofthefollowingpropertiesofthecovariancematrix:
1)ThatforagivenmatrixXmn ,wheren isthenumberofobservationsandm isthenumberof
variables,theexpression
1
N 1

(xij x
j )(xik x
k )

yieldsthecovariancematrix.
2)Underwhichconditionstakingdoestheeigendecompositionofamatrixyieldanorthonormalbasis,
andforwhichvectorspace.
http://localhost:8888/notebooks/Downloads/Lecture%202nd%20semester/MRC/%5B004%5D%20Applications%20of%20Singular

1/9

4/25/2016

assignment_matrix_decomposition-Copy2

3)WhenapplyingPrincipalComponentsAnalysis,eacheigenvectorpointsintothedirectionofonethe
dataset'sprincipalcomponentsandhowmuchdothecomponentcontributetotheoverallvarianceis
relatedtoeacheigenvector'seigenvalue.

Fittinglinestopointclouds
Readthisarticleaboutleastsquaresfitting(http://mathworld.wolfram.com/LeastSquaresFitting.html).
Readthislectureaboutlinearleastsquaresandmatrixdecompositions
(http://classes.soe.ucsc.edu/cmps290c/Spring04/paps/lls.pdf).Forallofthefollowingfittingtasks,
useasingularvaluedecompositiontofitapairoflinestoeachofthegivenpointclouds.

Detectingahallway
TherobotisdrivinginahallwaywhenitstartsitsKinectobtainingthefollowingplointcloud.

http://localhost:8888/notebooks/Downloads/Lecture%202nd%20semester/MRC/%5B004%5D%20Applications%20of%20Singular

2/9

4/25/2016

assignment_matrix_decomposition-Copy2

In[94]:
import IPython
IPython.core.display.Image("images/hallway.png", embed=True)
Out[94]:

http://localhost:8888/notebooks/Downloads/Lecture%202nd%20semester/MRC/%5B004%5D%20Applications%20of%20Singular

3/9

4/25/2016

assignment_matrix_decomposition-Copy2

In[95]:
%matplotlib inline
import matplotlib.pyplot as plt
def matrix_calculation(filename):
line = np.load(filename)
x= line[:,0]
y = line[:,1]
A = np.column_stack((x, np.ones(len(x))))
m,c = svd_solver(A,y)
return x,m,c
# x, y, m, c = matrix_calculation('data/Q1.npy')
# print y
def plotting(line, halt):
# to plotting the data from matrix calculation
plt.title('Scatter plot')
#plotting the values of x, m, c
plt.plot(line[0], line[1]*line[0] +line[2] , 'r', label = ' Fitted line data'
#plt.plot(x2, m1*x2+c1, 'r', label = ' Fitted line data')
plt.hold(halt)
if not halt:
plt.legend()
plt.show()
def svd_solver(A, b):
U,s,V= (LA.svd(A))
#print U.shape, s.shape, V.shape
#U = np.matrix(U),V
inv = np.dot((V.T).dot(np.diag(s**(-1))), U[:,:2].T)
#print inv
x = np.dot(inv,b)
#print x
return x

Yourtaskistofitapairlinesbasedonthepointsgeneratedbytherobot'sKinect.

http://localhost:8888/notebooks/Downloads/Lecture%202nd%20semester/MRC/%5B004%5D%20Applications%20of%20Singular

4/9

4/25/2016

assignment_matrix_decomposition-Copy2

In[96]:
def compute_parallel_lines(first_line, second_line):
plotting(first_line, True)
plotting(second_line, False)

In[97]:
first_line=matrix_calculation('data/P1.npy')
second_line = matrix_calculation('data/Q1.npy')
compute_parallel_lines(first_line,second_line)

Detectingacorner
Attheendofthehallwaytherobotdetectsadramaticchangeinthepointcloud.

http://localhost:8888/notebooks/Downloads/Lecture%202nd%20semester/MRC/%5B004%5D%20Applications%20of%20Singular

5/9

4/25/2016

assignment_matrix_decomposition-Copy2

In[98]:
import IPython
IPython.core.display.Image("images/corner.png", embed=True)
Out[98]:

Yourtaskistofitapairlinesbasedonthepointsgeneratedbytherobot'sKinect.
In[99]:
def compute_perpendicular_lines(first_line, second_line):
plotting(first_line, True)
plotting(second_line, False)

http://localhost:8888/notebooks/Downloads/Lecture%202nd%20semester/MRC/%5B004%5D%20Applications%20of%20Singular

6/9

4/25/2016

assignment_matrix_decomposition-Copy2

In[100]:
first_line=matrix_calculation('data/Q1.npy')
second_line = matrix_calculation('data/Q2.npy')
compute_perpendicular_lines(first_line,second_line)

Detectingatable
Whentherobotturnsatatablenearbythepointclouditreceivesisthefollowing.

http://localhost:8888/notebooks/Downloads/Lecture%202nd%20semester/MRC/%5B004%5D%20Applications%20of%20Singular

7/9

4/25/2016

assignment_matrix_decomposition-Copy2

In[101]:
import IPython
IPython.core.display.Image("images/rectangle.png", embed=True)
Out[101]:

In[102]:
def compute_rectangle_lines(first_line, second_line, third_line, fourth_line):
plotting(first_line, True)
plotting(second_line, True)
plotting(third_line, True)
plotting(fourth_line, False)

http://localhost:8888/notebooks/Downloads/Lecture%202nd%20semester/MRC/%5B004%5D%20Applications%20of%20Singular

8/9

4/25/2016

assignment_matrix_decomposition-Copy2

In[103]:
first_line=matrix_calculation('data/P2.npy')
second_line = matrix_calculation('data/S2.npy')
third_line = matrix_calculation('data/R2.npy')
fourth_line=matrix_calculation('data/Q2.npy')
compute_rectangle_lines(first_line, second_line, third_line, fourth_line)

http://localhost:8888/notebooks/Downloads/Lecture%202nd%20semester/MRC/%5B004%5D%20Applications%20of%20Singular

9/9

Você também pode gostar