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
21 ビュー (過去 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 件のコメント
champions2015
2017 年 8 月 31 日
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
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
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 件のコメント
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
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
0 件のコメント
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
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
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
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
0 件のコメント
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
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
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 Center および File Exchange で Performance and Memory についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!