Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function X = gauss(A, f)
- n = length(f);
- X = zeros(n, 1);
- buffer = zeros(n, 1);
- for i=1:n
- main_el = A(i, i);
- row = i;
- col = i;
- %find main element
- for k=i:n
- for l=1:n
- if abs(A(row, col)) < abs(A(k, l))
- row = k;
- col = l;
- end;
- end;
- end;
- buffer(i) = col;
- for j=i:n
- if (j == row)
- continue;
- end;
- d = A(j, col)/A(row, col);
- A(j, 1:n) = A(j, 1:n) - d*A(row, 1:n);
- f(j) = f(j) - d*f(row);
- end;
- %swap lines
- B = A(row, 1:n);
- A(row, 1:n) = A(i, 1:n);
- A(i, 1:n) = B;
- end;
- for i=n:-1:1
- s = f(i);
- for j=i+1:n
- s = s - A(i, buffer(j))*X(buffer(j));
- end;
- X(buffer(i)) = s/A(i, buffer(i));
- end;
- A*X - f
- endfunction
Add Comment
Please, Sign In to add comment