I took this code from i have a viva tomorrow plz explain me the code
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
Thomas
am 21 Mär. 2012
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 Kommentare
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!