Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab summation of a complex function

  1. Mar 1, 2015 #1

    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?

    Thanks in advance
  2. jcsd
  3. Mar 1, 2015 #2


    User Avatar

    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)));
  4. Mar 1, 2015 #3
    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)));
    A = 20*log(abs(Ssum)/8);

    Is this equivalent to yours? Thanks for your help and quick reply, btw.
  5. Mar 1, 2015 #4
    Yea, i'm going to do your version. Got the inner matrix dimensions degree error. Anyway thx!
  6. Mar 1, 2015 #5


    User Avatar

    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.

    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);
    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?
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook