MATLAB: Find n for inverse of e approximation

inverse

Hi! I have to find an 'n' that approximates 1/exp(1) correctly to 0.0001, and then diplay which n that was.
% write code to compute the value of the inverse to within the tolerance:
inv = 1 / exp(1);
n = 1;
guess = (1-(1/n))^n;
while 0.0001 > abs(inv-guess)
n = n+1;
guess = (1-(1/n))^n;
end
% store the value of n needed to reach this accuracy:
accurate_n = n
But at the end,
accurate_n = 1 and I'm not sure why it is not treating it like a normal counter.
Anything helps!

Best Answer

According to what I understand, you are trying to get the value of accurate_n which gives you a tolerance of ‘0.0001’. The problem is not in your approach but your condition in the while loop
while 0.0001 < abs(inv-guess)
Will give the expected answer which is 1840’
Related Question