椭圆方程的有限差分方法

时间:2017-04-19 21:07:48

标签: matlab math pde

如果R是平面(0,1)x(0,2)的区域,设L是2-d拉普拉斯算子,并考虑R上的泊松方程Lu = 4.一个解是函数v( x,y)=(xy)^ 2。设g是v对R边界的限制。为了得到它,我们设定h = k = 1/2,m = 3,n = 5.

这是我的代码:但它不起作用。

function w=poisson(xl,xr,yb,yt,M,N)
f=@(x,y) 0; % define input function data
g1=@(x) (x^2-2*x+1); % define boundary values
g2=@(x) (x^2-4*x+4); % Example 8.8 is shown
g3=@(y) y^2;
g4=@(y) y^2;
m=M+1;n=N+1; mn=m*n;
h=(xr-xl)/M;h2=h^2;k=(yt-yb)/N;k2=k^2;
x=xl+(0:M)*h; % set mesh values
y=yb+(0:N)*k;
A=zeros(mn,mn);b=zeros(mn,1);
for i=2:m-1 % interior points
  for j=2:n-1
    A(i+(j-1)*m,i-1+(j-1)*m)=1/h2;A(i+(j-1)*m,i+1+(j-1)*m)=1/h2;
    A(i+(j-1)*m,i+(j-1)*m)=-2/h2-2/k2;
    A(i+(j-1)*m,i+(j-2)*m)=1/k2;A(i+(j-1)*m,i+j*m)=1/k2;
    b(i+(j-1)*m)=f(x(i),y(j));
  end
end
for i=1:m % bottom and top boundary points
  j=1;A(i+(j-1)*m,i+(j-1)*m)=1;b(i+(j-1)*m)=g1(x(i));
  j=n;A(i+(j-1)*m,i+(j-1)*m)=1;b(i+(j-1)*m)=g2(x(i));
end
for j=2:n-1 % left and right boundary points
  i=1;A(i+(j-1)*m,i+(j-1)*m)=1;b(i+(j-1)*m)=g3(y(j));
  i=m;A(i+(j-1)*m,i+(j-1)*m)=1;b(i+(j-1)*m)=g4(y(j));
end
v=A\b; % solve for solution in v labeling
w=reshape(v(1:mn),m,n);  %translate from v to w
mesh(x,y,w')

Matthias Eisen

1 个答案:

答案 0 :(得分:1)

根据您的问题描述,我更改了以下内容并得到了答案:

h=1/2;