Large integer rounding problem

2 次查看(过去 30 天)
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!

采纳的回答

David Goodmanson
David Goodmanson 2020-7-14
编辑:David Goodmanson 2020-7-14
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numbers and Precision 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by