Question: v 1 = [ 2 1 1 ] , v 2 = [ 1 1 0 ] , v 3 = [ 0 1 1

v1=[
2
1
1
], v2=[
1
1
0
], v3=[
0
1
1
]
what is wrong with my code, i keep getting: Orthonormalized Vectors (Q):[[8.16496581e-01-3.14018492e-165.77350269e-01][4.08248290e-017.07106781e-01-5.77350269e-01][-4.08248290e-017.07106781e-015.77350269e-01]]. When i should get vector1=[0.81649658,0.4824829,-0.40824829], vector2=[0,0.70710678,0.707106678] and vector 3=[0.57735027,-0.57735027,0.57735027]. I included an image of the the expected numbers should be given these certain vectors.
this is my code: import numpy as np
# Define the input vectors
v1= np.array([2,1,-1])
v2= np.array([1,1,0])
v3= np.array([0,-1,1])
# Combine the vectors into a matrix where each column is a vector
V = np.column_stack((v1, v2, v3))
def gram_schmidt(V):
# Input: V is a set of vectors stored as columns in a matrix
V = V.astype(np.float64)# Ensure float precision
n, k = V.shape
Q = np.zeros_like(V)# Matrix to store the orthonormalized vectors
for j in range(k):
v_j = V[:, j].copy()# Work with the j-th vector
for i in range(j):
q_i = Q[:, i]
s = np.dot(v_j, q_i)# s v_j, q_i
v_j = v_j - s * q_i # v_j v_j - s * q_i
norm_v_j = np.linalg.norm(v_j)
if norm_v_j >1e-10: # Normalize to avoid division by zero
Q[:, j]= v_j / norm_v_j # v_j v_j /||v_j||
return Q
def verify_orthonormality(Q):
# Verify magnitudes (should be close to 1)
for idx, q in enumerate(Q.T):
magnitude = np.linalg.norm(q)
print(f"Magnitude of vector {idx+1}={magnitude:.5f}")
print("-"*50)
# Verify dot products between different vectors (should be close to 0)
k = Q.shape[1]
for i in range(k):
for j in range(i +1, k):
dot_product = np.dot(Q[:, i], Q[:, j])
print(f"Dot product of vectors {i+1} and {j+1}: {dot_product:.5f}")
# Perform Gram-Schmidt orthonormalization
Q = gram_schmidt(V)
# Display the orthonormalized vectors
print("Orthonormalized Vectors (Q):")
print(Q)
# Verify orthonormality
print("
Running verification function:")
verify_orthonormality(Q)
v 1 = [ 2 1 1 ] , v 2 = [ 1 1 0 ] , v 3 = [ 0 1 1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!