为 FDM 调整 MATLAB 网格/矩阵/数组的大小/重读

问题描述

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

Click for pictorial representation of a Parallel Plate Grid

这里,整个数组从零开始,除了最后一行用常数 8000 填充。类似地,第 Zeroth(或 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-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+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')

The code output can be seen by clicking this hyperlink.

从这个输出中可以看出,网格被调整为在“Y”轴上有 50 个单位,在“X”轴上有 5 个单位。但是,我想计算恒定长度(例如 5 米 x 1 米区域)上的电位,但想将其划分为不同的网格。基本上,最终目标是拥有一个脚本,可以采用真实世界的物理长度尺寸,将它们分配给“X”或“Y”轴,然后更改计算分辨率。我曾尝试在 Matlab 中使用 linspace 来生成一个向量,但它真的很快就变得很混乱,因为我不知道如何从相同的 2D 矩阵中创建一个 2D 矩阵。我还试图改变我如何处理这些位置。我的意思是,确定矩阵是 50x5,但我可以将其标记为 5x5,并将其纳入我程序的其他区域(加权平均部分)。然而,我的实现根本没有奏效,甚至让我的程序更加混乱。

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)