math equation using function
Mostrar comentarios más antiguos
function will recive input Value a and it caluclate its approximate value to e. e=1/e~~(1-1/n)^n. e value that the difference between the approximation and the actual value of e is smaller than err.function should return the corresponding value of n.
Respuestas (1)
Image Analyst
el 24 de Nov. de 2019
I don't know what that means. Let's say that a=8, and e=2.718281828. Do you simply want to return an ouput = (exp(1) - a)??? And do you just want to do a while loop, incrementing the value of n, until (1-1/n)^n is less than 1/e ???
n=0;
e1 = 1 / exp(1)
while difference > e1
difference = abs(e1 - (1-1/n)^n);
n = n + 1;
end
n = n - 1; % Undo last addition before the break.
6 comentarios
Roger Nadal
el 25 de Nov. de 2019
Editada: Walter Roberson
el 25 de Nov. de 2019
Image Analyst
el 25 de Nov. de 2019
It seems like they want you to compute e by summing a series. See if you can construct a while loop that accumulates a sum and breaks out of the loop when the error gets small enough. I think I've given you a good start above. Just construct the sum of the series.
Roger Nadal
el 25 de Nov. de 2019
Image Analyst
el 25 de Nov. de 2019
Correct. Do a for loop over, say a million iterations and break when the error is less. Since it seems to be homework, here's a hint
theSum = 0;
correctValue = 1 / exp(1); % or exp(1) depending on how you're constructing the series.
minAllowableError = whatever you want.
for n = 1 : 1000000
newTerm = ... compute the nth term.
theSum = theSum + newTerm;
theErrorSoFar = theSum - correctValue
if theErrorSoFar < minAllowableError
% The error is now small enough and we can bail out now.
break.
end
end
fprintf('We did %d iterations and got an error of %f.\n', n, theErrorSoFar)
Roger Nadal
el 26 de Nov. de 2019
Image Analyst
el 26 de Nov. de 2019
Well, only Walter here has the Mind Reading Toolbox, not me, so let's see what code you have so far, if you still want/need help. What modifications did you make to the snippet I gave you?
Categorías
Más información sobre Programming en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!