Geometric series that halves but prints first 10 elements i.e. 1/2,1/4....,1/1028
Afficher commentaires plus anciens
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??
Réponses (2)
Walter Roberson
le 10 Déc 2015
0 votes
"For a geometric sequence so always be multiplying the previous value by the multiplier, not the initial value."
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)
Catégories
En savoir plus sur Mathematics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!