2. Write an m-file called posdef.m that takes as input a matrix A, and (a) tests
ID: 3167890 • Letter: 2
Question
2. Write an m-file called posdef.m that takes as input a matrix A, and (a) tests whether A is square; (b) tests whether A is symmetric; (e) uses Sylvester's Criterion to determine whether A is positive definite, when A is square and symmetric. Your function should return 1 (true) when A is square, symmetrie and positive definite, and 0 (false) otherwise. Test your function on the matrices B=12 2 2 0 2 1 1 2 3 C=14 5 6 and include the output in your submission. Does the result match the conclusion you would draw from sing the command eig to find the eigenvalues!? Some hints. Read the help for if to see how to write a conditional statement. The command size(A) returns the size of A, andx - y is true when r and y are equal, and false otherwise. Use a loop of the form for k=1:n statements to erecute end to check each principal minor of A in turn. One possible approach is to initialise output to 1 before the start of the loop, and then set output to O if a minor fails the criterion. The command break can then be used to exit the loop premat urelyExplanation / Answer
function output=posdef(A)
[rows columns]=size(A);
n=size(A,1);
output=0;
if rows==columns %Matrix is square if no.of rows and columns are equal
if issymmetric(A)==1%checks whether a matrix is symmetric or not (MATLAB built-in function)
output=1;
for k=1:n
if det(A(1:k,1:k))<=0 % checks whether determinant of a leading principal minor is less than or equal to zero.
output=0;
break;
end
end
end
end
end
output for samples:
>> posdef([1 2;2 5])
ans =
1
>> posdef([3 2 0;2 2 2;0 2 1])
ans =
0
>> posdef([1 2 3;4 5 6])
ans =
0
To compare the results with the default eig function of matlab,
A matrix is positive definite if its all eigen values are positive
Now I check each of the three sample matrices with eig funciton
>> eig([1 2;2 5])
ans =
0.1716
5.8284
all eigen values are positive so it is positive definite.
>> eig([3 2 0;2 2 2;0 2 1])
ans =
-1.0000
2.0000
5.0000
one of the eigen values is not positive so it is not a positive definite matrix.
>> eig([1 2 3;4 5 6])
Error using eig
For standard eigenproblem EIG(A), A must be square.
not a postive definite matrix since it is not a square
If you observe all the results using eig function match with the result of our function posdef.m
all eigen values are positive so it is positive definite.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.