function [Ex,Ey,Ez,normE]=GetBWExEyEz(type,Nodes,Elements,EdgesOfElements,xx,gamma)
%有求解的x计算电场Ex,Ey,Ez及normE
NbrNodes=length(Nodes);
NbrElements=length(Elements);
Ex=complex(zeros(NbrNodes,1));
Ey=complex(zeros(NbrNodes,1));
Ez=complex(zeros(NbrNodes,1));
NodesNum=zeros(NbrNodes,1);

if type==1
    NbrReference=4;
    NbrBF=6;
    u=[1 0 0 0]';
    v=[0 1 0 0]';
    w=[0 0 1 0]';
elseif type==2
    NbrReference=6;
    NbrBF=9;
    u=[0 1 0 0 1 0]';
    v=[0 0 1 0 0 1]';
    w=[-1 -1 -1 1 1 1]';
end

for n=1:NbrElements
    %网格坐标
    if type==1
        x=zeros(4,1);y=zeros(4,1);z=zeros(4,1);
        x(1)=Nodes(Elements(n,1),1);y(1)=Nodes(Elements(n,1),2);z(1)=Nodes(Elements(n,1),3);
        x(2)=Nodes(Elements(n,2),1);y(2)=Nodes(Elements(n,2),2);z(2)=Nodes(Elements(n,2),3);
        x(3)=Nodes(Elements(n,3),1);y(3)=Nodes(Elements(n,3),2);z(3)=Nodes(Elements(n,3),3);
        x(4)=Nodes(Elements(n,4),1);y(4)=Nodes(Elements(n,4),2);z(4)=Nodes(Elements(n,4),3);
        %边长度
        l=ones(6,1);
        l(1)=sqrt((x(1)-x(2))*(x(1)-x(2))+(y(1)-y(2))*(y(1)-y(2))+(z(1)-z(2))*(z(1)-z(2)));
        l(2)=sqrt((x(1)-x(3))*(x(1)-x(3))+(y(1)-y(3))*(y(1)-y(3))+(z(1)-z(3))*(z(1)-z(3)));
        l(3)=sqrt((x(1)-x(4))*(x(1)-x(4))+(y(1)-y(4))*(y(1)-y(4))+(z(1)-z(4))*(z(1)-z(4)));
        l(4)=sqrt((x(2)-x(3))*(x(2)-x(3))+(y(2)-y(3))*(y(2)-y(3))+(z(2)-z(3))*(z(2)-z(3)));
        l(5)=sqrt((x(2)-x(4))*(x(2)-x(4))+(y(2)-y(4))*(y(2)-y(4))+(z(2)-z(4))*(z(2)-z(4)));
        l(6)=sqrt((x(3)-x(4))*(x(3)-x(4))+(y(3)-y(4))*(y(3)-y(4))+(z(3)-z(4))*(z(3)-z(4)));
        %雅可比矩阵
        Jac=zeros(3,3);
        Jac(1,1)=x(1)-x(4);Jac(1,2)=y(1)-y(4);Jac(1,3)=z(1)-z(4);
        Jac(2,1)=x(2)-x(4);Jac(2,2)=y(2)-y(4);Jac(2,3)=z(2)-z(4);
        Jac(3,1)=x(3)-x(4);Jac(3,2)=y(3)-y(4);Jac(3,3)=z(3)-z(4);
        InvJac=inv(Jac);
    end
    %基函数计算
    Ne=zeros(3,NbrBF,NbrReference);%3个分量 * n个基函数 * m个顶点数
    for i=1:NbrBF %基函数
        for k=1:NbrReference %顶点
            temp=BF_Edge(type,i,u(k),v(k),w(k));
            Ne(:,i,k)=InvJac*temp*l(i);
        end
    end
    
    %计算顶点值
    for k=1: NbrReference %顶点
       for i=1:NbrBF %基函数
           MappingIndex=EdgesOfElements((n-1)*NbrBF+i);
           Ex(Elements(n,k))=Ex(Elements(n,k))+Ne(1,i,k)*xx(MappingIndex)*exp(-gamma*z(k));
           Ey(Elements(n,k))=Ey(Elements(n,k))+Ne(2,i,k)*xx(MappingIndex)*exp(-gamma*z(k));
           Ez(Elements(n,k))=Ez(Elements(n,k))+Ne(3,i,k)*xx(MappingIndex)*exp(-gamma*z(k));
       end
       NodesNum(Elements(n,k))=NodesNum(Elements(n,k))+1;
    end
end
Ex=Ex./NodesNum;
Ey=Ey./NodesNum;
Ez=Ez./NodesNum;
normE=sqrt(abs(Ex.*Ex)+abs(Ey.*Ey)+abs(Ez.*Ez));