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

20 visualizaciones (últimos 30 días)
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 comentarios
champions2015
champions2015 el 31 de Ag. de 2017
why is it not 'while abs(e-exp(1))<=delta' as it asks that it 'must not differ by no more' than delta?
Jorge Briceño
Jorge Briceño el 4 de Feb. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

James Tursa
James Tursa el 19 de Mayo de 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 comentarios
Wasi von Deutschland
Wasi von Deutschland el 27 de Mayo de 2017
Hi James could you please do me a favor and tell me how to get solution for above question actually once I got the answer correct. Now it's incorrect again.
Jan
Jan el 28 de Mayo de 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.

Iniciar sesión para comentar.

Más respuestas (4)

Srishti Saha
Srishti Saha el 1 de Mayo de 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

Jan
Jan el 28 de Mayo de 2017
Editada: Jan el 28 de Mayo de 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 comentarios
Ranil Fernando
Ranil Fernando el 13 de Mayo de 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
Jan el 13 de Mayo de 2018
@Ranil: "Error using input Cannot call INPUT from EVALC." - My code does not contain an input command. So what do you call "your code" exactly? The message is clear: Use an input argument for the function, not an input() command.

Iniciar sesión para comentar.


rishabh gupta
rishabh gupta el 4 de Feb. de 2018
Editada: Walter Roberson el 13 de Mayo de 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

Ranil Fernando
Ranil Fernando el 13 de Mayo de 2018
Editada: Ranil Fernando el 13 de Mayo de 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 comentarios
Walter Roberson
Walter Roberson el 13 de Mayo de 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
Ranil Fernando el 13 de Mayo de 2018
This really helps, Thanks a lot @Walter. Thanks for the advice on not to make a variable with a name sum. I've made the correction. Many thanks.

Iniciar sesión para comentar.

Categorías

Más información sobre Performance and Memory en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by