sum of real numbers up to n using recursive code
14 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Will Murphy
il 15 Feb 2020
Commentato: Will Murphy
il 16 Feb 2020
I'm trying to make more sense of recursive code as I have only very recently started using matlab. I was wondering how I would go about finding the sum of all real numbers up to x using recursive code, I have done it using if / for / while loops however after trying to self teach myself I would expect to go about this by:
function y = sum(x)
if x > 0
y = x * (x+1) / 2
end
elseif x <= 0
y = 'does not exist'
end
end
disp(y)
I understand that I am probably wrong, in which case how would I go better about this task. Any help would be appreciated.
2 Commenti
Image Analyst
il 15 Feb 2020
Modificato: Image Analyst
il 15 Feb 2020
You can edit the code to fix it you know. So go ahead and add the end.
How are you calling this? What is the initial value of x you're using? I assume the x are all integers, right? Is x a scalar or vector? And you have a finite number of them. Because the sum of all real numbers is infinite.
Risposta accettata
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Function Creation in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!