原理如上
求解以下方程
function u=ExplicitFDM_Heat(M,N)
if(~exist('M','var'))
M = 50;
end
if(~exist('N','var'))
N = 15;
end
% M=50;
% N=15;
u=zeros(N+1,M+1);
k=1;
dx=1/N;
dt=0.1/M;
x=0:dx:1;
t=0:dt:0.1;
rho=k*dt/dx^2;
% u(1,:)=-5*t; g=-5*t;% g(t)
% u(N+1,:)=5*t; h=5*t;% h(t)
u(1,:)=0; g=zeros(M+1,1);% g(t)
u(N+1,:)=0; h=zeros(M+1,1);% h(t)
u(:,1)=sin(2*pi*x); f=sin(2*pi*x); % f(x)=sin(2pix)
A=zeros(N-1,N-1);
for i =1:N-1
if i==1
A(1,1:2)=[1-2*rho,rho];
elseif i==N-1
A(i,i-1:i)=[rho,1-2*rho];
else
A(i,i-1:i+1)=[rho,1-2*rho,rho];
end
end
B=zeros(N-1,M-1);
for i=1:M
B(1,i)=g(i);
B(N-1,i)=h(i);
end
% u(:,1)=A*u0+B()
for k=1:M
u(2:N,k+1) = A*u(2:N,k)+B(:,k); % (N-1,1)=(N-1,N-1)*(N-1,1)+(N-1,1)
end
end
% 主程序
M=50;
N=20;
u=zeros(N+1,M+1);
k=1;
dx=1/N;
dt=0.1/M;
x=0:dx:1;
t=0:dt:0.1;
u=ExplicitFDM_Heat(M,N)
figure(1)
[X,T]=meshgrid(x,t);
surf(X,T,u')