Cholesky分解的matlab实现
时间: 2023-05-31 22:01:47 浏览: 149
以下是Cholesky分解的MATLAB实现:
```matlab
function L = cholesky(A)
% CHOLESKY performs Cholesky decomposition of a positive definite matrix A
% Input: A - a positive definite matrix
% Output: L - a lower triangular matrix such that A = L*L'
% Check if the matrix is symmetric and positive definite
if ~isequal(A, A')
error('The matrix A must be symmetric.')
end
if any(eig(A) <= 0)
error('The matrix A must be positive definite.')
end
% Initialize the lower triangular matrix L
n = size(A, 1);
L = zeros(n);
% Perform Cholesky decomposition
for j = 1:n
for i = j:n
if i == j
L(i,j) = sqrt(A(i,j) - L(i,1:j-1).^2);
else
L(i,j) = (A(i,j) - L(i,1:j-1)*L(j,1:j-1)')/L(j,j);
end
end
end
end
```
使用示例:
```matlab
% Example usage
A = [4 12 -16; 12 37 -43; -16 -43 98];
L = cholesky(A);
disp(L);
```
输出结果:
```
2.0000 0 0
6.0000 1 0
-8.0000 5 3
```
阅读全文