How to sum over indices?
78 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Jan
on 28 Nov 2019
Edited: Jan
on 28 Nov 2019
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);
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!