Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clear all;
- clc;
- % Tolerance
- tolerance = 5;
- Es = 0.5*10^(2-tolerance);
- breakFlag = 0;
- % Aug matrix
- A = [ 3 7 13 76
- 1 5 3 28
- 12 3 -5 1];
- needsPivoting = 0;
- % Aug Matrix split
- [m,n] = size(A);
- B = A(:, n).';
- A(:, n) = [];
- clear m;
- clear n;
- [rows, columns] = size(A);
- % CONVERGENCE
- for i=1:rows
- sum = 0;
- for j=1:columns
- if(i~=j)
- sum = sum + abs( A(i, j) );
- end
- end
- if( abs( A(i, i) ) < sum )
- needsPivoting = 1;
- break;
- end
- end
- % PIVOTING
- if( needsPivoting )
- for i=1:(rows-1)
- for z=2:rows
- sum = 0;
- for j=1:columns
- if(i~=j)
- sum = sum + abs( A(i, j) );
- end
- end
- if( abs( A(i, i) ) < sum )
- tempRowA = A(i, :);
- tempRowB = B(i);
- A(i, :) = A(z, :);
- B(i) = B(z);
- A(z, :) = tempRowA;
- B(z) = tempRowB;
- end
- end
- end
- end
- % Gauss Siedel
- x = zeros(columns, 1);
- n = length(x);
- iteration = 1;
- while(1)
- for i=1:n
- temp = 0;
- if( iteration==1 )
- for j=1:n
- if(i~=j)
- temp = temp + A(i, j)*x(j, iteration);
- end
- end
- x(i, iteration) = (B(i) - temp)/A(i, i);
- else
- for j=1:n
- if(i~=j)
- temp = temp + A(i, j)*x(j, iteration-1);
- end
- end
- x(i, iteration) = (B(i) - temp)/A(i, i);
- end
- end
- for i=1:n
- if( iteration>2 )
- errors(i) = (abs(x(i, iteration) - x(i, iteration-1))/x(i, iteration))*100;
- Ea(iteration-2) = max(errors);
- if( Ea(iteration-2) <= Es )
- breakFlag = 1;
- break;
- end
- end
- end
- if(breakFlag == 1)
- break;
- end
- iteration = iteration + 1;
- end
- [m, n] = size(x);
- display(x(:, n));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement