CS代考 APS1070_Week_7_Lecture_Code – cscodehelp代写

APS1070_Week_7_Lecture_Code
APS1070 Week 7 Lecture Code¶
Example 1 – Projections¶
import numpy as np
import pandas as pd
from numpy import linalg as LA
from mpl_toolkits import mplot3d
%matplotlib inline
import matplotlib.pyplot as plt
# Let’s create a column vector and plot it in the 3 dimensional space
b = np.array([[1, 2, 2]]).T
array([[1],
fig = plt.figure()
ax = plt.axes(projection=’3d’)
# We use the x dimension as the baseline
# This line of code creates five linearly distanced points in the range of 0 to 1
b_x = np.linspace(0, 1 ,5)
# We measure the y and z coordiantes of the vector according to its x coordinate
b_y = b[1]/b[0]*b_x
b_z = b[2]/b[0]*b_x
ax.plot3D(b_x, b_y, b_z, ‘orange’)
ax.set_xlabel(‘x’)
ax.set_ylabel(‘y’)
ax.set_zlabel(‘z’);
Create arbitrary point x (can be the endpoint of a vector)
x = np.array([[1, 1, 1]]).T
array([[1],
fig = plt.figure()
ax = plt.axes(projection=’3d’)
x_x = x[0]
x_y = x[1]
x_z = x[2]
ax.plot3D(b_x, b_y, b_z, ‘orange’)
ax.scatter3D(x_x, x_y, x_z, color=”b”);
ax.set_xlabel(‘x’)
ax.set_ylabel(‘y’)
ax.set_zlabel(‘z’);
Prepare projection matrix
$P_pi = frac{bb^T}{b^Tb}$
array([[0.11111111, 0.22222222, 0.22222222],
[0.22222222, 0.44444444, 0.44444444],
[0.22222222, 0.44444444, 0.44444444]])
Project point x onto the line
fig = plt.figure()
ax = plt.axes(projection=’3d’)
Px_x = Px[0]
Px_y = Px[1]
Px_z = Px[2]
ax.plot3D(b_x, b_y, b_z, ‘orange’)
ax.scatter3D(x_x, x_y, x_z, color=”b”);
ax.scatter3D(Px_x, Px_y, Px_z, color=”r”);
ax.set_xlabel(‘x’)
ax.set_ylabel(‘y’)
ax.set_zlabel(‘z’);
What about projecting other points?
#create a second point “h”
h = np.array([[1, 0.5, 0.5]]).T
array([[1. ],
Project h onto the line
array([[0.33333333],
[0.66666667],
[0.66666667]])
fig = plt.figure()
ax = plt.axes(projection=’3d’)
h_x = h[0]
h_y = h[1]
h_z = h[2]
Ph_x = Ph[0]
Ph_y = Ph[1]
Ph_z = Ph[2]
ax.plot3D(b_x, b_y, b_z, ‘orange’)
ax.scatter3D(x_x, x_y, x_z, color=”b”);
ax.scatter3D(Px_x, Px_y, Px_z, color=”r”);
ax.scatter3D(h_x, h_y, h_z, color=”b”);
ax.scatter3D(Ph_x, Ph_y, Ph_z, color=”r”);
ax.set_xlabel(‘x’)
ax.set_ylabel(‘y’)
ax.set_zlabel(‘z’);
What about a point already in the span of $b$?
# create point “i” that is on the line
i = np.array([[0.1, 0.2, 0.2]]).T
array([[0.1],
Project i onto the line…
# The projection of the point i is itself
array([[0.1],
# We plot the projection of point i in green
fig = plt.figure()
ax = plt.axes(projection=’3d’)
i_x = i[0]
i_y = i[1]
i_z = i[2]
Pi_x = Pi[0]
Pi_y = Pi[1]
Pi_z = Pi[2]
ax.plot3D(b_x, b_y, b_z, ‘orange’)
ax.scatter3D(x_x, x_y, x_z, color=”b”);
ax.scatter3D(Px_x, Px_y, Px_z, color=”r”);
ax.scatter3D(h_x, h_y, h_z, color=”b”);
ax.scatter3D(Ph_x, Ph_y, Ph_z, color=”r”);
ax.scatter3D(i_x, i_y, i_z, color=”b”);
ax.scatter3D(Pi_x, Pi_y, Pi_z, color=”g”);
ax.set_xlabel(‘x’)
ax.set_ylabel(‘y’)
ax.set_zlabel(‘z’);
Similarly, projecting the projection matrix onto itself results in P = PP
array([[0.11111111, 0.22222222, 0.22222222],
[0.22222222, 0.44444444, 0.44444444],
[0.22222222, 0.44444444, 0.44444444]])
array([[0.11111111, 0.22222222, 0.22222222],
[0.22222222, 0.44444444, 0.44444444],
[0.22222222, 0.44444444, 0.44444444]])
Example 2 – Projection onto Subspace¶
Projection onto a line can be thought of as a projection onto a 1-d subspace. Now we will project onto a 2-d subspace.
# Example 3.11 from the textbook
# create our 2-d subspace, U, using two independent basis vectors
B = np.array([[1, 0], [1, 1], [1, 2]])
array([[1, 0],
# create arbitrary vector x to project onto subspace U
x = np.array([[6], [0], [0]])
array([[6],
The main formula is
$B^TBlambda=B^Tx$
# Matrix in the left-hand side of the equation
array([[3, 3],
# Right-hand side of the equation
array([[6],
# Solving for lambda
array([[ 5.],
# B lambda is the projection
array([[ 5.],
#Projection error was np.sqrt(6)
np.linalg.norm(x-proj)
2.449489742783178
# The projection matrix
array([[ 0.83333333, 0.33333333, -0.16666667],
[ 0.33333333, 0.33333333, 0.33333333],
[-0.16666667, 0.33333333, 0.83333333]])
# Confirming that PP=P
array([[ 0.83333333, 0.33333333, -0.16666667],
[ 0.33333333, 0.33333333, 0.33333333],
[-0.16666667, 0.33333333, 0.83333333]])
Visualize the projection of points onto a 2-d subspace
fig = plt.figure()
ax = plt.axes(projection=’3d’)
normal_to_plane = np.cross(B[:,0], B[:,1]) #more info here: https://math.stackexchange.com/questions/562123/equation-of-plane-containing-two-vectors
B_x, B_y = np.meshgrid(np.linspace(0, 6, 10), np.linspace(0, 6, 10))
B_z = (-normal_to_plane[0]*B_x – normal_to_plane[1]*B_y) / normal_to_plane[2] #based on algorithm here: https://stackoverflow.com/questions/3461869/plot-a-plane-based-on-a-normal-vector-and-a-point-in-matlab-or-matplotlib/23006541
ax.set_xlabel(‘x’)
ax.set_ylabel(‘y’)
ax.set_zlabel(‘z’)
ax.plot_wireframe(B_x, B_y, B_z, color = ‘orange’)
U1_x = np.linspace(0, 6, 10)
U1_y = B[1,0]/B[0,0]*U1_x
U1_z = B[2,0]/B[0,0]*U1_x
ax.plot3D(U1_x, U1_y, U1_z, ‘black’, linewidth=4)
U2_x = np.linspace(0,0,10)
U2_y = np.linspace(0, 6, 10)
U2_z = B[2,1]/B[1,1]*U2_y
ax.plot3D(U2_x, U2_y, U2_z, ‘grey’, linewidth=4)
x_x = x[0]
x_y = x[1]
x_z = x[2]
Px_x = Px[0]
Px_y = Px[1]
Px_z = Px[2]
ax.scatter3D(x_x, x_y, x_z, color=”b”);
ax.scatter3D(Px_x, Px_y, Px_z, color=”r”);
# change the point of view
#ax.view_init(65, 90)

Leave a Reply

Your email address will not be published. Required fields are marked *