1d wave equation with term

 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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, here is an example code for solving the 1D compressible Euler equation in Matlab with a shock happening: ```matlab % Define the domain and initial conditions x = linspace(0,1,1000); rhoL = 1; rhoR = 0.125; uL = 0; uR = 0; pL = 1; pR = 0.1; gamma = 1.4; x0 = 0.5; tEnd = 0.25; dx = x(2)-x(1); dt = 0.01; % Initialize the solution arrays rho = ones(size(x))*rhoL; u = ones(size(x))*uL; p = ones(size(x))*pL; % Set up the shock initial condition for i = 1:length(x) if x(i) >= x0 rho(i) = rhoR; u(i) = uR; p(i) = pR; end end % Define the flux function flux = @(rho,u,p) [rho.*u; rho.*u.^2+p; u.*(gamma*p./(gamma-1)+0.5*rho.*u.^2+u.^2)]; % Solve the Euler equations with an explicit upwind scheme t = 0; while t < tEnd % Compute the time step lambda = max(abs(u) + sqrt(gamma*p./rho)); dt = dx / lambda; % Compute the fluxes at the cell interfaces F = flux(rho,u,p); Fp = F(:,2:end); Fm = F(:,1:end-1); % Compute the numerical flux using the upwind scheme Fnum = 0.5*(Fp+Fm - lambda.*(rho(:,2:end)-rho(:,1:end-1))); % Update the solution rho(:,2:end-1) = rho(:,2:end-1) - dt/dx*(Fnum(1,2:end)-Fnum(1,1:end-1)); u(:,2:end-1) = u(:,2:end-1) - dt/dx*(Fnum(2,2:end)-Fnum(2,1:end-1)); p(:,2:end-1) = p(:,2:end-1) - dt/dx*(Fnum(3,2:end)-Fnum(3,1:end-1)); % Apply boundary conditions rho(:,1) = rho(:,2); u(:,1) = u(:,2); p(:,1) = p(:,2); rho(:,end) = rho(:,end-1); u(:,end) = u(:,end-1); p(:,end) = p(:,end-1); % Update the time t = t + dt; end % Plot the final solution plot(x,rho,'-k',x,u,'-r',x,p,'-b') xlabel('x') ylabel('Density, Velocity, Pressure') legend('Density','Velocity','Pressure') title('1D Compressible Euler Equation with Shock') ``` Note that this code uses an explicit upwind scheme to solve the equations, which may not be stable for all choices of parameters. If you encounter stability issues, you may need to use a different numerical method. Additionally, this code assumes a constant gamma value of 1.4; if you need to use a different value of gamma, you will need to modify the code accordingly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值