MATLAB: Does the plot of Intensity vs angle produce a horizontal line

electric fieldfunctionintensityplotting

Normally, Intensity should definitely fluctuate, but I keep getting a flat line plot every time I try. I have defined this Electric field strength function in another script:
function E = Efield(theta,N) % defining Electric field equation
E = zeros(size(theta));
a = 0.00008; % defining some slit width as 8 mm
k = 2*pi/650*10^(-9); % defining k as 2pi/450 nm
for n=1:(N/2)
A = a*k/N;
E = E + cos((n-0.5)*(A*sin(theta)));
end
And the actually given formula for E is given by E(theta,N) = (2/N)*Sum(cos((n-0.5)*(sin(theta)))) from n=1:(N/2). However, since the initial (2/N) factor isn't summed, I left it out and include it in the main script later. What I really have to plot is the relative intensity vs angle, which is given by "I(theta,N) = [E(theta,N)]^2:
N1 = 2;
N2 = 4;
N3 = 6;
N4 = 12;
N5 = 82;
% Defining range of function
theta = linspace(-pi/4,pi/4,100);
% Defining individual intensity functions for given "N" values
I1 = ((2/N1)*Efield(theta,N1)).^2;
I2 = ((2/N2)*Efield(theta,N2)).^2;
I3 = ((2/N3)*Efield(theta,N3)).^2;
I4 = ((2/N4)*Efield(theta,N4)).^2;
I5 = ((2/N5)*Efield(theta,N5)).^2;
figure()
plot(theta,I1)
xlabel('Angle (radians)'), ylabel('Intensity')
title('Relative Intensity versus Angle')
However, when I plot this, I get a flatline graph at around I = pi/4 and I'm not sure where in the function is wrong.

Best Answer

You forgot to show the lines of code where you do the actual plotting!
And you didn't add an index to E in your function. Perhaps try this:
E(n) = E(n) + cos((n-0.5) .* (sin(theta(n))));