symbolic toolbox problem ?

3 ビュー (過去 30 日間)
Mohammad Ehsani
Mohammad Ehsani 2020 年 8 月 10 日
編集済み: John D'Errico 2020 年 8 月 10 日
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 件のコメント
Star Strider
Star Strider 2020 年 8 月 10 日
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.
.

サインインしてコメントする。

回答 (2 件)

hosein Javan
hosein Javan 2020 年 8 月 10 日
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 2020 年 8 月 10 日
編集済み: John D'Errico 2020 年 8 月 10 日
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.

カテゴリ

Help Center および File ExchangeNumbers and Precision についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by