Large integer rounding problem
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
HAORAN SUN
le 13 Juil 2020
Commenté : HAORAN SUN
le 14 Juil 2020
I have a question about an equation when I put it in Matlbab.
We know (1+x/n)^n = e^x as n goes infinity. However, the LHS gives me 1 when I use a small value of x and a large integer value of n(1e15, for example). Is there a better way I can compute this so that the final value is closer to e^x? Thank you so much!
0 commentaires
Réponse acceptée
David Goodmanson
le 14 Juil 2020
Modifié(e) : David Goodmanson
le 14 Juil 2020
Hello HS,
you ran into the limits of double precision numbers. However,
(1+x/n)^n = e^x*f % f = correction factor that is nearly 1 for large n
f = e^(-x)*(1+x/n)^n
% in terms if the log,
log(f) = log(e^(-x)) + log((1+x/n)^n)
= -x + n*log(1+x/n)
% taylor series for log about x = 1
= -x + n*( x/n -(x/n)^2/2 +(x/n)^3/3 -(x/n)^4/4 ...)
= -x^2/(2*n) +x^3/(3*n^2) -x^4/(4*n^3) ...
For large n, the log of the correction factor is -x^2/(2*n) to lowest order. That's the basic result.
You can exponentiate this to get f itself, but again for large n you will run into the limits of double precision. Using the taylor series for exp, you have
f ~~ exp(-x^2/(2*n)) ~~ 1 -x^2/(2*n)
showing the correction factor itself to first order. From this you can estimate how large n has to be until the second term goes to about 1e-15 and is no longer describable in double precision.
You can expand out to many more decimal places with vpa, but storing 1000 decimal places is not nearly as important as knowing the basic behavior for large n.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Numbers and Precision dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!