记录一下数值计算课大作业
设时间t和空间x
离散:x
i
_{i}
i=ih , t
n
_{n}
n=n
Δ
\Delta
Δ*t (i = 0,1,…I , n = 0,1,…)
设 U(x
i
_{i}
i,t
n
_{n}
n),在该点:
对t用向前差商:
∂
u
∂
t
\frac{\partial u}{\partial t}
∂t∂u
≈
\approx
≈
U
i
n
+
1
−
U
i
n
Δ
t
\frac{U^{n+1}_i -U^{n}_i}{\Delta t}
ΔtUin+1−Uin
对用中心二阶差商:
∂
2
u
∂
x
2
\frac{\partial^2 u}{\partial x^2}
∂x2∂2u
≈
\approx
≈
U
i
+
1
n
−
2
U
i
n
+
U
i
−
1
n
h
2
\frac{U^{n}_{i+1} -2U^{n}_i+U^n_{i-1}}{h^2}
h2Ui+1n−2Uin+Ui−1n
代入上式,得到隐格式:
U
i
n
+
1
−
U
i
n
Δ
t
\frac{U^{n+1}_i -U^{n}_i}{\Delta t}
ΔtUin+1−Uin – b*
U
i
+
1
n
−
2
U
i
n
+
U
i
−
1
n
h
2
\frac{U^{n}_{i+1} -2U^{n}_i+U^n_{i-1}}{h^2}
h2Ui+1n−2Uin+Ui−1n = 0
化简得:– b ∗ Δ t h 2 ∗ U i − 1 n + 1 + ( 1 + 2 b ∗ Δ t h 2 ) ∗ U i ( n + 1 ) − b ∗ Δ t h 2 ∗ U i − 1 n = U i n \frac{b*{\Delta t}}{h^2} *U^{n+1}_{i-1}+(1+\frac{2b*\Delta t}{h^2})*U^{(n+1)}_i-\frac{b*\Delta t}{h^2}*U^n_{i-1}=U^n_i h2b∗Δt∗Ui−1n+1+(1+h22b∗Δt)∗Ui(n+1)−h2b∗Δt∗Ui−1n=Uin
写出当n=0.时i=0,1,…I的式子,利用初值和边值条件,可得系数矩阵:
A = [ 1 + 2 b ∗ Δ t h 2 − b ∗ Δ t h 2 − b ∗ Δ t h 2 1 + 2 b ∗ Δ t h 2 − b ∗ Δ t h 2 − b ∗ Δ t h 2 1 + 2 b ∗ Δ t h 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . − b ∗ Δ t h 2 1 + 2 b ∗ Δ t h 2 ] A= \begin{bmatrix} 1+\frac{2b*\Delta t}{h^2} & -\frac{b*\Delta t}{h^2} & \\ -\frac{b*\Delta t}{h^2} & 1+\frac{2b*\Delta t}{h^2} & -\frac{b*\Delta t}{h^2} \\ & -\frac{b*\Delta t}{h^2} & 1+\frac{2b*\Delta t}{h^2}\\ &... &... &...\\ &&...&...&...&\\ &&&&&\\ &&&...&...&...&\\ &&&&...&...&...&\\ &&&&&-\frac{b*\Delta t}{h^2}&1+\frac{2b*\Delta t}{h^2} \end{bmatrix} A=⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎡1+h22b∗Δt−h2b∗Δt−h2b∗Δt1+h22b∗Δt−h2b∗Δt...−h2b∗Δt1+h22b∗Δt..............................−h2b∗Δt...1+h22b∗Δt⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎤
令AU=C,其中:
U
=
(
U
1
0
,
U
2
0
,
.
.
.
U
I
−
1
1
)
=(U^0_1,U^0_2,...U^1_{I-1})
=(U10,U20,...UI−11)
C
=
(
b
∗
Δ
t
h
2
∗
U
0
1
+
U
1
0
,
U
2
0
,
.
.
.
U
I
−
2
0
+
b
∗
Δ
t
h
2
∗
U
I
−
1
0
)
=(\frac{b*\Delta t}{h^2}*U^1_0+U^0_1,U^0_2,...U^0_{I-2}+\frac{b*\Delta t}{h^2}*U^0_{I-1})
=(h2b∗Δt∗U01+U10,U20,...UI−20+h2b∗Δt∗UI−10)
可解出U,同理只需解出当n=1,2,…,时的U即可。
用matlab实现:
function sol = HeatExp2(h,maxt,deltat,a) %a为方程中的系数b,可任取
I = 1/h;
N = maxt/deltat;
sol = zeros(fix(I+1),fix(N+1));
mu = a*deltat/h^2;
vetx = [0:h:1];
veti = ones(I-1,1)
vetii = ones(I-2,1)
A=sparse(diag((1+2*mu)*veti,0)-diag(vetii*mu,1)-diag(vetii*mu,-1))
[L U]=lu(A)
for i=2:ceil((I+1)/2)
sol(i,1)=2*vetx(i);
sol(I+2-i,1)=sol(i,1);
end
b = sol(2:I,1)
for j=2:N
u=U\(L\b);
for i=2:I
sol(i,j)= u(i-1);
end
b = u;
%分别画出初始、中间和最末时刻的图:
if j==2
plot([0:h:1],sol(:,j),'c')
axis([0 1 0 1]);
xlabel('x');
ylabel('u');
hold on;
elseif j==N/2
plot([0:h:1],sol(:,j),'r')
axis([0 1 0 1]);
xlabel('x');
ylabel('u');
hold on;
elseif j==N
plot([0:h:1],sol(:,j),'y')
axis([0 1 0 1]);
xlabel('x');
ylabel('u');
hold on;
end
legend('t=0.001s','t=0.05s','t=0.1s');
end
在命令行输入:
得到结果矩阵:
得到二维图像:
画出三维图像:
figure(2)
mesh([0:h:1],[0:deltat:maxt],sol)
计算结果是合理的,因为热量逐渐扩散,至端点处消失。