https://www.youtube.com/watch?v=MqlDQzw3x6Q
Lab12_1: Wave Equation 1D
Lx = 10;
dx = 0.1;
nx = fix(Lx/dx);
x = linespace(0,Lx,nx);
T = 20;%Time
wn = zeros(nx,1);
wnm1 = wn; % at time n - 1
wnp1 = wn; % at time n + 1
CFL = 1; %CFL = c.dt/dx
c = 1;
dt = CFL * dx / c;
% initial condition
wn(49:51) = [0.1,0.2,0.1];
wnp1(:) = wn(:);
%% Time Stepping loop
t = 0;
while t < T
% reflecting boundary condition
wn([1 end]) = 0;
t = t + dt;
wnm1 = wn;wn = wnp1;
for i = 2:nx-1
wnp1(i) = 2*wn(i) - wnm1(i) + CFL^2*(wn(i+1)-2wn(i)+wn(i+1));
end
end
反射条件,波会来回反射,吸收条件,波碰到边界就会消失。
rho = 1; %density
cp = 1;%specific heat
k = 1;%thermal conductivity
A = K / rho / cp;
Lx = 10;
Nx = 12;
NT = 500;
dx = Lx/(Nx - 1);
%CFL Condtion
c = 1;%Speed
C = 0.1;%Courant Number CFL Condition C < 1;
dt = C * dx / c;
%Initial Condition
Tn(:) = 0;
t = 0;
for n = 1:NT
Tc = Tn;
t = t + dt;
for i = 2:Nx = 1
Tn(i) = Tc(i) + dt * A *((Tc(i+1) - 2*Tc(i) + Tc(i))/dx/dx);
end
%Source Term
Sx = round(7 * Nx / Lx);
if(t < 5)
{
Tn(Sx) = Tn(Sx) + dt * 10 / rho / cp;
}
Tn(1) = Tn(2);%黎曼边界条件
Tn(end) = 0;
end