Matlab summation of a complex function

Click For Summary

Discussion Overview

The discussion revolves around generating a summation of a complex function in MATLAB, specifically focusing on the correct implementation of a summation loop. Participants are addressing issues related to initializing variables and the structure of the loop for summing complex exponentials.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant expresses difficulty in generating the sum within a for loop and requests assistance with the MATLAB code.
  • Another participant suggests initializing the variable S to zero before the loop and provides a corrected summation line.
  • A participant attempts to implement the suggestion but modifies the code, leading to a question about the equivalence of their approach to the previous suggestion.
  • Further feedback includes recommendations to use the defined variable K for loop iteration and to initialize Ssum as an array of zeros, along with a caution about potential issues in the loop's first and last iterations.

Areas of Agreement / Disagreement

There is no consensus on the final implementation of the summation loop, as participants propose different approaches and highlight potential errors without resolving the overall correctness of any single method.

Contextual Notes

Participants note issues related to variable initialization and array dimensions, indicating that the current implementations may not function as intended without further adjustments.

Waxterz
Messages
4
Reaction score
0
Hi,

I need to plot the last function of this:

RB9vKTW.jpg

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:

FBlwCaW.jpg


K = 8;
L = 950*10^-9;
g = 2*pi/L;
v0 = 30;
v = -90:10:90;
d = 950*10^-9;
for i = 1:8;

S = exp(1i*g*(K-i)*d*(sind(v)-sind(v0)));
end
A = 20*log(abs(S)/8);
A

Can someone fix the summation loop part for me?

Thanks in advance
 
Physics news on Phys.org
You need to set S to zero before the loop, then
Code:
S = S + exp(1i*g*(K-i)*d*(sind(v)-sind(v0)));
 
DrClaude said:
You need to set S to zero before the loop, then
Code:
S = S + exp(1i*g*(K-i)*d*(sind(v)-sind(v0)));
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);
AIs this equivalent to yours? Thanks for your help and quick reply, btw.
 
Yea, I'm going to do your version. Got the inner matrix dimensions degree error. Anyway thx!
 
Waxterz said:
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);
AIs this equivalent to yours? Thanks for your help and quick reply, btw.
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:
for i = 1:8;
would be better as
Code:
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:
Ssum = zeros(19,1);
Finally,
Code:
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?
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K