clear
close all
clc
%example-2,求解温度场；
%利用此程序验证：不同初始猜测对收敛速度有影响，但对结果基本没有影响

N=100;
u=zeros(N+1,N+1);
u(N+1,:)=10; %边界条件
u1=u;
uold=u;
uold_1=u1;
u(2:N,2:N)=u(2:N,2:N)+2.5; %不同初始猜测对收敛速度有影响，但对结果基本没有影响
u1(2:N,2:N)=u1(2:N,2:N)+17.1; 
nn=0;
tic
while max(max(abs(u-uold)))>=0.0000001
    uold=u;   
    u(2:N,2:N)=0.25*(u(3:N+1,2:N)+u(1:N-1,2:N)+u(2:N,3:N+1)+u(2:N,1:N-1));
    nn=nn+1;
end
toc
nn
mm=0;
tic
while max(max(abs(u1-uold_1)))>=0.0000001
    uold_1=u1;   
    u1(2:N,2:N)=0.25*(u1(3:N+1,2:N)+u1(1:N-1,2:N)+u1(2:N,3:N+1)+u1(2:N,1:N-1));
    mm=mm+1;
end
toc
mm
figure,surf(u);
figure,[c,h]=contour(u,20);
set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2);
colormap cool
title('温度场的平面等高图')
figure;surf(u-u1)