trying to find main diagonal of a matrix for cholesky method

조회 수: 2 (최근 30일)
Gabriel
Gabriel 2023년 9월 3일
댓글: Dyuman Joshi 2023년 9월 3일
my goal is to find the main diagonal of a matrix G, through the cholesky method. For this I'm using a summation, however there is no success follow the code so far and the sum of interest.
clear;
clc;
%cholesky
n= input ("dimension of system:"); %dimension of system for the matrix
A= input("enter matrix A "); %initial matrix A for the cholesky method
disp (A);
i=1;
j=1;
for i=j
if i==1
g(1,1)=sqrt (A(1,1));
fprintf ("first element: \n"); %first element of the diagonal g(1,1)
disp (g(1,1))
end
%diagonal
k=1;
for i=2:n
for i=i-1
g(i,i)= g(i,i) + sqrt(A(i,i) - g(i,k)^2); % summation for the remaining diagonals dependent on the dimension of the system
end
end
end
disp(g(i,i))
the sum of interest below:
the entries are respectively:
3 -> dimension of system
[ 1 1 0; 1 2 -1; 0 -1 3] -> matrix A
the error is:

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 9월 3일
%Random data
n = 5; %input ("dimensão do sistema:"); %dimension of system for the matrix
A = magic(5); %input("entre com a matriz "); %initial matrix A for the cholesky method
disp(A);
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
%Pre-allocate g array
g = zeros(n);
for i=1:n
if i==1
g(i,i) = sqrt(A(i,i));
fprintf("primeiro elemento: %f\n", g(1,1)); %first element of the diagonal g(1,1)
else
k=1:i-1;
%summation for the remaining diagonals dependent on the dimension of the system
g(i,i) = sqrt(A(i,i) - sum(g(i,k).^2));
end
end
primeiro elemento: 4.123106
disp(g)
4.1231 0 0 0 0 0 2.2361 0 0 0 0 0 3.6056 0 0 0 0 0 4.5826 0 0 0 0 0 3.0000
  댓글 수: 2
Gabriel
Gabriel 2023년 9월 3일
maybe I got it wrong somewhere, but the results don't match the table test, you could test with n = 3 and A = [ 1 1 0; 1 2 -1; 0 -1 3] please?
Dyuman Joshi
Dyuman Joshi 2023년 9월 3일
FYI, You can make a function and test for any inputs -
n = 3;
A = [1 1 0; 1 2 -1; 0 -1 3];
disp(A);
1 1 0 1 2 -1 0 -1 3
g = choleskymethod(A,n)
primeiro elemento: 1.000000
g = 3×3
1.0000 0 0 0 1.4142 0 0 0 1.7321
function g = choleskymethod(A,n)
%Pre-allocate g array
g = zeros(n);
for i=1:n
if i==1
g(i,i) = sqrt(A(i,i));
fprintf("primeiro elemento: %f\n", g(1,1)); %first element of the diagonal g(1,1)
else
k=1:i-1;
%summation for the remaining diagonals dependent on the dimension of the system
g(i,i) = sqrt(A(i,i) - sum(g(i,k).^2));
end
end
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by