You are on page 1of 3

EXPERIMENT-4 Zeeshan Zakariya

NAME: E.SAI YAKSHITH 22BTRCA057


USN: 22BTRCA010

COURSE: CSE-AI

1.SOLUTION:

given,t:R^2 to R^2

T(x,y)=(x+y,x-2y)

a)V=[-3 -2],T(V) =

python code:

import numpy as np
def T(V):
W = np.zeros((2,1)) # Size of the output vector (2 X 1)
W[0,0] = 1*V[0,0]+1*V[1,0]
W[1,0] = 1*V[0,0]-2*V[1,0]
return W
# creating a 1-D list (Vertical)
list = [[-3],[-2]] # input vector
V = np.array(list)
W = T(V)
# showing input vector
print("Input Vector")
print(V)
# showing Output vector
print("Output Vector")
print(W)

Input Vector
[[-3]
[-2]]
Output Vector
[[-5.]
[ 1.]]

b)v = [-1 5],T(v) =

python code:

import numpy as np
def T(V):
W = np.zeros((2,1)) # Size of the output vector (2 X 1)
W[0,0] = 1*V[0,0]+1*V[1,0]
W[1,0] = 1*V[0,0]-2*V[1,0]
return W
# creating a 1-D list (Vertical)
list = [[-1],[5]] # input vector
V = np.array(list)
W = T(V)
# showing input vector
print("Input Vector")
print(V)
# showing Output vector
print("Output Vector")
print(W)

Input Vector
[[-1]
[ 5]]
Output Vector
[[ 4.]
[-11.]]

2. Write a Python program to find the orthonormal matrix from the following set of orthogonal vectors. V1 = [1,0,-1];V2 = [1,sqrt2,1] and v3=
[1,-sqrt2,1]

solution:

python code:

import numpy as np
# Define the orthogonal vectors
v1 = np.array([1, 0, -1])
v2 = np.array([1, np.sqrt(2), 1])
v3 = np.array([1, -np.sqrt(2), 1])
# Perform Gram-Schmidt process to obtain orthonormal vectors
u1 = v1 / np.linalg.norm(v1)
u2 = v2 - np.dot(v2, u1) * u1
u2 = u2 / np.linalg.norm(u2)
u3 = v3 - np.dot(v3, u1) * u1 - np.dot(v3, u2) * u2
u3 = u3 / np.linalg.norm(u3)
# Convert the orthonormal vectors into a matrix
orthonormal_matrix = np.vstack((u1, u2, u3))
# Output the orthonormal matrix
print("Orthonormal Matrix:")
print(orthonormal_matrix)

Orthonormal Matrix:
[[ 0.70710678 0. -0.70710678]
[ 0.5 0.70710678 0.5 ]
[ 0.5 -0.70710678 0.5 ]]

3. Write a Python program to check whether the following vectors are orthogonal or not?. If so, find the orthonormal vectors.v1= [3,1,1], v2=
[-1,2,1] and v3=[-1/2,-2,7/2]

SOLUTION:

PYTHON CODE:

import numpy as np

# Define the vectors


v1 = np.array([3, 1, 1])
v2 = np.array([-1, 2, 1])
v3 = np.array([-1/2, -2, 7/2])

# Check if the vectors are orthogonal


if np.dot(v1, v2) == 0 and np.dot(v1, v3) == 0 and np.dot(v2, v3) == 0:
# If the vectors are orthogonal, find the orthonormal vectors
u1 = v1 / np.linalg.norm(v1)
u2 = v2 / np.linalg.norm(v2)
u3 = v3 / np.linalg.norm(v3)
print("The vectors are orthogonal.")
print("Orthonormal vectors:")
print(u1)
print(u2)
print(u3)
else:
print("The vectors are not orthogonal.")

The vectors are orthogonal.


Orthonormal vectors:
[0.90453403 0.30151134 0.30151134]
[-0.40824829 0.81649658 0.40824829]
[-0.12309149 -0.49236596 0.86164044]

4. Write a Python program to find the orthonormal basis for the following set of vectors S = {(1,-1,1);(1,0,1);(1,1,2)}

SOLUTION:

Python code:

import numpy as np

# Define the set of vectors


S = np.array([[1, -1, 1], [1, 0, 1], [1, 1, 2]])

# Define a function to normalize a vector


def normalize(v):
norm = np.linalg.norm(v)
if norm == 0:
return v
return v / norm

# Apply the Gram-Schmidt process to find the orthonormal basis


u1 = normalize(S[0])
u2 = normalize(S[1] - np.dot(u1, S[1]) * u1)
u3 = normalize(S[2] - np.dot(u1, S[2]) * u1 - np.dot(u2, S[2]) * u2)

# Print the orthonormal basis


print("Orthonormal basis:")
print(u1)
print(u2)
print(u3)

Orthonormal basis:
[ 0.57735027 -0.57735027 0.57735027]
[0.40824829 0.81649658 0.40824829]
[-0.70710678 0. 0.70710678]
check 0s completed at 2:39 PM

You might also like