用网格划分网格划分网格网格

时间:2018-01-08 03:28:58

标签: matlab matlab-figure

我有一个分段函数,其中每个案例的域都会更改。功能如下:

  

对于

     

(x,y)大于分频器v = f(x,y)(A1)

     

(x,y)小于Divider v = g(x,y)(A2)

分隔符的位置随图1和图2中给出的矩形的倾斜角度而变化。Figure 1 & 2分隔符将始终是矩形的平分线。例如,分隔线与水平方向形成一个角度(alpha + 90)。 如果矩形的角度为0 ,则可以轻松实现上述功能,因为我可以从

创建meshgrid
  

x = B到C&对于A1,y = A到D

     

x = A到B&对于A2,y = A到D

然而,当矩形的角度不同时,我无法弄清楚如何使用上面的算法A1和A2来创建网格来计算函数v。

我正在考虑使用一些不等式并使用线的方程(因为我有矩形中心和倾斜角度的坐标)。但是,我似乎无法想到一种方法来为所有角度做这件事(例如,pi / 2的斜率与第一个数字相同,产生无穷大)。即使我确实创造了某种不平等,我也无法创建网格。 1请帮我解决这个问题。我浪费了很多时间在这上面。这似乎超出了我的范围

%% Constants
Angle1=0;
Angle1=Angle1.*pi./180;

rect_center=0; % in m
rect_length=5; % in m
rect_width=1; % in m
rect_strength=1.8401e-06;

Angle2=0;
Angle2 =Angle2.*pi./180;


  %% This code calculates the outer coordinates of the rectangle by using the central point

% the following code calculates the vertices   
vertexA=rect_center+(-rect_width./2.*exp(1i.*1.5708)-rect_length./2).*exp(1i.*Angle2);
vertexA=[vertexA,vertexA+2.*(rect_width./2.*exp(1i.*1.5708)).*exp(1i.*Angle2)];
vertexB=rect_center+(-rect_width./2.*exp(1i.*1.5708)+rect_length./2).*exp(1i.*Angle2);
vertexB=[vertexB,vertexB+2.*(rect_width./2.*exp(1i.*1.5708)).*exp(1i.*Angle2)];

za1=vertexA(1:numel(vertexA)/2);
za2=vertexA(1+numel(vertexA)/2:numel(vertexA));
zb1=vertexB(1:numel(vertexB)/2);
zb2=vertexB(1+numel(vertexB)/2:numel(vertexB));
arg1=exp(-1i.*Angle2);



%% This Section makes the two equations necessary for making the graphs 


    syms var_z    

    % Equation 1
    Eqn1(var_z)=1.5844e-07.*exp(-1i.*Angle1).*var_z./9.8692e-13;



    % subparts of the Equation 2 
    A = 1.0133e+12.*(-1i.*rect_strength.*exp(-1i*Angle2)./(2*pi.*rect_length.*rect_width*0.2));
    ZA1 = var_z+za1-2*rect_center;
    ZA2 = var_z+za2-2*rect_center;
    ZB1 = var_z+zb1-2*rect_center;
    ZB2 = var_z+zb2-2*rect_center;


   ZAA2 = log(abs(ZA2)) + 1i*mod(angle(ZA2),2*pi);
   ZAA1 = log(abs(ZA1)) + 1i*mod(angle(ZA1),2*pi);
   ZBB1 = log(abs(ZB1)) + 1i*mod(angle(ZB1),2*pi);
   ZBB2 = log(abs(ZB2)) + 1i*mod(angle(ZB2),2*pi);

   %Equation 2 ; this is used for the left side of the center
   Eqn2= A*(ZA2*(log(ZA2)-1)-(ZA1*(log(ZA1)-1))+(ZB1*(log(ZB1)-1))-(ZB2*(log(ZB2)-1)));
   %Equation 3 ; this is used for the right side of the center
   Eqn3 = A.*(ZA2*(ZAA2-1)-(ZA1*(ZAA1-1))+(ZB1*(ZBB1-1))-(ZB2*(ZBB2-1)));


    %Equation 4 :Add Equation 2 and Equation 1; this is used for the left side of the center
    Eqn4 = matlabFunction(Eqn1+Eqn2,'vars',var_z);
    %Equation 5: Add Equation 3 and Equation 1; this is used for the right side of the center
    Eqn5 = matlabFunction(Eqn1+Eqn3,'vars',var_z);






    %% Prepare for making the plots

    minx=-10;     %min x coordinate 
    maxx=10;      %max x coordinate 
    nr_x=1000;    %nr x points
    miny=-10;     %min y coordinate 
    maxy=10;      %max y coordinate 
    nr_y=1000;    %nr y points

    %This vector starts from left corner (minx) to the middle of the plot surface,
    %The middle of the plot surface lies at the center of the rectange
    %created earlier
    xvec1=minx:(rect_center-minx)/(0.5*nr_x-1):rect_center;

    %This vector starts from middle to the right corner (maxx) of the plot surface,
    %The middle of the plot surface lies at the center of the rectange
    %created earlier
    xvec2=rect_center:(maxx-rect_center)/(0.5*nr_x-1):maxx;


    %the y vectors start from miny to maxy
    yvec1=miny:(maxy-miny)/(nr_y-1):maxy;
    yvec2=miny:(maxy-miny)/(nr_y-1):maxy;


    % create mesh from above vectors
    [x1,y1]=meshgrid(xvec1,yvec1);
    [x2,y2]=meshgrid(xvec2,yvec2);


     z1=x1+1i*y1;
     z2=x2+1i*y2;



    % Calculate the above function using equation 4 and equation 5 using the mesh created above  
    r1 = -real(Eqn5(z1));  
    r2 = -real(Eqn4(z2));

    %Combine the calculated functions
    Result = [r1 r2];

    %Combine the grids
    x = [x1 x2];
    y = [y1 y2];

 % plot contours  
 [c,h]=contourf(x,y,Result(:,:,1),50,'LineWidth',1);
 % plot the outerboundary of the rectangle
 line_x=real([vertexA;vertexB]);
 line_y=imag([vertexA;vertexB]);
 line(line_x,line_y,'color','r','linestyle',':','linewidth',5)

最终的图应该看起来像这样Final Expected Figure.

1 个答案:

答案 0 :(得分:1)

我不确定哪个角度定义了分界线,所以我假设它是Angle1。看起来像逻辑索引是这里的方式。我们只需创建整个网格网格,然后将其分成两组,然后独立操作,而不是创建两个单独的网格网格。

%% Prepare for making the plots

minx=-10;     %min x coordinate 
maxx=10;      %max x coordinate 
nr_x=1000;    %nr x points
miny=-10;     %min y coordinate 
maxy=10;      %max y coordinate 
nr_y=1000;    %nr y points

% create full mesh grid
xvec=linspace(minx,maxx,nr_x);
yvec=linspace(miny,maxy,nr_y);
[x,y]=meshgrid(xvec,yvec);

% Partition mesh based on divider line
% Assumes the line passes through (ox,oy) with normal vector defined by Angle1
ox = rect_center;
oy = rect_center;
a = cos(Angle1);
b = sin(Angle1);
c = -(a*ox + b*oy);
% use logical indexing to opperate on the appropriate parts of the mesh
idx1 = a*x + b*y + c < 0;
idx2 = ~idx1;

z = zeros(size(x));
z(idx1) = x(idx1) + 1i*y(idx1);
z(idx2) = x(idx2) + 1i*y(idx2);

% Calculate the above function using equation 4 and equation 5
%   using the mesh created above  
Result = zeros(size(z));
Result(idx1) = -real(Eqn5(z(idx1)));
Result(idx2) = -real(Eqn4(z(idx2)));

例如,对于Angle1 = 45Angle2 = 45,我们会得到以下索引

>> contourf(x,y,idx1);
>> line(line_x,line_y,'color','r','linestyle',':','linewidth',5);

enter image description here

黄色区域使用Eqn5,蓝色区域使用Eqn4。这与您发布的示例一致,但我不知道其他案例的结果等值线图应该是什么样的。

希望这有帮助。