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
Afficher commentaires plus anciens
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 commentaires
Wasi von Deutschland
le 19 Mai 2017
Wasi von Deutschland
le 28 Mai 2017
Jan
le 28 Mai 2017
@Wasi: Please try to write complete sentences. How can the value of n what? The verb is missing. If you have really severe problems with English, use the Google translator or a German Matlab forum. Then take into account, that there is no "above equation". All I see is failing code.
Wasi von Deutschland
le 28 Mai 2017
Jan
le 28 Mai 2017
Sorry, no, I do not understand what you are asking for. The shown code is a fragment only and I cannot guess, how the complete code looks like.
In your code a smaller delta causes n to grow. With some maths your can determine the relation between delta and n.
Wasi von Deutschland
le 28 Mai 2017
Wasi von Deutschland
le 28 Mai 2017
Wasi von Deutschland
le 28 Mai 2017
Wasi von Deutschland
le 28 Mai 2017
Wasi von Deutschland
le 28 Mai 2017
If you are in the 2nd iteration, so k == 2, you get the estimation:
est = 2
This is less than 1.0 away from the correct value. The grader tells you, that your code replies the wrong value for delta=1. And what is the expected value? k=2.
What happens after est was calculated? k is increased. Therefore k=3 is replied, as if the estimation is satisfying at k=3.
Increase k before adding the new term. See me answer below.
champions2015
le 31 Août 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
le 4 Fév 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.
Réponse acceptée
Plus de réponses (4)
Srishti Saha
le 1 Mai 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
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 commentaires
Wasi von Deutschland
le 28 Mai 2017
Jan
le 29 Mai 2017
Please explain with any details what "doesn't work" mean. I am able to run this code and I get the results I expect. Does "doesn't work" mean, that the grader is not satisfied? Is the error message still (phew, please do not post messages as screenshot, because this does not allow to copy parts of it - insert them as text. Thanks.):
Your function made an error for argument(s) 1
? It is tedious to pick the meaning of your answer from different comments. Please try to make it as easy as possible to assist you.
Of course you are not a Matlab expert. I'm not sure, if I am an expert also. But we do not want to master Matlab, but to solve this small problem only. Find out, what the grader wants. What is the correct answer for "argument(s) 1"? What do you get, if you solve the problem manually?
I think it is:
est = 2, k = 2
because this is:
est = 1 + 1 = 2
distance to exp(1) is 0.718 and therefore < delta=1.0
Do you get the same result and do you have the same expectation? If so, blame the grader. "Your function made an error for argument(s) 1" is a dull and mean message. It is impossible to teach students (and their helpers in to forum) with such dump messages. Sorry for getting emotional, but I'm convinced that such auto-grading by silly software is frustrating only and therefore reduces the ability to learn. I know, that many teachers have to use such auto-grading, because they have to care about 200 students. But then the quality of the teaching suffers. Bad teaching means, that the work is done in the forum. Sigh.
Wasi von Deutschland
le 29 Mai 2017
You have posted the same output twice. When you claim "Sometimes I get values of 'k' right and sometimes 'est' for different arguments", please be so kind and post the corresponding values. Show us the wanted output and the output of the current function. As far as I can see, my k values are 1 to high. Then start with:
k = 0; % Start with 0'th iteration
est = 1; % Instead of my approach est = 0
The changes you have to apply are very easy and basic. You should be able to do this by your own. Otherwise I cannot imagine how you will proceed with the course. We are dicsussing here for 12 days yet.
Wasi von Deutschland
le 4 Juin 2017
Sai Rajeev Deavaragudi
le 6 Juin 2017
Modifié(e) : Sai Rajeev Deavaragudi
le 6 Juin 2017
okay so i just modified your code a bit. check it out! it works with the grader.
function [est, k] = approximate_e(delta)
k = 0; % Start with 0'th iteration
est = 1;
x = exp(1);
F = 1;
while abs(est - x) > delta && k < 10000
k = k + 1;
if k <2
est = est + 1;
else
F = F * k;
est = est + 1 / F;
end
end
Wasi von Deutschland
le 9 Juin 2017
Ranil Fernando
le 13 Mai 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
le 13 Mai 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.
rishabh gupta
le 4 Fév 2018
Modifié(e) : Walter Roberson
le 13 Mai 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
le 13 Mai 2018
Modifié(e) : Ranil Fernando
le 13 Mai 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 commentaires
Walter Roberson
le 13 Mai 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
le 13 Mai 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.
Catégories
En savoir plus sur Mathematics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



