Matlab function that takes in a matrix to test for positive definite
24 views (last 30 days)
Show older comments
Write a matlab function that takes in a matrix to test for positive definite.
1 Comment
Steven Lord
on 26 Apr 2023
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
Answers (1)
Kautuk Raj
on 2 Jun 2023
This is a MATLAB function that tests whether a given matrix is positive definite:
function [is_pd] = isPositiveDefinite(A)
% Function to test whether a matrix is positive definite
% Input: A - the matrix to test
% Output: is_pd - a boolean indicating whether A is positive definite
% Check that A is square
if size(A,1) ~= size(A,2)
error('Matrix must be square');
end
% Check that A is symmetric
if ~isequal(A, A')
error('Matrix must be symmetric');
end
% Compute the eigenvalues of A
lambda = eig(A);
% Check that all eigenvalues are positive
is_pd = all(lambda > 0);
end
An example of how to use the function:
% Test matrix
A = [4 1 2; 1 5 3; 2 3 6];
% Check if A is positive definite
is_pd = isPositiveDefinite(A);
% Display result
if is_pd
disp('A is positive definite');
else
disp('A is not positive definite');
end
0 Comments
See Also
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!