E field plot

Discussion in 'Programmer's Corner' started by ihaveaquestion, Apr 30, 2011.

1. ihaveaquestion Thread Starter Active Member

May 1, 2009
314
0
Hi all,

I'm trying to do a very simple plot of E field as a function of theta with 0 <= theta >= 90 for the following E field:

E = exp(-1j*k*(r-(a/2)*sin(theta)))+exp(-1j*k*(r+(a/2)*sin(theta)));

I've attached a picture of the E function in handwriting which may be easier to see... (I hope my express above matches)

Here is my code (assumptions made are that E$_{}o$: is 1 and a/r =0, i.e., r >> a... also k = $\beta$)

r = 1000;
lam = 1;
a = lam/4;
k = 2*pi/lam;
theta = 0:5:90;
x = exp(-1j*k*(r-(a/2)*sin(theta)))+exp(-1j*k*(r+(a/2)*sin(theta)));
plot(theta,x);
xlabel('theta');
ylabel('E');

Is this correct?

Thanks a lot everyone

• HW4.jpg
File size:
16.5 KB
Views:
23
Last edited: Apr 30, 2011
2. ihaveaquestion Thread Starter Active Member

May 1, 2009
314
0
anyone willing to help?

Jul 7, 2009
1,585
141
You don't say whether you want the real or imaginary part (I assume you mean j is the unit imaginary). Unless I screwed up on my algebra, this expression can be simplified to a complex exponential times a cosine (see attached).

I've also attached a plot of this function I made using python, numpy, and matplotlib.

Code ( (Unknown Language)):
1.
2. from pylab import *
3. theta, A = arange(0, pi/2, 0.1), 1
4. plot(theta, 2*cos(A*sin(theta)))
5. xlabel('theta')
6. text(0.6, 2, r"$2cos(A sin \theta)$", size=18)
7. savefig("cos_term.png", dpi=75)
8.

File size:
57.5 KB
Views:
19
File size:
15.7 KB
Views:
14
4. ihaveaquestion Thread Starter Active Member

May 1, 2009
314
0
Yes, I think you're right about just taking the real part.... is there a command in MATLAB to just take the real part of the function to avoid any algebra errors?

Would it also be possible for you to please post the magnitude of E as a function of theta with theta sweeping from 0 to 90? I'd like to see if our graphs match... thanks

Jul 7, 2009
1,585
141
I don't have or use Matlab, so I can't help you. It's easy to do in python though (and the nice thing about the python tools is that they are open source and free).

I did plot the function of theta -- see the second image in my post above. That curve is just multiplied by a complex exponential in r, which you can take as a constant, as it won't change as theta varies.

But since it's trivial to do in python, here's the code to plot the E function you gave (I just set the constants to convenient values):

Code ( (Unknown Language)):
1.
2. from pylab import *
3. theta, A = arange(0, pi/2, 0.1), 1
4. a, r, k = 2, 1, 1
5. E = exp(-1j*k*(r - a/2*sin(theta))) + exp(-1j*k*(r + a/2*sin(theta)))
6. plot(theta, E.real)
7. xlabel('theta')
8. ylabel('Real part of E')
9.
The plot is attached. BTW, the folks who wrote the matplotlib library I use for plotting modeled it after Matlab, so it's possible the Matlab code is quite similar to the python code. One nice feature of matplotlib is that you can put equations and text on the screen anywhere and in the titles and axis labels. Makes it easier to produce useful plots.

File size:
15.6 KB
Views:
20