MATLAB: How to sum over indices

sum

Given for some , how can one writes the sum using the Matlab Code. When I try, I get confused. Please Help me out.
I started by fixing my m=5 say, and write an=[1,-1,0,1,1] say, and with r=0.5, I generate r^m=3.125e-07. Then I don't know how to fit this into the for environment.
This is as follows:
an=[1,-1,0,1,1];
b0=an(1); rm=3.125e-07;
for k=1:5;
b(k)=b0+rm^k*a(k)
end
what is wrong here please? I cannot proceed from here. I appreciate any assistance how to sort this out.

Best Answer

If you assume, that something is wrong, mention, what you observe. This is better than letting the readers guess, what the problem is.
You have defined an, but use a later. For me this code is running, when I change "an" to "a":
a = [1,-1,0,1,1];
b0 = a(1);
rm = 3.125e-07;
for k = 1:5
b(k) = b0 + rm^k * a(k);
end
This creates the elements of the sum, but not the sum itself. Perhaps you want:
S = 0;
for k = 1:5
S = S + rm^k * a(k);
end
or simpler:
k = 1:5;
S = sum(rm.^k .* a);