How to find sum using single statement?

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 日

0 投票

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 件)

カテゴリ

ヘルプ センター および File ExchangeDynamic System Models についてさらに検索

タグ

質問済み:

2014 年 10 月 27 日

編集済み:

2014 年 10 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by