clear,clc;
n=9; % grid number
h=0.25; % step length
u=zeros(n,n);
u(:,1)=1; % top boundary
u(:,n)=0; % bottom boundary
for k=1:1000 % maximum iteration
u0=u;
% left boundary
u(2,2:n-1)=(u(3,2:n-1)+u(2,1:n-2)+u(2,3:n)-0.5*h)/5.0;
u(1,2:n-1)=u(2,2:n-1)-0.5*h;
% right boundary
u(n-1,2:n-1)=(u(n-2,2:n-1)+u(n-1,1:n-2)+u(n-1,3:n)-0.5*h)/5.0;
u(n,2:n-1)=u(n-1,2:n-1)-0.5*h;
% middle grid
u(3:n-2,2:n-1)=(u(2:n-3,2:n-1)+u(4:n-1,2:n-1)+u(3:n-2,1:n-2)+u(3:n-2,3:n))/6.0;
delta=max(max(abs(u-u0)));
if(delta<1e-8) % precision
disp(['Number of iteration: ',num2str(k)]);
disp(u');
break;
end
end
运行下这个试试