Set working precision in Matlab

15 views (last 30 days)
Giancarlo Nino
Giancarlo Nino on 12 Dec 2016
Commented: Jan on 10 Mar 2023
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).

Accepted Answer

Walter Roberson
Walter Roberson on 12 Dec 2016
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.

More Answers (2)

John D'Errico
John D'Errico on 12 Dec 2016
Edited: John D'Errico on 12 Dec 2016
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 Comments
Jan
Jan on 10 Mar 2023
Moved from flags:
Andrew Wade wrote: "Sexist analogies probably don't have a place here anymore."
Jan
Jan on 10 Mar 2023
The answer does not contain rudeness degradation, but bad examples for the topic of nutrition.

Sign in to comment.


Giancarlo Nino
Giancarlo Nino on 12 Dec 2016
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 Comment
Walter Roberson
Walter Roberson on 12 Dec 2016
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)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!