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))

采纳的回答

Thomas
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

更多回答(1 个)

FYP
FYP 2012-3-26
thanks mate

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by