简体   繁体   English

Matlab Meshgrid彩色地图绘制

[英]Matlab Meshgrid color map plotting

I am a matlab beginner. 我是matlab初学者。 I successfully used meshgrid to plot a color-map I needed for my project, with following codes: 我成功使用meshgrid绘制了我的项目所需的颜色图,并带有以下代码:

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: 现在Eureal是我要绘制的函数,以x1和z1作为其坐标:

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. 我不知道第二种方法有问题的原因是什么,我需要for循环和x(x1),z(z1)索引稍后在涉及矩阵时进行编码,因为那样第一种方法就不会工作。

Try to use the ndgrid function instead of meshgrid . 尝试使用ndgrid函数而不是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(:) ). meshgrid函数像FORTRAN一样使用逐列方法存储矩阵(您可以在MATLAB中进行检查: a = [1 4 7; 2 5 8; 3 6 9]; a(:) )。 Whereas the ndgrid function produces tradition row-by-row matrix storage. ndgrid函数产生传统的逐行矩阵存储。 Or rearange variables in the surf function. 或在surf函数中重新排列变量。 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. 此外,如果x1z1是整数,则可以在第二种方法中使用Ei(x1,z1)而不是Ei(z1,x1)

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM