How to sum over indices?

77 次查看(过去 30 天)
Omar
Omar 2019-11-28
评论: Omar 2019-11-28
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.

采纳的回答

Jan
Jan 2019-11-28
编辑:Jan 2019-11-28
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);
  1 个评论
Omar
Omar 2019-11-28
Thanks a lot for your response. This is really helpful. I got solution to the problem.

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by