如何提取偶极子天线空间定位增益值的动态二维网格?
偶极子天线切面场强计算异常问题
需求说明
已完成偶极子天线建模,目标是创建从参考系原点延伸至(maxWidth, maxHeight)的网格,网格由第三坐标固定定位。用户可通过x0、y0、z0指定天线相位中心位置,可选择平行于X/Y(z恒定)、Y/Z(x恒定)或X/Z(y恒定)的平面,网格步长设为mesh_size。
原始可正常运行代码
x0 = 25.0; y0 = 25.0; z0 = 25.0; frequency = 150000.0; dipoleAntenna = dipole('Length', 3.0, 'Tilt', 90, 'TiltAxis', [0, 0, 1]); % 绕z轴垂直倾斜及极化 pattern(dipoleAntenna, frequency, 'CoordinateSystem', 'polar'); maxGridWidth = 50.0; % 最大宽度(单位:米) maxGridHeight = 50.0; % 最大高度(单位:米) mesh_size = 0.1; num_divisions_width = floor(maxGridWidth / mesh_size); num_divisions_height = floor(maxGridHeight / mesh_size); planToDisplay = "X/Z"; thirdcoord = 25.0; x_coords = 0:mesh_size:maxGridWidth; y_coords = 0:mesh_size:maxGridHeight; [X, Y] = meshgrid(x_coords, y_coords); valueMatrix = zeros(size(X)); for i = 1:length(x_coords) for j = 1:length(y_coords) if strcmp(planToDisplay, 'X/Y') z = thirdcoord - z0; x = x_coords(i) - x0; y = y_coords(j) - y0; elseif strcmp(planToDisplay, 'X/Z') y = thirdcoord - y0; x = x_coords(i) - x0; z = y_coords(j) - z0; elseif strcmp(planToDisplay, 'Y/Z') x = thirdcoord - x0; z = y_coords(j) - z0; y = x_coords(i) - y0; end r = sqrt(x^2 + y^2 + z^2); if x == 0 && y == 0 && z == 0 Eeff = 25001; valueMatrix(i, j) = Eeff; else [direct, ~, ~] = pattern(dipoleAntenna, frequency, 'Azimuth', atan2(y, x), 'Elevation', acos(z / sqrt(x^2 + y^2 + z^2))); Eeff = sqrt(30*10000.0*2.0*direct)/r; valueMatrix(i, j) = Eeff; end end end
修改后出现异常的代码
尝试通过旋转天线切换切面,但未同步修改变量映射逻辑,得到异常结果:
planToDisplay = 'Y/Z'; if strcmp(planToDisplay, 'X/Y') dipoleAntenna = dipole('Length', 3.0, 'Tilt', 0, 'TiltAxis', [0, 0, 1]); % 绕z轴垂直倾斜及极化 disp(planToDisplay); elseif strcmp(planToDisplay, 'X/Z') dipoleAntenna = dipole('Length', 3.0, 'Tilt', 90, 'TiltAxis', [0, 1, 0]); % 绕y轴水平倾斜及极化 disp(planToDisplay); elseif strcmp(planToDisplay, 'Y/Z') dipoleAntenna = dipole('Length', 3.0, 'Tilt', 90, 'TiltAxis', [1, 0, 0]); % 绕x轴垂直倾斜及极化 disp(planToDisplay); end for i = 1:length(x_coords) for j = 1:length(y_coords) z = thirdcoord - z0; x = x_coords(i) - x0; y = y_coords(j) - y0; [ ... ] % 后续逻辑与原始代码一致,但未按当前切面类型更新坐标映射规则
问题分析与修复方案
核心问题
修改天线朝向时,未同步更新坐标映射逻辑。原始代码通过planToDisplay的值动态分配每个网格点的x/y/z相对坐标,但修改后的代码直接固定了坐标计算方式,导致天线朝向与所选切面不匹配,场强计算逻辑混乱,结果异常。
修复步骤
- 保留动态坐标映射逻辑:在初始化对应朝向的天线后,必须保留根据
planToDisplay分支判断计算x/y/z的代码,不能写死坐标计算规则。 - 确保天线朝向与切面匹配:天线的轴向需垂直于所选切面:
- X/Y平面(z恒定):天线沿z轴方向
- X/Z平面(y恒定):天线沿y轴方向
- Y/Z平面(x恒定):天线沿x轴方向
- 变量一致性检查:确认
x_coords、y_coords始终对应所选切面的两个可变坐标轴。
修复后的代码示例
planToDisplay = 'Y/Z'; % 根据切面类型初始化对应朝向的天线 if strcmp(planToDisplay, 'X/Y') dipoleAntenna = dipole('Length', 3.0, 'Tilt', 0, 'TiltAxis', [0, 0, 1]); disp(planToDisplay); elseif strcmp(planToDisplay, 'X/Z') dipoleAntenna = dipole('Length', 3.0, 'Tilt', 90, 'TiltAxis', [0, 1, 0]); disp(planToDisplay); elseif strcmp(planToDisplay, 'Y/Z') dipoleAntenna = dipole('Length', 3.0, 'Tilt', 90, 'TiltAxis', [1, 0, 0]); disp(planToDisplay); end x_coords = 0:mesh_size:maxGridWidth; y_coords = 0:mesh_size:maxGridHeight; [X, Y] = meshgrid(x_coords, y_coords); valueMatrix = zeros(size(X)); % 保留动态坐标映射逻辑,适配当前切面 for i = 1:length(x_coords) for j = 1:length(y_coords) if strcmp(planToDisplay, 'X/Y') z = thirdcoord - z0; x = x_coords(i) - x0; y = y_coords(j) - y0; elseif strcmp(planToDisplay, 'X/Z') y = thirdcoord - y0; x = x_coords(i) - x0; z = y_coords(j) - z0; elseif strcmp(planToDisplay, 'Y/Z') x = thirdcoord - x0; z = y_coords(j) - z0; y = x_coords(i) - y0; end r = sqrt(x^2 + y^2 + z^2); if x == 0 && y == 0 && z == 0 Eeff = 25001; valueMatrix(i, j) = Eeff; else [direct, ~, ~] = pattern(dipoleAntenna, frequency, 'Azimuth', atan2(y, x), 'Elevation', acos(z / sqrt(x^2 + y^2 + z^2))); Eeff = sqrt(30*10000.0*2.0*direct)/r; valueMatrix(i, j) = Eeff; end end end
额外优化建议
- 将坐标映射逻辑封装成独立函数,减少重复代码,降低出错概率。
- 计算方位角时,添加
x=0的特殊处理,避免atan2(y,x)返回NaN。 - 所有坐标计算均基于相对天线相位中心的位置,避免绝对坐标混淆。
内容的提问来源于stack exchange,提问作者Mr. Dixit
相关产品推荐
相关产品推荐

