Matlab approximate_e problem Write a function called approximate_e that uses the
ID: 3859608 • Letter: M
Question
Matlab approximate_e problem
Write a function called approximate_e that uses the following formula to compute e, Euler's number: e = sigma^infinity_k = 0 1/K! = 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 exp (1) (i.e., the value returned MATLAB's built-in function) by no more than the positive scalar, delta, which is the only input argument. The first output of the function is the approximate value of e, while the second is k. You are not allowed to use the built-in function factorial.Explanation / Answer
Create file named fact.m and paste given code into it.(Note that filename must be fact.m)
fact.m
function f = fact(n)
f = 1;
for i = 1:n
f = f*i;
end
end
Create file named approximate_e.m and paste given code into it.(Note that filename must be approximate_e.m)
approximate_e.m
function [value,k] = approximate_e(delta)
format long;
tmp = 0;
k = 0;
while(abs(tmp - exp(1)) > delta )
tmp = tmp + 1/(fact(k)) ;
k = k+1;
end
value = tmp;
end
Note: Keep both fact.m and approximate_e.m files into same folder and run approximate_e.m file only.
Sample Output: