How to find sum using single statement?

4 ビュー (過去 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 Center および File ExchangeDynamic System Models についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by