MATLAB: Problems in accuracy assessment

accuracy assessmentpisymbolic computation

Dear friends I tried continued fractiona approximation to pi, and got the following rational approximation
1244969988745789040106366155256015114976454553337906033890313/396286255419907262612262286579004636343912658949984351074158
but if I try to find the difference of it with pi, I failed, since
>> sym(1244969988745789040106366155256015114976454553337906033890313/396286255419907262612262286579004636343912658949984351074158)
I got pi directly. How can I assess the difference? Thanks

Best Answer

Using my HPF toolbox, it looks to be correct to about 120 significant digits.
double(log10(abs(hpf('1244969988745789040106366155256015114976454553337906033890313',1000)/hpf('396286255419907262612262286579004636343912658949984351074158',1000) - hpf('pi',1000))))
ans =
-119.53
Looking back at what you did, this is wrong. Why?
sym(1244969988745789040106366155256015114976454553337906033890313/396286255419907262612262286579004636343912658949984351074158)
ans =
pi
It is wrong because MATLAB will first compute the ratio as a DOUBLE precision number, and only then pass it to sym. sym then recognizes the ratio as a double precision approximation to pi, so it returns that as the result.
If you insist on doing this with the symbolic toolbox, we might try...
log10(abs(vpa(sym('1244969988745789040106366155256015114976454553337906033890313/396286255419907262612262286579004636343912658949984351074158 - pi'),200)))
ans =
-119.52655990941722562288816970381
Of course, if you want a better approximation, say accurate to 250 digits, HPF can provide it too.
[N,D] = rat(hpf('pi',500),hpf('1e-250'))
N =
165213461336854892880967397129207248425612593828416835132438559586109735260365863184040108685528782183687397179966550074377225
D =
52589078074164381557662496030246690228630477463437042437123678996605078304650603819026850043803035007564529614801553827009746
If you need convincing that this ratio is indeed accurate to 250 digits for pi, try this:
double(log10(abs(N/D - hpf('pi',1000))))
ans =
-250.05