用平分线划分网格,MATLAB

用平分线划分网格,MATLAB,matlab,matlab-figure,Matlab,Matlab Figure,我有一个分段函数,每种情况下域都会改变。功能如下: 为了 x、 y大于分隔器v=fx,y A1 x、 y小于分压器v=gx,y A2 分隔器的位置随图1和图2所示矩形的倾斜角度而变化。分隔线将始终是矩形的平分线。例如,分割线与水平线形成角度alpha+90。 如果矩形的角度为0,则很容易实现上述函数,因为我可以从中创建meshgrid 对于A1,x=B至C&y=A至D 对于A2,x=A到B&y=A到D 但是,当矩形的角度不同时,我不知道如何使用上面的算法A1和A2创建网格来计算函数v 我想用一些

我有一个分段函数,每种情况下域都会改变。功能如下:

为了

x、 y大于分隔器v=fx,y A1

x、 y小于分压器v=gx,y A2

分隔器的位置随图1和图2所示矩形的倾斜角度而变化。分隔线将始终是矩形的平分线。例如,分割线与水平线形成角度alpha+90。 如果矩形的角度为0,则很容易实现上述函数,因为我可以从中创建meshgrid

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

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

但是,当矩形的角度不同时,我不知道如何使用上面的算法A1和A2创建网格来计算函数v

我想用一些不等式和直线方程,因为我有矩形中心的坐标和倾斜角。但是,我似乎想不出一种方法来处理所有的角度,例如,第一幅图中的π/2的斜率,产生无穷大。即使我创造了某种不平等,我也不能创造网格。 请帮我解决这个问题。我在这件事上浪费了很多时间。好像我够不着

%% 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)

最后的图形应该是这样的。

我不确定哪个角度定义了分界线,所以我假设它是角度1。看起来逻辑索引是解决这个问题的方法。我们不需要创建两个单独的网格,只需创建整个网格,然后将其划分为两个集合,并分别对每个集合进行操作

%% 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)));
其中,黄色区域使用Eqn5,蓝色区域使用Eqn4。这与您发布的示例一致,但我不知道其他情况下生成的等高线图应该是什么样子


希望这有帮助。

分隔符总是一条线吗?如果你能把它写成vx,y=a*x+b*y+c=0的形式,那么对于任意点x,y,vx,y的值在直线的一侧为正值,在另一侧为负值。如果你能发布一些自包含的代码来演示在0度或90度的情况下工作的方法,那么我可以进一步提供帮助。我不确定您希望从您给出的描述中得到什么输出。添加了代码。非常感谢你的帮助。当Angle2=0时,代码可以工作,但在其他情况下它不工作。在我看来,Angle2定义矩形,而Angle1应该定义分隔符。但你的评论恰恰相反。哪一个?谢谢你。但是这个数字看起来一点也不像。我已经编辑了图片。我发布的代码旨在替换名为%%Prepare for making the plots的代码块。如果这不清楚,我会道歉的。我发布的图只是演示分别使用Eqn5和Eqn4计算的网格部分。与您的0角度匹配的结果图像在45度时看起来不正确,这表明Eqn4或Eqn5可能存在问题,但我不知道它们代表什么。该图表明索引不是问题所在。我还假设了Angle1/2的含义,因为你从未回复过原始帖子中的评论。Angle是正确的。是的,我的代码似乎还有其他问题。您提供的代码片段似乎运行良好。感谢
>> contourf(x,y,idx1);
>> line(line_x,line_y,'color','r','linestyle',':','linewidth',5);