Set working precision in Matlab
조회 수: 15 (최근 30일)
이전 댓글 표시
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
채택된 답변
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.
댓글 수: 0
추가 답변 (2개)
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
2023년 3월 10일
Moved from flags:
Andrew Wade wrote: "Sexist analogies probably don't have a place here anymore."
Jan
2023년 3월 10일
The answer does not contain rudeness degradation, but bad examples for the topic of nutrition.
Giancarlo Nino
2016년 12월 12일
댓글 수: 1
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 Center 및 File Exchange에서 Numbers and Precision에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!