Set working precision in Matlab
15 views (last 30 days)
Show older comments
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).
0 Comments
Accepted Answer
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.
0 Comments
More Answers (2)
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
on 10 Mar 2023
Moved from flags:
Andrew Wade wrote: "Sexist analogies probably don't have a place here anymore."
Jan
on 10 Mar 2023
The answer does not contain rudeness degradation, but bad examples for the topic of nutrition.
Giancarlo Nino
on 12 Dec 2016
1 Comment
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)
See Also
Categories
Find more on Numbers and Precision in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!