You are on page 1of 3

Yonda Lavembelno

1806202014
clc
clear

A = [1 -1/4 -1/4 0; -1/4 1 0 -1/4; -1/4 0 1 -1/4; 0 -1/4 -1/4 1];


B = [1/2; 1/2; 1/4; 1/4];
n = length (B);
tol = 1e-4;
iter = 1;
maxit = 50;
x = zeros (n ,1) ; // I n t i a l g u e s s
E = ones (n ,1) ; //Assuming to a vo id
S = diag ( diag (A));
T = S-A;
xold = x;
while (1)
for i = 1:n
x(i, iter +1) = (B(i) + T(i ,:) * xold ) / A(i,i);
E(i, iter +1) = (x(i, iter +1) -xold (i))/x(i, iter+1) *100;
xold (i) = x(i, iter +1);
end
if x(:, iter ) == 0
E = 1;
else
E = sqrt (( sum ((E(:, iter +1) ) .^2) )/n);
end
if E <= tol | iter == maxit
break
end
iter = iter + 1;
end
X = x(:, iter );
x = round (x *10^5) /10^5;
x(: ,1) = [];
mprintf ( '%s %3s %11s %10s %10s ' , 'I t e r No ' , 'x1' , 'x2', 'x3' , 'x4' );
disp ([(1: iter )'x']) ;

I t e r No x1 x2 x3 x4

1. 0.5 0.625 0.375 0.5

2. 0.75 0.8125 0.5625 0.59375

3. 0.84375 0.85938 0.60938 0.61719

4. 0.86719 0.87109 0.62109 0.62305

5. 0.87305 0.87402 0.62402 0.62451

6. 0.87451 0.87476 0.62476 0.62488

7. 0.87488 0.87494 0.62494 0.62497

8. 0.87497 0.87498 0.62498 0.62499


9. 0.87499 0.875 0.625 0.625

10. 0.875 0.875 0.625 0.625

11. 0.875 0.875 0.625 0.625

12. 0.875 0.875 0.625 0.625

clc
clear

A = [130 -30 0; 90 -90 0; 40 60 -120];


B = [500; 0;-500];
n = length (B);
tol = 1e-4;
iter = 1;
maxit = 50;
x = zeros (n ,1) ; // I n t i a l g u e s s
E = ones (n ,1) ; //Assuming to a vo id
S = diag ( diag (A));
T = S-A;
xold = x;
while (1)
for i = 1:n
x(i, iter +1) = (B(i) + T(i ,:) * xold ) / A(i,i);
E(i, iter +1) = (x(i, iter +1) -xold (i))/x(i, iter+1) *100;
xold (i) = x(i, iter +1);
end
if x(:, iter ) == 0
E = 1;
else
E = sqrt (( sum ((E(:, iter +1) ) .^2) )/n);
end
if E <= tol | iter == maxit
break
end
iter = iter + 1;
end
X = x(:, iter );
x = round (x *10^5) /10^5;
x(: ,1) = [];
mprintf ( '%s %3s %11s %10s %10s ' , 'I t e r No ' , 'x1' , 'x2', 'x3' , 'x4' );
disp ([(1: iter )'x']) ;

I t e r No x1 x2 x3 x4

1. 3.84615 3.84615 7.37179

2. 4.73373 4.73373 8.11144

3. 4.93855 4.93855 8.28213

4. 4.98582 4.98582 8.32152

5. 4.99673 4.99673 8.33061


6. 4.99924 4.99924 8.3327

7. 4.99983 4.99983 8.33319

8. 4.99996 4.99996 8.3333

9. 4.99999 4.99999 8.33333

10. 5. 5. 8.33333

11. 5. 5. 8.33333

You might also like