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.
Try to use the ndgrid
function instead of meshgrid
. The meshgrid
function uses column-by-column method for matrix storage like to FORTRAN (you can check this in MATLAB: a = [1 4 7; 2 5 8; 3 6 9]; a(:)
). Whereas the ndgrid
function produces tradition row-by-row matrix storage. Or rearange variables in the surf
function. For example, use:
surf(z1,x1,Eureal,'EdgeColor','None'); % maybe you need to use Eureal.'
instead of
surf(x1,z1,Eureal,'EdgeColor','None');
In addition, you can use Ei(x1,z1)
instead of Ei(z1,x1)
in your second method if x1
and z1
are integers.
Comment: you can use comlex number. Thus, use:
Ei = exp(1i*(kxi.*x1 + kzi.*z1));
instead of
Ei = cos(kxi.*x1 + kzi.*z1)+ sin(kxi.*x1 + kzi.*z1).*1i;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.