Set working precision in Matlab

조회 수: 15 (최근 30일)
Giancarlo Nino
Giancarlo Nino 2016년 12월 12일
댓글: Jan 2023년 3월 10일
Hi everyone, I'm trying to control some numerical roundoff error by varying the working precision used for the computations. I downloaded a free trial version of the symbolic Toolbox. I'm using the vpa and digits commands, but I'm not completely sure they are effective for the case I'm treating. Just an example: consider the Vandermonde matrix A(i,j)=x(i)^(j-1) where x=linspace(-pi,pi,20) and j=1:20. It is well known that this matrix is ill-conditioned. Compute b=A*ones(20,1). Now I try to compute A\b and vpa(A\b,64) and the result is a vector of zero elements. It seems that nothing happens using vpa and I don't get an improvement of the precision of the solution (that is correct up to an error of about e-4).

채택된 답변

Walter Roberson
Walter Roberson 2016년 12월 12일
You need to create your array as symbolic, like if you used sym(x) in your calculations and if you preallocate A as sym(zeros(20,20))
The digits setting does not affect calculations carried out in floating point.

추가 답변 (2개)

John D'Errico
John D'Errico 2016년 12월 12일
편집: John D'Errico 2016년 12월 12일
Hmm. It looks like the problem is not in VPA, but in your understanding of how MATLAB works under the hood, and of floating point arithmetic in general. :) Let me explain.
(As someone who has been married for 36 years...) Think of MATLAb as having a guy sitting in a room waiting for you to tell it what to do. Until then, he is munching on pizza and beer, watching the football game on the tube, but doing nothing useful otherwise.
When you type in vpa(A\b), the guy sits up, grumbles a bit, then looks around to see what are A and b. Then he checks on what you want to do with them, so he computes A\b. Finally, he sees that you want the result passed into vpa, so he does that. See that A\b has a DOUBLE precision result. Then when vpa sees it, it thinks, no problem, I can turn those numbers into symbolic numbers.
Now, had there been a woman in the room awaiting your input, she would have been doing something constructive until then. Then she would see the expression vpa(A\b) and first, tell you that you are doing it the wrong way, if what you wanted to compute was a symbolic result.
So how do I know its a guy under the hood? Because MATLAB does nothing constructive while awaiting input! :)
The point is, MATLAB does NOT start from the outside in on any expression. It works from the inside out, without thinking about what you want to do with those numbers in the end. It computes each subexpression, then sees what to do with them. It does exactly as it is told, but from the inside out.
Where is Loren when I need her to confirm all of this idle speculation?
As far as the computation is concerned, it helps if you start in symbolic form.
j = 1:20;
x = linspace(-sym(pi),sym(pi),20);
A = repmat(x.',1,20).^repmat(j,20,1);
b = A*ones(20,1);
A\b
ans =
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
  댓글 수: 2
Jan
Jan 2023년 3월 10일
Moved from flags:
Andrew Wade wrote: "Sexist analogies probably don't have a place here anymore."
Jan
Jan 2023년 3월 10일
The answer does not contain rudeness degradation, but bad examples for the topic of nutrition.

댓글을 달려면 로그인하십시오.


Giancarlo Nino
Giancarlo Nino 2016년 12월 12일
So, just to be sure I understood, this is the first time I use symbolic calculus on Matlab. My code is much more complicated thant the toy problem I mentioned before and all of my codes are written in Matlab (otherwise I would use Mathematica that is much simpler to use in these cases). I need to increase the precision of calculations, that is, I want a numerical roundoff initial error smaller than the standard one (about e-16). It is sufficient to initialize all of my objects as symbolic and then to use vpa to force the numerical calculations to consider a higher number of digits?
  댓글 수: 1
Walter Roberson
Walter Roberson 2016년 12월 12일
You need to be quite consistent about using symbolic calculations to get high precision output. For example you would not use
x = sym(2/3)
You would use
x = sym(2)/sym(3)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numbers and Precision에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by