symbolic toolbox problem ?

2 views (last 30 days)
Mohammad Ehsani
Mohammad Ehsani on 10 Aug 2020
Edited: John D'Errico on 10 Aug 2020
I am not sure why my symbolic toolbox truncate the numbers up to 4 digits?
vpa(pi,16);
ans =
3.1416
rounds the pi up to 4 digits
  1 Comment
Star Strider
Star Strider on 10 Aug 2020
It works correctly for me:
vpa(pi,16)
produces:
ans =
3.141592653589793
That also overrides a previous digits setting:
digits(4)
so that is not the problem, either.
.

Sign in to comment.

Answers (2)

hosein Javan
hosein Javan on 10 Aug 2020
by default matlab vpa uses 32 digits.
old = digits(4)
vpa(pi)
old =
32
ans =
3.142
old = digits(9)
vpa(pi)
old =
4
ans =
3.14159265
if the problem persists close and reopen matlab and it would load its defauld precision of 32 digits.

John D'Errico
John D'Errico on 10 Aug 2020
Edited: John D'Errico on 10 Aug 2020
A strong possibility is you have overwritten/overloaded the stored value of pi.
>> clear pi
>> pi
ans =
3.14159265358979
So, by default, pi is 3.1459265358979 in MATLAB. That is the double precision value stored in the variable pi.
Now, what happens with the symbolic toolbox?
>> digits 40
digits 40
>> vpa(pi)
ans =
3.141592653589793238462643383279502884197
sym(pi)
ans =
pi
So the symbolic toolbox knows what pi is. I have even checked, and it indeed knows those are the first 40 digits of pi. I don't know how far it goes.
hpf('pi',40)
ans =
3.141592653589793238462643383279502884197
My own toolbox, HPF, also knows the first half milloin digits of pi or so. I agrees with those 40 digits.
HOWEVER, what if you decide to overwrite pi with a variable of your own? DON'T DO THIS!!!!!!!!!!!!!
pi = 3.1416
pi =
3.1416
>> vpa(pi)
ans =
3.1416
So now, I have overwritten pi. MATLAB no longer understands that pi is that long string of digits. It thinks it to be 3.1416. BAD NEWS.
Fix that by clearing pi, and NOT overloading pi in the future.
clear pi
Again, please don't overload pi, at least not unless you happen to work for the board of education in some school districts that I will choose to leave nameless.

Community Treasure Hunt

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

Start Hunting!