Can anyone tell me the error with sum function?

r=0.5;
n=0:10;
x=r.^n
s=sum(x)
What's the error with s?

3 comentarios

Adam
Adam el 11 de Dic. de 2019
No idea. You tell us! If you get an error message then post it here, if you don't then what kind of error are you talking about?
What is wrong? It is the summation of a geometric progression and the result seems ok:
%Your code
r=0.5;
n=0:10;
x=r.^n
s=sum(x)
%General formula
s2 = x(1) + r*(x(end)-1)/(r-1)
s =
1.9990
s2 =
1.9990
Bandar
Bandar el 11 de Dic. de 2019
The code is running just fine.

Iniciar sesión para comentar.

Respuestas (1)

Steven Lord
Steven Lord el 11 de Dic. de 2019

0 votos

Based on prior experience, I suspect you received the error "Array indices must be positive integers or logical values." If that's the case, clear the variable you've created named sum which is preventing you from calling the built-in sum function.

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 11 de Dic. de 2019

Comentada:

el 11 de Dic. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by