Problem1.二维possion方程:
{ Δ u = 2 π 2 e π ( x + y ) ( s i n π x c o s π y + c o s π x s i n π y ) , ( x , y ) ∈ G = ( 0 , 1 ) × ( 0 , 1 ) , u = 0 , ( x , y ) ∈ G . \begin{cases} &\Delta u=2\pi^2e^{\pi(x+y)}(sin\pi x cos\pi y+cos\pi xsin\pi y),(x,y)\in G=(0,1)\times (0,1),\\ &u=0,(x,y)\in G. \end{cases} {Δu=2π2eπ(x+y)(sinπxcosπy+cosπxsinπy),(x,y)∈G=(0,1)×(0,1),u=0,(x,y)∈G.
精确解: u ( x , y ) = e π ( x + y ) s i n π x s i n π y . u(x,y)=e^{\pi(x+y)}sin\pi x sin\pi y . u(x,y)=eπ(x+y)sinπxsinπy.
n=200;m=300;
[err1,u1,U1,X1,Y1]=FE2(n,m);
figure(1)
s=surf(X1,Y1,err1);
s.EdgeColor = 'interp';
s.FaceColor = 'w';
figure(2)
err_max=[];
Nvec=2.^[4:11];
for N=Nvec
[err,u,U,X,Y]=FE2(N,N);
err_max=[err_max,max(max(err))];
end
plot(log10(Nvec),log10(err_max),'ro-')
hold on
plot(log10(Nvec), log10(Nvec.^(-2)), '--')
grid on
xlabel('log_{10}N'); ylabel('log_{10}err');
ax = [0.4 0.40];
ay = [0.4 0.45];
annotation('textarrow',ax,ay,'String','slope = -2 ','fontsize',14)
function [err,u,U,X,Y]=FE2(n,m)
h1=1/m;h2=1/n;
x=[0:h1:1]';y=[0:h2:1]';
[X,Y]=meshgrid(y,x);
%解析解
U=exp(pi*(X+Y)).*sin(pi*X).*sin(pi*Y);
X1=X(2:m,2:n);
Y1=Y(2:m,2:n);
f=2*pi^2*exp(pi*(X1+Y1)).*(sin(pi*X1).*cos(pi*Y1)+cos(pi*X1).*sin(pi*Y1));
e1=ones(m-1,1);e2=ones(n-1,1);
dxx=1/h1^2*spdiags([e1,-2*e1,e1],[-1,0,1],m-1,m-1);
dyy=1/h2^2*spdiags([e2,-2*e2,e2],[-1,0,1],n-1,n-1);
A=kron(speye(n-1),dxx)+kron(dyy,speye(m-1));
u=zeros(m+1,n+1);
u(2:m,2:n)=reshape(A\f(:),m-1,n-1);
err=abs(u-U);
end
结果图: