# 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

10 views (last 30 days)

Show older comments

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

##### 14 Comments

champions2015
on 31 Aug 2017

Jorge Briceño
on 4 Feb 2018

Hi everyone,

Why do not you try this? I explained what the variables are doing.

function [approx_e, k] = approximate_e (delta)

% Set the variables for the while loop.

k = 0;

fact = 1;

approx_e=1;

while abs(approx_e - exp(1)) > delta

% The counter k is necesary for factorial calculus. If you put it after

% the approx_e, it will add an addional number since the condition will be

% reevaluated.

k=k+1;

% For factorial calculus one could use:

% fact = fact * (1/k) or prod([1 : n])

% fact and approx_e is calculated to recursive method.

fact = fact * (1/k);

approx_e = approx_e + fact;

end

approx_e;

k;

end

I think you should also review the if statements, since you could try using if-elseif statements, in other to make your code a bit more readable. Also, try to add notes (ctrl R) so you will make yourself aware of what you are coding.

### Accepted Answer

James Tursa
on 19 May 2017

You need to change your while loop criteria to compare your estimate with exp(1). E.g.,

while abs(est-exp(1)) > delta

##### 16 Comments

Jan
on 28 May 2017

@Wasi: Then restore the former version from your backup. Do you know how to use the "Fromer versions" provided since Windows 7?

If you cannot m,anage to restore the correct version, post the current code and explain, what does not work as expected.

### More Answers (4)

Srishti Saha
on 1 May 2018

Tested this:

%function to compute euler's number

function [approx_e, k] = approximate_e (delta)

% Set the variables for the while loop.

k = 0;

fact = 1;

approx_e=1;

while abs(approx_e - exp(1)) > delta

k=k+1;

fact = fact * (1/k);

approx_e = approx_e + fact;

end

approx_e;

k;

end

##### 0 Comments

Jan
on 28 May 2017

Edited: Jan
on 28 May 2017

function [est, k] = approximate_e(delta)

k = 0; % Start with 0'th iteration

est = 0;

want = exp(1); % Do not calculate this repeatedly

Fac = 1;

while abs(est - want) > delta && k < 10000

k = k + 1; % *Before* appending the new term!

if k <= 2

est = est + 1;

else

Fac = Fac * (k - 1); % Cheaper than PROD()

est = est + 1 / Fac;

end

end

##### 9 Comments

Ranil Fernando
on 13 May 2018

I'm getting the bellow error for you code.

Problem 4 (approximate_e): Error using input Cannot call INPUT from EVALC.

Error in factorial (line 7) n=input('Specify a integer to calculate factorial : ');

Error in hw6

Error in hw6

Error in hw6

Error in hw6

Error in hw6

Do you have any idea about why is it happening ?

Jan
on 13 May 2018

rishabh gupta
on 4 Feb 2018

Edited: Walter Roberson
on 13 May 2018

This could also be done without using if condition, and it works too...

function [ app_e , k ] = approximate_e( delta )

app_e = 1;

k=0;

facto=1;

exp_1 = exp(1);

while abs(app_e - exp_1) > delta

k=k+1;

facto = facto*k;

app_e = app_e + 1/facto;

end

app_e = app_e;

k

end

##### 0 Comments

Ranil Fernando
on 13 May 2018

Edited: Ranil Fernando
on 13 May 2018

I'm getting the bellow error for my code when using the grader; Any one hen help me with why, many thanks. But the code works just fine when using without the grader.

Problem 4 (approximate_e): Error using input Cannot call INPUT from EVALC.

Error in factorial (line 7) n=input('Specify a integer to calculate factorial : ');

Error in hw6

Error in hw6

Error in hw6

Error in hw6

Error in hw6.

______________________________________________________________________________

And the code is;

function [app_e,k] = approximate_e(delta)

sum = 0;

app_e = sum;

k = 0;

fact = 0;

while (exp(1)-app_e) > delta

if k==0 || k==1

fact=1;

else

fact = fact*k;

end

k = k + 1;

sum = sum + 1/fact;

app_e = sum;

end

end

##### 2 Comments

Walter Roberson
on 13 May 2018

Your file hw6 does not have any obvious relationship to approximate_e .

Your file hw6.m is calling a function named factorial, but you appear to have supplied your own factorial.m that is using input() to talk to the user. You should be checking

which factorial

and removing or renaming your factorial.m that you find there.

By the way, I recommend you never name a variable sum, as it is a fact of life that soon you will try to call the MATLAB function sum() from within a workspace where you defined sum as a variable, and then you will have trouble figuring out what is going on.

Ranil Fernando
on 13 May 2018

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!