I took this code from i have a viva tomorrow plz explain me the code
조회 수: 1 (최근 30일)
이전 댓글 표시
clear X A=input ('please enter the Matrix :') b=[8;4;5]
tol = 1.e-8; maxiter = 100; relerr = inf; niter = 1; S = diag( diag(A) ); display (S) T = S - A; display (T)
X(:,1) = zeros(size(b)); while relerr > tol & niter< maxiter, X(:,niter+1) = S \ (b+T*X(:,niter)); relerr = norm(X(:,niter+1)-X(:,niter),inf)/norm(X(:,niter+1),inf); niter = niter+1; end display (niter) display (X(:,niter))
댓글 수: 0
채택된 답변
Thomas
2012년 3월 21일
Dont know if this code works.. the function of each line is inthe comments i.e after the %
clear X % clears the variable X
A=input ('please enter the Matrix :') % asks the user to input the matrix
% define variable:
b=[8;4;5] % define b
tol = 1.e-8; % define tolerance
maxiter = 100; % define maximum number of iterations
relerr = inf; % define relative error
niter = 1;
% cal
S = diag( diag(A) ); % finds the diagonal matrix of the diagonal elements of input
display (S) % displays matrix S from previous step
T = S - A; % create a new matrix T
display (T) % displays matrix t from previous step
X(:,1) = zeros(size(b)); % for a matrix the sixe of b insert all zeroes
% while the relative error is > the tolerance and interation no < max number of iterations
while relerr > tol & niter< maxiter,
X(:,niter+1) = S \ (b+T*X(:,niter)); % X is calculated as
relerr = norm(X(:,niter+1)-X(:,niter),inf)/norm(X(:,niter+1),inf); % new relative error is
niter = niter+1; % iteration number is incremented to step through the loop
end
display (niter) % display the iteration number
display (X(:,niter)) % display the output
댓글 수: 0
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!