MATLAB Answers

How to sum over indices?

6 views (last 30 days)
Omar
Omar on 28 Nov 2019
Commented: Omar on 28 Nov 2019
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

Sign in to comment.

Accepted Answer

Jan
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);

  1 Comment

Omar
Omar on 28 Nov 2019
Thanks a lot for your response. This is really helpful. I got solution to the problem.

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Tags