Write a MATLAB function G givensrotnijtheta which produces a
**Write a MATLAB
function G= givensrot(n,i,j,theta)
which produces an n x n Givens matrix. Consider 1<=i < j <= n . If these conditions do not hold, the program returns an empty matrix G = [ ] and terminates. Otherwise, the output has to be the Givens rotation matrix G.
**Type the function givensrot in your diary file.
(a)**Run the function G= on each set of variables (1) - (3).
(1) ; n = 4 , i = 3, j=2 , theta = pi/2
(2) ; n= 5, i = 2, j = 4 , theta = pi/4
(3) ;n= 3, i = 1, j = 2, theta = pi
(type pi for pi in MATLAB.)
Solution
The MATLAB function givensrot.m
function G =givensrot(n,i,j,theta) % The function to generate Givens rotation matrix
if(i<1 || i>=j || j>n) % Checking the conditions
G = []; % If condition is true return the function with empty matix
return % function return
end % end of condition checking
G = eye(n); % Creating the G matrix gkk =1 if k not eq to j,i
G(i,i) = cos(theta); % gii = cos(theta)
G(j,j) = cos(theta); % gjj = cos(theta)
G(j,i) = -sin(theta); % gji = -sin(theta)
G(i,j) = sin(theta); %gij = sin(theta)
end % End of function
Testing the function
G =givensrot(4,3,2,pi/2)
G =givensrot(5,2,4,pi/4)
G =givensrot(3,1,2,pi)
OUTPUT
G =
[]
G =
1.0000 0 0 0 0
0 0.7071 0 0.7071 0
0 0 1.0000 0 0
0 -0.7071 0 0.7071 0
0 0 0 0 1.0000
G =
-1.0000 0.0000 0
-0.0000 -1.0000 0
0 0 1.0000

