Solve a system of algebraic equations by Gauss elimination using two significant digits
4 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
John D'Errico
2016-2-16
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.
2 个评论
John D'Errico
2016-2-16
编辑:John D'Errico
2016-2-16
Your expectation would be, with only 2 digits carried, to get virtually meaninglessly random results. Of course, that depends on the matrix.
My only suggestion is to use HPF. If you use all HPF numbers along the way, it will indeed do the arithmetic in 2 digits, without your need to force a round at every step.
The problem with using digits(2) and vpa at each step, is that does not really restrict the result to be only 2 digits internally. For example, I'll set digits to be 2, then create a symbolic number.
digits 2
p = sym(1.23456);
vpa(p)
ans =
1.2
So it seems that p is now rounded to 2 digits. Or is really rounded to 2 digits?
vpa(p,10)
ans =
1.23456
So, even though I used digits(2), p is still carried to full precision internally. Perhaps I needed to force p to be the result of vpa(p,2)?
p = vpa(p,2)
p =
1.2
vpa(p,10)
ans =
1.23456
But even so, we see that p is STILL carried to full precision internally.
HPF on the other hand, will allow us to do as desired. First, execute these two commands:
DefaultDecimalBase 1
DefaultNumberOfDigits 2 0
Now, create an HPF number from 1.23456.
p = hpf(1.23456)
p =
1.2e0
It was displayed as 1.2. What digits are carried internally for p?
mantissa(p)
ans =
1 2
So p was truly reduced to 2 digits.
So the only serious options I see are hand calculations on paper, or to use HPF, which should do as you desire.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!