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 次查看(过去 30 天)
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 个评论
Jorge Briceño
Jorge Briceño 2018-2-4
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.

请先登录,再进行评论。

采纳的回答

James Tursa
James Tursa 2017-5-19
You need to change your while loop criteria to compare your estimate with exp(1). E.g.,
while abs(est-exp(1)) > delta
  16 个评论
Wasi von Deutschland
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 2017-5-28
@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.

请先登录,再进行评论。

更多回答(4 个)

Srishti Saha
Srishti Saha 2018-5-1
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 2017-5-28
编辑:Jan 2017-5-28
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 个评论
Ranil Fernando
Ranil Fernando 2018-5-13
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 2018-5-13
@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.

请先登录,再进行评论。


rishabh gupta
rishabh gupta 2018-2-4
编辑:Walter Roberson 2018-5-13
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 2018-5-13
编辑:Ranil Fernando 2018-5-13
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 个评论
Walter Roberson
Walter Roberson 2018-5-13
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 2018-5-13
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by