Matlab summation of a complex function

1. Mar 1, 2015

Waxterz

Hi,

I need to plot the last function of this:

But I don't know how to generate the sum. I know the for loop is totally wrong, but I can't go any further. This is what I have:

Can someone fix the summation loop part for me?

2. Mar 1, 2015

Staff: Mentor

You need to set S to zero before the loop, then
Code (Text):

S = S + exp(1i*g*(K-i)*d*(sind(v)-sind(v0)));

3. Mar 1, 2015

Waxterz

Hi DrClaude,

I posted it on the forum, just in case I didn't found the answer myself. I knew it had something to do with setting something on zero. :)

So I tried something, I tried this:

K = 8;
L = 950*10^-9;
g = 2*pi/L;
v0 = 30;
v = -90:10:90;
d = 950*10^-9;
Ssum = 0;
for i = 1:8;
Ssum = S + Ssum;
S = exp(1i*g*(K-i)*d*(sind(v)-sind(v0)));
end
A = 20*log(abs(Ssum)/8);
A

Is this equivalent to yours? Thanks for your help and quick reply, btw.

4. Mar 1, 2015

Waxterz

Yea, i'm going to do your version. Got the inner matrix dimensions degree error. Anyway thx!

5. Mar 1, 2015

Staff: Mentor

Your use of Ssum is better if you need $S_a$ afterwards. But I see a few things that could be better, and one error.

First,
Code (Text):

for i = 1:8;

would be better as
Code (Text):

for i = 1:K

since K is already defined. Second, since Ssum will be an array (as is S), you should initialize it as an array of zeros:
Code (Text):

Ssum = zeros(19,1);

Finally,
Code (Text):

Ssum = S + Ssum;
S = exp(1i*g*(K-i)*d*(sind(v)-sind(v0)));

will not work. What happens at the first and last iterations of the loop?