vpa is not outputing enough digits

4 visualizaciones (últimos 30 días)
Diego Mercado
Diego Mercado el 14 de Oct. de 2021
Comentada: Diego Mercado el 17 de Oct. de 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
dx = 0.0500
f(x,y) = x*cos(x) + y
df = diff(f,x) + f*diff(f,y)
a = vpa(df(0,.5)/factorial(2)*dx^2)
a = 
0.001875
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)
ans = 
3.1415926535897932384626433832795

Respuesta aceptada

John D'Errico
John D'Errico el 14 de Oct. de 2021
Editada: John D'Errico el 14 de Oct. de 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);
  1 comentario
Diego Mercado
Diego Mercado el 17 de Oct. de 2021
This was is! Thank you, don't know how that happened but gald its fixed.

Iniciar sesión para comentar.

Más respuestas (1)

KSSV
KSSV el 14 de Oct. de 2021
Editada: KSSV el 14 de Oct. de 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.
  3 comentarios
KSSV
KSSV el 14 de Oct. de 2021
You can specify the number of digits you want in vpa.
vpa(pi,12)
ans = 
3.14159265359
vpa(pi,24)
ans = 
3.14159265358979323846264
Diego Mercado
Diego Mercado el 14 de Oct. de 2021
Editada: Diego Mercado el 14 de Oct. de 2021
Yes, I know that you can do that, but that's not the issue. No matter what number I use, I am always getting 4 digits after the decimal as if I was typing vpa(pi,4), and I've checked with
digits
Digits = 32
to confirm that I have the default setting of 32.

Iniciar sesión para comentar.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by