Set working precision in Matlab
15 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Giancarlo Nino
el 12 de Dic. de 2016
Comentada: Jan
el 10 de Mzo. de 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).
0 comentarios
Respuesta aceptada
Walter Roberson
el 12 de Dic. de 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 comentarios
Más respuestas (2)
John D'Errico
el 12 de Dic. de 2016
Editada: John D'Errico
el 12 de Dic. de 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 comentarios
Jan
el 10 de Mzo. de 2023
Moved from flags:
Andrew Wade wrote: "Sexist analogies probably don't have a place here anymore."
Jan
el 10 de Mzo. de 2023
The answer does not contain rudeness degradation, but bad examples for the topic of nutrition.
Giancarlo Nino
el 12 de Dic. de 2016
1 comentario
Walter Roberson
el 12 de Dic. de 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)
Ver también
Categorías
Más información sobre Numbers and Precision en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!