Introduction to the problem
Consider the boundary-value problem
Boundary condition:
gird in x direction
gird in y direction
length if the grid
These equations can be arranged in the form
Where, for intervals in and , is an -by- matrix of the form
where is in -by- matrix of the form
This is solved using the finite difference method with 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.