MATLAB: Solve a system of algebraic equations by Gauss elimination using two significant digits

MATLABmatrix manipulation

Hi, I want to solve a system of algebraic equation by Gauss elimination using to significant digit. I am able to solve this, but I have no idea how to set the matlab to store only two significant digit in each step of calculation.
function Error1 = gauss3(A,b)
b = b';
ExtSol = A\b; % This is more efficient than inv(A)*b
n = length(b);
Data1 = [];
% Elimination
for k = 1:n-1
for i = k+1:n
lambda = A(i,k)/A(k,k);
A(i,k:n) = A(i,k:n)-lambda*A(k,k:n);
b(i) = b(i) -lambda*b(k);
% Back Substitution
x(n) = b(n)/A(n,n);
for k = n-1:-1:1
x(k) = (b(k)-A(k,k+1:n)*x(k+1:n)')/A(k,k);
xround = round(x,2,'significant');
Error1 = abs(ExtSol-xround');
Data1 = [Data1; ExtSol x' xround' Error1];
disp(' Exact Sol Calculated Rounded Error')

Best Answer

  • I'm sorry. But you cannot "set" MATAB to store only two significant digits, or any fixed number of significant digits, other than double or single precision.
    I suppose you could round each result to two significant digits after EVERY individual computation. I'm not at all sure why you wish to bother doing such a ridiculous thing anyway, as it would yield complete garbage for results almost always.
    The easiest way, IF this is not a homework assignment, would be to use my HPF. tool, which CAN be used to specify a number of decimal digits in all computations. You would need to download and install the toolbox from the File Exchange. Then issue this command:
    defaultNumberOfDigits 2 0
    So now all computations using HPF numbers will be done in only 2 digits of precision.
    p = hpf(pi)
    p =
    ans =
    So you could easily force all computations done using HPF to be in 2 decimal digits of accuracy. Now just define all of your variables as HPF numbers, and execute the Gauss elimination. Again, a complete waste of time unless it is a homework assignment, and you were forced to do it.
    If this is for homework, then do the Gaussian elimination by hand, carrying two digits at each step.
    Finally, if your question is only how to retain only two significant digits at the VERY end, WHY? Another silly goal. However, you could just use the round function on the final result.