How to find sum using single statement?

조회 수: 5 (최근 30일)
Airas Akhtar
Airas Akhtar 2014년 10월 27일
편집: Mischa Kim 2014년 10월 27일
I had to find the sum of the series 1/2+(1/2)^2+....(1/2)^100
why does my code not work?
(ones(1,100).*1/2).^([1:100])
it gives the answer zero.

채택된 답변

Mischa Kim
Mischa Kim 2014년 10월 27일
편집: Mischa Kim 2014년 10월 27일
Airas, use
f = sum(arrayfun(@(x) 0.5^x,1:100))
or, with your approach, add the sum()
f = sum((ones(1,100).*1/2).^([1:100]))

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dynamic System Models에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by