central difference A matrix

我是研究僧i 提交于 2020-06-01 07:38:25

问题


Hi im trying to code the following coefiicient matrix A in a for loop in Matlab. N is the total number of elements. And i and j are obtained from k. Could someone help me please code the A matrix correctly for any N value. The A and b are shown

for k = 1:N
    A(k,k) = 4; % the diagonal element, corresponding to Tij, is always 4
    %     In the following, we look at the four neighbours of (i,j) that are
    %     involved in the 5-point formula
    i = mod(k-1,nx)+1;
    j = (k-i)/nx+1; %get (i,j) from k
    %boundary condtion
    if i == 1
        b(k,1) = Tl;

    elseif i == nx
        b(k,1) = Tr;

    elseif j == 1
        b(k,1) = Tb;
    elseif j == ny
        b(k,1) = Tt;
    end
    %A matrix construction
    if i>1 && j>1
        if i == j
            A(j-1,i) = -1;
            A(j,i+1) =- 1;
            A(j,i-1) = -1;
            A(j+1,i) = -1;

        end
    end
end

来源:https://stackoverflow.com/questions/61584719/central-difference-a-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!