I'm attempting to create a function that provides me with an estimate of e(exp(1)), and that provides me with information about the error within the function. Estimating e was not an issue, however, when attempting to use an if statement created an infinite recursion within my function.
Here is my function
function e = myExpFunction(i)
for i = 1:20
n = 10.^i;
e = (1+1./n).^n;
if abs(exp(1) - myExpFunction(i)) < abs(exp(1) - myExpFunction(i-1))
fprintf('The error always decreases as n increases.\n')
end
end
end
And here is the error message
myExpFunction(i)
Out of memory. The likely cause is an infinite recursion within the program.
Error in myExpFunction (line 5)
if abs(exp(1) - myExpFunction(i)) < abs(exp(1) - myExpFunction(i-1))
I have tried using a while loop to bound the value of i between 1 and 20 but got the same error message. So I suppose I must have created an infinite loop within those bounds.
I'm having trouble fixing this as I don't understand where in the program I have created an infinite recursion. If anyone could help me out and shed some light it would be a massive help, thanks.
Aucun commentaire:
Enregistrer un commentaire