一维抛物方程紧致差分MATLAB(时间二阶空间四阶
精品资料 欢迎下载 %%%%%Parabolic equation u_t-Laplace(u)=f (x,t) [0,1]*[0,1]%%% %%%%% u(x,0)=0 %%% %%%%% u(0,t)=u(1,t)= 0 %%%% %%%%% genuine solution u(x,t)=sin(pi*x)*sin(t) %%% %%%%% f(x,t)=sin(pi*x)*cos(t)+pi^2*sin(pi*x)*sin(t) %%%%% N=40; h=1/N; x=0:h:1; M=80; d=1/M; t=0:d:1; r=d/(2*h^2); A=zeros(N-1,N-1); B=zeros(N-1,N-1); u=zeros(N+1,M+1); for i=1 A(i,i)=5/6; A(i,i+1)=1/12; B(i,i)=2*r; B(i,i+1)=-r; end for i=N-1 A(i,i-1)=1/12; A(i,i)=5/6; B(i,i-1)=-r; B(i,i)=2*r; end for i=2:N-2 A(i,i-1)=1/12; A(i,i)=5/6; A(i,i+1)=1/12; B(i,i-1)=-r; B(i,i)=2*r; B(i,i+1)=-r; end f=zeros(N+1,M+1); for i=1:N+1 u(i,2)=sin(pi*(i-1)*h)*cos(d)*d; end for j=3:M+1 for i=2:N; f(i,j-1)=sin(pi*(i-1)*h)*cos((j-2)*d)+pi^2*sin(pi*(i-1)*h)*sin((j-2)*d); end u(2:N,j)=(A+B)\(2*d*A*f(2:N,j-1)-2*B*u(2:N,j-1)+(A-B)*u(2:N,j-2)); end u_exact=zeros(N+1,M+1); for i=1:N+1 for j=1:M+1 u_exact(i,j)=sin(pi*(i-1)*h)*sin((j-1)*d); end end [x,t]=meshgrid(x,t); subplot(1,2,1) surf(u) subplot(1,2,2) surf(u_exact) % subplot(1,2,1) % plot(x,u(:,M+1)) % subplot(1,2,2) % plot(x,u_exact(:,M+1)) Err=max(abs(u-u_exact)); L2_err=norm(u-u_exact)*h^(1/2)*d^(1/2); % Err=max(abs(u(:,M+1)-u_exact(:,M+1))); % L2_err=norm(u(:,M+1)-u_exact(:,M+1))*h^(1/2); Err L2_err %L2_err(j)