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)
if(size(b,2)>1);
b = b';
end
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);
end
end
% 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);
end
xround = round(x,2,'significant');
Error1 = abs(ExtSol-xround');
Data1 = [Data1; ExtSol x' xround' Error1];
disp(' Exact Sol Calculated Rounded Error')
disp('________________________________________________________________________________')
disp(Data1)
end

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 =
3.1e0
p*3
ans =
9.4e0
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.