フィルターのクリア

Geometric series that halves but prints first 10 elements i.e. 1/2,1/4....,1/1028

1 回表示 (過去 30 日間)
Zachary Holmes
Zachary Holmes 2015 年 12 月 10 日
編集済み: Stephen23 2015 年 12 月 10 日
My solution is:
for i=1:10
GS(i)=1/(2)^2;
fprintf('GS is: ',GS(i))
end
however it only prints 1/4. How would i fix this??

回答 (2 件)

Walter Roberson
Walter Roberson 2015 年 12 月 10 日
"For a geometric sequence so always be multiplying the previous value by the multiplier, not the initial value."

Stephen23
Stephen23 2015 年 12 月 10 日
編集済み: Stephen23 2015 年 12 月 10 日
Much neater than using a loop:
>> 1./pow2(1:10)
ans =
0.5000 0.2500 0.1250 0.0625 0.0313 0.0156 0.0078 0.0039 0.0020 0.0010
or perhaps
>> fprintf('GS is: %f\n',1./pow2(1:10))
GS is: 0.500000
GS is: 0.250000
GS is: 0.125000
GS is: 0.062500
GS is: 0.031250
GS is: 0.015625
GS is: 0.007813
GS is: 0.003906
GS is: 0.001953
GS is: 0.000977
or perhaps even
>> rat(1./pow2(1:10))
ans =
1 + 1/(-2)
0 + 1/(4)
0 + 1/(8)
0 + 1/(16)
0 + 1/(32)
0 + 1/(64)
0 + 1/(128)
0 + 1/(256)
0 + 1/(512)
0 + 1/(1024)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by