Clear Filters
Clear Filters

I took this code from i have a viva tomorrow plz explain me the code

1 view (last 30 days)
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))

Accepted Answer

Thomas
Thomas on 21 Mar 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

More Answers (1)

FYP
FYP on 26 Mar 2012
thanks mate

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!