首页 > 解决方案 > 为 FDM 调整/重新调整 MATLAB 网格/矩阵/数组的大小

问题描述

所以,我是一名 EE 学生,试图创建一个 MATLAB 脚本,通过有限差分法找到自定义数组不同部分的电压。基本上,我必须遍历每个数组元素,平均它周围的四个元素并将平均值放在原始位置[所以基本上对于任意点 Vi,j = 1/4(Vi+1,j + Vi-1 ,j + Vi,j+1 + Vi, j-1)]。但是,当到每个网格点的间距发生变化时,问题会略有变化。该问题成为加权平均数之一。让我们看一下我在 Excel 中为自己制作的一个简单的“图表”。

单击以查看平行板网格的图示

这里,整个数组从零填充开始,除了最后一行用常量 8000 填充。类似地,第零(或 MATLAB 中的第一行)用常量 0 填充。最后一行和第零行不变。代码从取平均值开始,多次迭代将收敛到正确的解决方案。可以看出或推断,沿“X”轴的值不会有太大变化(也是如此,因为它是等势面)。更改将在“Y”轴上。因此,我基本上可以通过将显示的八 (8) 列更改为仅一 (1) 列来计算网格,并且应该大幅增加行数,以便我可以进行“更精细”/更详细/更高分辨率的分析。这就是我的问题所在。我无法弄清楚如何“调整网格大小”。

% Description: Graphing the potential between
% two parallel plates of set voltages (boundary conditions) using FDM.
% I have considered the area under observation as a grid/array/matrix
% as shown in the accompanying photos. Each element of the matrix gives the
% magnitude of the potential at that very point. 

% The matrix starts off zero-filled, and the boundary conditions are set 
% (in this case, two parallel edges of the matrix are set to 8 kV and 0 kV
% to represent equipotential surfaces). Since the potential is only
% changing in one direction/axes, the magnitude of each grid element can be
% calculated relatively easily.

% The output graph is a 3D graph. The X and Y axes show the map of the
% region. The Z axis shows the strength of electric potential at every
% given unit grid on the X-Y plane. 

% Initial Setup
clc;
clear all;
close all;

% xAxisResolution and yAxisResolution showcase how fine I want to calculate
% the field for. For example, in the attached photo, potential changes
% substantially more drastically over the y axis, but does not change with
% change of x axis. Thus, my yAxisResolution is great by a factor of 10. 
xAxisResolution = 5*10^0;
yAxisResolution = 5*10^1;

% Creating the grid I want to calculate my potential strength over and
% zero-filling.
fieldArray = zeros (yAxisResolution, xAxisResolution);

% Setting up the Initial Conditions. Since one of the parallel plates is
% 0kV, and the matrix is already zero-filled, I do not need to do anything.
% For the second plate, the voltage is set to 8 kV. 
for column = 1:xAxisResolution
    fieldArray(1, column) = 8000;
end

% FDM Calculation
% iterator - Number of times that the whole grid/array is
% refreshed/updated. For best results, use 1:10^5
% To improve accuracy, some boundary condition if statements are added.
% They are described below. 

for iterator = 1:10^4
    for row = 1:(yAxisResolution) % Traverses each row
        for column = 1:(xAxisResolution) % Traverses each column in every row

            % Case #1: Check to see if the current row being traversed is 
            % the user-provided initial/boundary condition. In my case, I
            % am testing two parallel plates at two edges. (Colored RED in 
            % photo) They are 0 kV and 8kV respectively and cannot be 
            % changed. Thus, when the loop encounters these edges, nothing 
            % is changed and break condition is encountered. 
            if (row == 1) || (row == yAxisResolution) 
                break

            % Case #2: Checks to see if the current column is on the edge. 
            % If so, there will only be 3 adjacent elements, and the 
            % stencil is changed. This is accounted for with a specific
            % formula to account for stencil change (average of only 3
            % adjacent elements).
            % (Colored YELLOW in photo)
            elseif (column == 1)
                fieldArray(row, column) = (fieldArray(row+1, column) + fieldArray(row-1, column) + fieldArray(row, column+1))/3;
            elseif (column == xAxisResolution)
                fieldArray(row, column) = (fieldArray(row+1, column) + fieldArray(row-1, column) + fieldArray(row, column-1))/3;

            % Case #3: Checks to see if the current element is at any other
            % position of the matrix/grid that can be changed/overwritten
            % that has 4 adjacent elements. These elements are accounted
            % for by their specific averaging formula (4 elements). 
            % (Colored GREEN in photo)
            else
                fieldArray(row, column) = (fieldArray(row+1, column) + fieldArray(row-1, column) + fieldArray(row, column+1) + fieldArray(row, column-1))/4;
            end
        end
    end
end

% Produces graph of potential at every point
fieldArray;
surf(fieldArray)
xlabel('X'), ylabel('Y'), zlabel('Potential Strength')

单击此超链接可以看到代码输出。

从这个输出中可以看出,网格的大小被调整为在“Y”轴上有 50 个单位,在“X”轴上有 5 个单位。但是,我想计算恒定长度(例如 5 米 x 1 米区域)上的电势,但想将其划分为不同的网格。基本上,最终目标是拥有一个脚本,该脚本可以采用实际长度的物理尺寸,将它们分配给“X”或“Y”轴,然后更改计算的分辨率。我曾尝试在 Matlab 中使用 linspace 来生成向量,但由于我不知道如何从中制作 2D 矩阵,所以它很快就变得非常混乱。我还尝试改变我处理这些位置的方式。我的意思是,确保矩阵是 50x5,但我可以将其标记为 5x5,并将其容纳在我程序的其他区域(加权平均部分)中。然而,

如果你们有我可以尝试的线索或想法,请告诉我。也有任何问题,请告诉我。

标签: arraysmatlabmatrixvectoroctave

解决方案


推荐阅读