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

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

Jan
Jan le 19 Mai 2017
Modifié(e) : Jan le 19 Mai 2017
You forgot to mention a question. It looks like you have tried to insert the question in the title, where it is cropped.
You set est 3 time: at first to 0, then to 1 and finally to 2, becfore the processing begins. This is not useful.
What should I do instead? could you please elaborate a bit more.
Jan Simon could you please tell me how one can the value of n in the above equation.
@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.
Sorry. My English language is not at advance level. However, my question was '' In the above mentioned code I could not understand how the value of 'n' varies. We give input as ''delta'' and how the values of output n and est do change? Secondly, I got
[est n]=approximate_e(1)
est =2
n = 3
BUT the value of n should have been n=1 rather than 3. I hope you can understand my question now.
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.
This is the question and error. Actually my studies are in English that's why I am using English forum. I'm sorry but for the next time. I'll try to make you clear.
function [est, k ] = approximate_e( delta )
k =1;
est = 0;
while abs(est-exp(1))> delta
if k ==1;
est = 1;
end
if k==2;
est=2;
end
if k >2
est = est+1/prod(1:(k-1));
end
k = k + 1;
if k >10000
break;
end
end
my current code
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.
why is it not 'while abs(e-exp(1))<=delta' as it asks that it 'must not differ by no more' than delta?
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.

Connectez-vous pour commenter.

 Réponse acceptée

You need to change your while loop criteria to compare your estimate with exp(1). E.g.,
while abs(est-exp(1)) > delta

16 commentaires

I have changed it but it doesn't work it makes an error for argument 1.
Your exact code, with that one change I show above for the while loop criteria, works for me. Example output on my machine:
>> format long
>> approximate_e(1e-4)
ans =
2.718253968253968
>> ans-exp(1)
ans =
-2.786020507716813e-005
>> approximate_e(1e-8)
ans =
2.718281826198493
>> ans-exp(1)
ans =
-2.260552633970292e-009
>> approximate_e(1e-12)
ans =
2.718281828458230
>> ans-exp(1)
ans =
-8.153477892847150e-013
Hi, It's my exercise and I'm beginner but my automatic grader says it's incorrect. I don't know why.? I have been trying to solve it more than an hour. Could you please explain a bit more then may be I can understand better.
Your "automatic grader" could be looking for any number of things. Variable names, order of arguments, definition of n, etc. We would have to know precisely what this "grader" is looking for in order to help with that. All I can say at the moment is that the code (with that one correction) does produce an estimate of e according to your formula. Maybe change the value of n that is returned so that it only includes the actual terms that were used? E.g., maybe put this statement at the very end of your code so that it only accounts for the terms that were actually used to calculate est:
n = n - 1;
(Alternatively, you could change the way n counts. E.g., start it at 0 and then do the n = n + 1 as the first statement inside the while loop. Then you wouldn't need the n = n - 1 at the end of the code).
Maybe change your output argument variable names to e and k? E.g., assuming you keep your current code as-is except for that one while loop change noted above, maybe something like this?
function [e, k] = approximate_e( delta )
:
e = est;
k = n - 2;
Although, I would again note, that it would have been easier to use the same counting that is in the formula for k than to do a different counting with n and then have to adjust it at the end.
Does the automated grader provide any information other than "it's incorrect" when it runs on your code? Does it indicate the specific test case for which your code didn't return what it expected?
Please post the current code you are using.
function [est, n ] = approximate_e( delta )
%APPROXIMATE_E Summary of this function goes here
% Detailed explanation goes here
n =1;
est = 0;
while abs(est-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
end
question is already written in the above comments. Please tell me where am I making mistake.
You did not implement my suggestion as written. Compare your while test against my while test ... they are not the same.
could you please write me the code. So, I can compare here I'm going tangent.
I already have written the code. It is above. All you have to do is look and compare my code with your code. But, as simple as this seems to me, I will repeat it here at your request:
while abs(est-exp(1)) > delta % <-- my code
while abs(est-exp(1)> delta) % <-- your code
See the difference?
I got it. really thanks but only difference was parentheses.
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.
@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.

Connectez-vous pour commenter.

Plus de réponses (4)

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

Jan Simon I am sorry but unfortunately this code doesn't work. I can not say anything because I am not expert in MATLAB. My autograder doesn't say anything either but same error comes up as before.
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.
Jan Simon I have talked with my mentor and asked my code seems good but auto-grader says your function is incorrect and he said your values should look like the values given below. I tried myself but I couldn't figure out where can I change to get the function right. Sometimes I get values of 'k' right and sometimes 'est' for different arguments.
[est,k] = approximate_e(1)
est =
2
k =
1
>> [est,k] = approximate_e(0.5)
est =
2.500000000000000
k =
2
>> [est,k] = approximate_e(0.2)
est =
2.666666666666667
k =
3
>> [est,k] = approximate_e(1)
est =
2
k =
1
>> [est,k] = approximate_e(0.5)
est =
2.500000000000000
k =
2
>> [est,k] = approximate_e(0.2)
est =
2.666666666666667
k =
3
>>
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.
Actual expected value is given above but my function still gives value for argument(0.5) is 3 and 2 to est and k respectively. However, actual value should have been like 2.5 and 2 to est and k respectively. Could you please tell me why is it so
Feedback: Your function performed correctly for argument(s) 1
Feedback: Your function made an error for argument(s) 0.5
Your solution is _not_ correct.
>> [est k]=approximate_e(0.5)
est =
3
k =
2
My current code is here
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 - 1);
est = est + 1 / F;
end
end
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
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 ?
@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.

Connectez-vous pour commenter.

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
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

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.
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.

Connectez-vous pour commenter.

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!

Translated by