Poisson Equation on a Square Domain, Dirichlet Boundary Conditions

Introduction to the problem

Consider the boundary-value problem
2ux2+2uy2=x+y\frac{{\partial}^{2}u}{{\partial}x^{2}}+\frac{{\partial}^{2}u}{{\partial}y^{2}}=x+y
Boundary condition:
u(0,y)=0u(1,y)=1u(x,0)=0u(x,1)=1 u(0,y)=0\\u(1,y)=1\\u(x,0)=0 \\u(x,1)=1

jj \rightarrow gird in x direction
kk \rightarrow gird in y direction
hh \rightarrow length if the grid
2ux2xj,yk=uj1,k2uj,k+uj+1,kh2\left.\frac{{\partial}^{2}u}{{\partial}x^{2}}\right|_{x_{j},y_{k}} = \frac{u_{j-1,k}-2u_{j,k}+u_{j+1,k}}{h^{2}}

2uy2xj,yk=uj,k12uj,k+uj,k+1h2\left.\frac{{\partial}^{2}u}{{\partial}y^{2}}\right|_{x_{j},y_{k}} = \frac{u_{j,k-1}-2u_{j,k}+u_{j,k+1}}{h^{2}}
uj1,kuj+1,kuj,l1uj,k+1+4uj,k=h2fj,k -u_{j-1,k}-u_{j+1,k}-u_{j,l-1}-u_{j,k+1}+4u_{j,k}=-h^{2}f_{j,k}
These equations can be arranged in the form
Au=b Au = b
Where, for NxN_{x} intervals in xx and NyN_{y}, AA is an (Nx1)(Ny1)(N_{x}-1)(N_{y}-1)-by-(Nx1)(Ny1)(N_{x}-1)(N_{y}-1) matrix of the form

A=[DIIDI...IDIID]A = \begin{bmatrix} D & -I & & & & & \\ -I & D & -I & & & & \\ & & . & & & & \\ & & & . & & & \\ & & & & . & & \\ & & & -I & D & -I \\ & & & & -I & D \\ \end{bmatrix}
where DD is in (Ny1)(N_{y}-1)-by-(Ny1)(N_{y}-1) matrix of the form
D=[41141...14114] D= \begin{bmatrix} 4 & -1 & & & & & \\ -1 & 4 & -1 & & & & \\ & & . & & & & \\ & & & . & & & \\ & & & & . & & \\ & & & -1 & 4 & -1 \\ & & & & -1 & 4 \\ \end{bmatrix}
This is solved using the finite difference method with N=1010N=10*10 subintervals

Code

%實在沒找到matlab代碼的插入方法,就插入了python的代碼塊,其實用的還是matlab,代碼複製粘貼後換行有點問題
%應該不能直接運行,把回車刪一刪應該就能用
Nx    =                        10; % Number of sub-segments in x

Ny    =                        10; % Number of sub-segments in y

a     =                         0; % Location of boundary 'a' for 'x'

b     =                         1; % Location of boundary 'b' for 'x'

c     =                         0; % Location of boundary 'c' for 'y'

d     =                         1; % Location of boundary 'd' for 'y'

f     = @(x,y)              x + y; % Defining RH-side function 'f(x,y)'

 

function [gxy] = g(x,y,a,b,c,d)    % Defining boundary condition 'g(x,y)'

 

 gxy = 0;   % Default value

 

 if (x==a)  % g(a,y), Left BC

  gxy = 0;

 end

 

 if (x==b)  % g(b,y), Right BC

  gxy = 1;

 end

 

 if (y==c)  % g(x,y), Bottom BC

  gxy = 0;

 end

 

 if (y==d)  % g(x,y), Top BC

  gxy = 1;

 end

 

end

 

%%% Setting up System of Eq's Au=B: %%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

x = linspace(a,b,Nx+1); % Defining locations 'x'

y = linspace(c,d,Ny+1); % Defining locations 'x'

h =         (b-a)/Nx;
% Sub-interval size, assumed hx=hy=h

 

% Defining "D" Block

D(1) = 4;

for ii = 2:(Nx-1)

 D(ii,ii-1) = -1;

 D(ii-1,ii) = -1;

 D(ii,ii)   =  4;

end

 

% Defining "I" Block

I = -eye([Nx-1,Nx-1]);

 

A(1:(Nx-1),1:(Nx-1)) = D;

for ii = 2:(Ny-1)

 A((ii-1)*(Nx-1)+1:(ii-1)*(Nx-1)+Nx-1,...

  
(ii-1)*(Nx-1)+1:(ii-1)*(Nx-1)+Nx-1) = D;

 A((ii-2)*(Nx-1)+1:(ii-2)*(Nx-1)+Nx-1,...

  
(ii-1)*(Nx-1)+1:(ii-1)*(Nx-1)+Nx-1) = I;

 A((ii-1)*(Nx-1)+1:(ii-1)*(Nx-1)+Nx-1,...

  
(ii-2)*(Nx-1)+1:(ii-2)*(Nx-1)+Nx-1) = I; 

end

 

% Defining RHS

B = zeros((Ny-1)*(Nx-1),1);

for ii = 1:(Ny-1)

 for jj = 1:(Nx-1)

  

  B((ii-1)*(Nx-1)+jj)
= -h^2*f(x(jj+1),y(ii+1));

  

  % Adding BC to 'B'

  if (ii==1)

   B((ii-1)*(Nx-1)+jj)
= B((ii-1)*(Nx-1)+jj) + g(x(jj+1),y(ii),a,b,c,d);

  end

  

  % Adding BC to 'B'

  if (ii==(Ny-1))

   B((ii-1)*(Nx-1)+jj)
= B((ii-1)*(Nx-1)+jj) + g(x(jj+1),y(ii+2),a,b,c,d);

  end

  

  % Adding BC to 'B'

  if (jj==1)

   B((ii-1)*(Nx-1)+jj)
= B((ii-1)*(Nx-1)+jj) + g(x(jj),y(ii+1),a,b,c,d);

  end

  

  % Adding BC to 'B'

  if (jj==(Nx-1))

   B((ii-1)*(Nx-1)+jj)
= B((ii-1)*(Nx-1)+jj) + g(x(jj+2),y(ii+1),a,b,c,d);

  end

  

 end

end

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

u = A\B; % Solving Au=b for unknowns'u'

 

% Placing solution 'u' inside larger array with BCs

uu = zeros(Ny+1,Nx+1);%

for ii = 1:(Ny+1)

 for jj = 1:(Nx+1)

  uu(ii,jj) =
g(x(jj),y(ii),a,b,c,d);

 end

end

uu(2:(end-1),2:(end-1)) = reshape(u,(Nx-1),(Ny-1))';

在這裏插入圖片描述

Iteration method

clear; % Clear stored variables
h = 0.1
Nx    =                        10; % Number of sub-segments in x
Ny    =                        10; % Number of sub-segments in y
a     =                         0; % Location of boundary 'a' for 'x'
b     =                         1; % Location of boundary 'b' for 'x'
c     =                         0; % Location of boundary 'c' for 'y'
d     =                         1; % Location of boundary 'd' for 'y'
f     = @(x,y)              x + y; % Defining RH-side function 'f(x,y)'
u = zeros(Nx,Ny);
u(1,:) = a;
u(Nx,:) = b;
u(:,1) = c;
u(:,Ny) = d;
for i = 1:1000
   for x = 2:Nx-1
       for y = 2:Ny-1
           u(x,y) = 0.25*(u(x-1,y)+u(x+1,y)+u(x,y+1)+u(x,y-1))-h^2*f(x,y);
       end
   end
end

在這裏插入圖片描述

Two method has different result. BUT why?
I will explain it in the future.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章