MATLAB: How to write a MATLAB program which calculates the exact values for the geometric series

geometric seriesMATLAB

a/(1-r)=a+ar+ar^2…ar^6 0<r<1
Exact Values to calculate
  1. a=1 r=(1/7)
  2. a=3 r=(1/3)
  3. a=2 r=(1/9)
Please help. Thanks

Best Answer

a=1; %first term
r=1/7;
n=5; %number of values in geometric series
s = a*r.^(0:n-1)
If you want sum, then write
gp_sum=sum(a*r.^(0:n-1))