vpa is not outputing enough digits
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Diego Mercado
le 14 Oct 2021
Commenté : Diego Mercado
le 17 Oct 2021
I'm having an error where vpa is only diplaying 5 digits when I need more. The code i have is:
syms x y
dx = 0.05
f(x,y) = x*cos(x) + y
df = diff(f,x) + f*diff(f,y)
a = vpa(df(0,.5)/factorial(2)*dx^2)
Obviosly here it outputs correctly to a = 0.001875, but on my end it outputs a = 0.0019. I've checked digits is set to the default 32 for vpa. The same thing occurs with
vpa(pi)
It outputs 3.1416 and not
vpa(pi)
0 commentaires
Réponse acceptée
John D'Errico
le 14 Oct 2021
Modifié(e) : John D'Errico
le 14 Oct 2021
I think you may have a setting messed up on your computer. For example...
X = sym(3)/32;
vpa(X)
ans =
0.09375
But now, if I try this:
sympref('floatingpointoutput',true);
vpa(X)
ans =
0.0938
Yet I have digits set as 32.
digits
Digits = 32
So you may want to reset sympref, as:
sympref('floatingpointoutput',false);
Plus de réponses (1)
KSSV
le 14 Oct 2021
Modifié(e) : KSSV
le 14 Oct 2021
It seems a is a rational number so there are no digits to display else from zeros. Where as pi is a irrational number, so you have lot of digits to display.
For more discussion refer here: https://in.mathworks.com/matlabcentral/answers/320024-how-can-i-test-if-a-number-is-irrational
3 commentaires
KSSV
le 14 Oct 2021
You can specify the number of digits you want in vpa.
vpa(pi,12)
vpa(pi,24)
Diego Mercado
le 14 Oct 2021
Modifié(e) : Diego Mercado
le 14 Oct 2021
Voir également
Catégories
En savoir plus sur Number Theory dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!