Can anyone tell me the error with sum function?

조회 수: 2 (최근 30일)
Swati Sarangi
Swati Sarangi 2019년 12월 11일
댓글: Bandar 2019년 12월 11일
r=0.5;
n=0:10;
x=r.^n
s=sum(x)
What's the error with s?
  댓글 수: 3
Alex Mcaulley
Alex Mcaulley 2019년 12월 11일
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 2019년 12월 11일
The code is running just fine.

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2019년 12월 11일
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.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by