Can anyone tell me the error with sum function?

1 Ansicht (letzte 30 Tage)
Swati Sarangi
Swati Sarangi am 11 Dez. 2019
Kommentiert: Bandar am 11 Dez. 2019
r=0.5;
n=0:10;
x=r.^n
s=sum(x)
What's the error with s?
  3 Kommentare
Alex Mcaulley
Alex Mcaulley am 11 Dez. 2019
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 am 11 Dez. 2019
The code is running just fine.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Steven Lord
Steven Lord am 11 Dez. 2019
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.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by