为什么我的解决方案没有衰减

Why is my solution not decaying

我正在使用前向时间中心 space 有限差分法对热方程进行数值求解。

一旦我采用足够小的空间步长,我的解决方案就不再衰减。这是为什么?

我似乎无法弄明白,非常感谢您的帮助。

这是我的代码:

TI=0; %Intial time
TF=1; %Final Time

sigma=2;
dx=0.01;
dt=(dx^2)/(5*sigma); %Ensure the criteria on r is met by chooding delta t in this way

r=sigma*dt/(dx^2);

x=-1:dx:1;
phi=sin(pi*x); %Initial cpndition

old=phi; %To be used in the algorithm
new=zeros(1,numel(x));

timesteps=(TF-TI)/dt; %Classically called n
timesteps=int8(timesteps); %Ensure this number is an integer so it doesnt make matlab mad
spacesteps=numel(x);


M=zeros(timesteps,spacesteps);

M(1,:)=phi;  %Going to put all my computed time steps into a matrix.



for i=2:timesteps

    %Now take dx space steps

    for j=2:spacesteps-1

        new(1)=0;
        new(end)=0;
        new(j)=old(j) + r*(old(j+1)-2*old(j)+old(j-1));

    end

    M(i,:)=new;

    old=new;
    new=zeros(1,numel(x));

end


DIM_M=size(M);

[X,T]=meshgrid(linspace(-1,1,DIM_M(2)),linspace(0,TF,DIM_M(1)));

figure(1)
surf(X,T,M);
xlabel('x')
ylabel('t')
title('Numerical Solution')
shading interp

AS=exp(-sigma*pi^2*T).*sin(pi*X);

figure(2)
surf(X,T,AS)
xlabel('x')
ylabel('t')
title('Actual Solution')
shading interp

Error=AS-M;

figure(3);
surf(X,T,Error)
shading interp

选择一些导致不当行为的行。

TI=0;
TF=1;

sigma=2;
dx=0.01;
dt=(dx^2)/(5*sigma); // .01^2 == .0001, 5*sigma == 10, dt := .00001

timesteps=(TF-TI)/dt; // TF-TI == 1, 1/.00001 == 10000, timesteps := 10000
timesteps=int8(timesteps);

最后一行出现了超出范围的情况。 MATLAB 将类型转换中的溢出剪辑到最接近的可表示值,因此您实际上得到了 timesteps = 127 并且违反了 TF == TI + dt * timesteps; 的不变量。模拟将在 TF.

之前停止