第一题
h = 1/40;
t = 1/3200;
H = 41;
T = 3201;
chuzhi = @(x)(cos(pi*x));
r = t/h^2;
x = 0:1/40:1;
u = zeros(T,H);
u(1,:) = cos(pi*x);
for n = 2:T
A = zeros(H,H);
b = zeros(H,1);
for i = 2:H-1
A(i,i+1) = -r;
A(i,i) = 1 + 2*r;
A(i,i-1) = -r;
b(i,1) = u(n-1,i) + t * sin(t*n-2*t);
end
%边界条件
A(1,1) = A(1,1) + 1;
A(1,2) = A(1,2) - 1;
A(H,H-1) = A(2,H-1) + 1;
A(H,H) = A(2,H) - 1;
x = (A\b)';
u(n,:) = x;
end
jingque = @(x,t)(exp(-pi*pi*t)*cos(pi*x)+1-cos(t));
X = jingque(x,1);
运行结果自己画吧,我的实验报告格式乱掉了