MATLAB: Write a function called approximate_e that uses the following formula to compute e, Euler’s number: = 1 ! ∞ = 1+1+ 1 2 + 1 6 + 1 24 +⋯ Instead of going to infinity, the function stops at the smallest k for which the approximation differs from

approximate_ehomework

function [est, n ] = approximate_e( delta )
%APPROXIMATE_E Summary of this function goes here
% Detailed explanation goes here
n =1;
est = 0;
while abs(exp(1)> delta
if n ==1
est = 1;
end
if n == 2
est = 2;
end
if n >2
est = est+1/prod(1:(n-1));
end
n = n + 1;
if n >10000
break;
end
end
could you please tell me how one can solve above mention question. thanks

Best Answer

You need to change your while loop criteria to compare your estimate with exp(1). E.g.,
while abs(est-exp(1)) > delta
Related Question