Effacer les filtres
Effacer les filtres

Everytime I run my code I keep getting an infinite recursion error. My code is posted below. Can you please help me figure out why it keeps giving me this error?

2 vues (au cours des 30 derniers jours)
It flags me at the y(:, i) = x.^(i)./factorial(i); line below.
function exp = expapprox(x,n)
n = 5;
x = 1:5;
% number of iterations for loop
for i = 1:n
y(:, i) = x.^(i)./factorial(i);
end
exp = 1+ sum(y);
actexp = exp(x);
plot(actexp, expapprox(x,5))
end

Réponse acceptée

James Tursa
James Tursa le 18 Juil 2018
Modifié(e) : James Tursa le 18 Juil 2018
Your plot statement calls your function expapprox, so it gets into an infinite recursion. You've got another problem as well, since you create a variable named "exp" which is shadowing the MATLAB function of the same name. Pick a different variable name.
  2 commentaires
Alison Michell
Alison Michell le 19 Juil 2018
Thank you for your help! I can change the name of the variable exp, however, is there an alternative way for me to plot the values from my expapprox function without causing the infinite recursion?
James Tursa
James Tursa le 19 Juil 2018
Modifié(e) : James Tursa le 19 Juil 2018
E.g., something like this:
expcumsum = 1 + cumsum(y,2);
actexp = repmat(exp(x(:)),1,n);
figure;
plot([expcumsum;actexp]','.-');
xlabel('Number of Terms');
ylabel('exp value');
title('Taylor Series exp convergence')
grid on
The value returned from the function could be something like this:
function expvalue = expapprox(x,n)
:
expvalue = expcumsum(:,end);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by