I am a matlab beginner. I successfully used meshgrid to plot a color-map I needed for my project, with following codes:
Creating meshgrid first:
[x1,z1] = meshgrid(0:1:600,300:1:600);
[x2,z2] = meshgrid(0:1:600,0:1:300);
Then just some constants defined:
n1 = 1.0;
n2 = 1.5;
lambda = 100; % wavelength is 600 nm
k0 = (2*pi)/lambda; % freespace wavenumber
ti = 30*pi/180; % angle of incident, t1 in Brewster function
kxi = n1*k0*sin(ti); % x component of the incident wavevector
kzi = -n1*k0*cos(ti); % z component of the incident wavevector
kxr = kxi; % x component of the reflected wavevector
kzr = -kzi; % z component of the reflected wavevector
Rs = (kzi-kzt)/(kzi+kzt);
Now Eureal is the function I want to plot, with x1 and z1 as its coordinate:
Ei = cos(kxi.*x1 + kzi.*z1)+ sin(kxi.*x1 + kzi.*z1).*1i; %incident E_field use upper x and z
Er = Rs.*(cos(kxr.*x1 + kzr.*z1)+ sin(kxr.*x1 + kzr.*z1).*1i); %reflected E_field
Eu = Er + Ei; %Uper E_field
Eureal = real(Eu);
figure;
surf(x1,z1,Eureal,'EdgeColor','None');
view(2);
xlabel('x','fontsize',20);
ylabel('z','fontsize',20);
colormap jet;
My problem is, I needed a different way of coding, because later on it will involves matrix calculation, but this way of coding doesn't produce the right graph like it did in the first method so the different way I used is as follow:
[x,z] = meshgrid(0:5:600,0:5:600);
Then still the same constants as above, so I will not type down
Now here is what's different:
Ei = zeros(size(x));
Er = zeros(size(x));
for z1 = 80:mm(1)
for x1 = 1:mm(1)
Ei(z1,x1) = cos(kxi.*x(x1) + kzi.*z(z1))+ sin(kxi.*x(x1) + kzi.*z(z1)).*1i;%exp(-1i*kzia*z(z1))*exp(-1i*kxia*x(x1)); % ETazi*exp(-1i*kxia*x(x1))
Er(z1,x1) = Rs.*(cos(kxr.*x(x1) + kzr.*z(z1))+ sin(kxr.*x(x1) + kzr.*z(z1)).*1i); %exp(-1i*kzra*z(z1))*exp(-1i*kxra*x(x1)); % ETazout*exp(-1i*kxia*x(x1))
Eu = Er + Ei; %Uper E_field
Eureal = real(Eu);
Eplot = zeros(size(x)) + Eureal;
end
end
figure
surf(x,z,Eplot,'EdgeColor','None');
view(2);
xlabel('x','fontsize',20);
ylabel('z','fontsize',20);
colormap jet;
I don't know what is the reason that the second method is problematic, and I need the for loops and x(x1), z(z1) indexing for coding later on when it involves matrix, because then the first method won't work.
kztandmm?