Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. Write a second m-file called ntn.m that takes as input a matrix A, and return

ID: 3167891 • Letter: 3

Question

3. Write a second m-file called ntn.m that takes as input a matrix A, and returns a matrix N such that A NTN if A is symmetric and positive definite. Your function should call your function posdef to first check if A is symmetric and positive definite, and return [ ] (the 0 x 0 matrix) if it is not. The diagonalisation of A can be found using the command [P,D]-eig(A), and if A is real symmetric the output P is orthogonal. Square roots can be found using sqrt (which operates elementwise). Test your function on the matrix 3 2 3 A 2 5 2 3 2 6 Include the output from your test in your submission

Explanation / Answer

%%% Matlab Function %%%

function [C,gh]=posdef (A)
[r,c]=size(A);
temp=0; %%% to check positive definite
gh=0; %%% Condition to calculate sqrt
C=A;
%%% To check positive definite
for n=1:r
for k=1:c
if A(n,k)<0
temp=1;
fpritf('Matrix is not positive definite ');
C=zero(r,c);
gh=1;
break;
end
end
end

%%% To check symmetry of matrix
if (temp==0)
for n=1:r
for k=1:c
if (A(n,k)~=A(k,n))
gh=1;
fpritf('Matrix is positive definite but not symmetry ');
C=zero(r,c);
break;
end
end
end
end

%%% Matlab programme%%

A=[3 2 3;2 5 2;3 2 6];
[C,gh]=posdef(A);
  

if (gh==0)
fprintf(' Given matrix is symmetric and positive definite ');
[p,d]=eig(A);
N=sqrt(d)*p';
fprintf('A=N^tN where N: ');
disp(N);
%%% U can verify that B'B=A
end

OUTPUT:

Given matrix is symmetric and positive definite
A=N^tN where N:
-0.8729 0.2182 0.4364
0.1383 -1.5333 1.0433
1.4896 1.6129 2.1728

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote