MATLAB Answers

Large integer rounding problem

조회 수: 1(최근 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!

  댓글 수: 0

댓글을 달려면 로그인하십시오.

채택된 답변

David Goodmanson
David Goodmanson 14 Jul 2020
편집: David Goodmanson 14 Jul 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.

  댓글 수: 1

HAORAN SUN 14 Jul 2020
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변(0개)



Community Treasure Hunt

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

Start Hunting!

Translated by