0% found this document useful (0 votes)
34 views4 pages

2 3wave

This Matlab code simulates 1D and 2D wave propagation over time using finite difference methods. For the 1D case, it sets up the domain and time step, applies an initial condition and reflecting boundary conditions, then iterates through time using an explicit update formula. For the 2D case, it similarly sets up the 2D domain and time step, applies an initial condition and reflecting boundary conditions on the edges with an additional source term, then iterates through time using a 2D explicit update formula, plotting the results.

Uploaded by

Haseeb Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views4 pages

2 3wave

This Matlab code simulates 1D and 2D wave propagation over time using finite difference methods. For the 1D case, it sets up the domain and time step, applies an initial condition and reflecting boundary conditions, then iterates through time using an explicit update formula. For the 2D case, it similarly sets up the 2D domain and time step, applies an initial condition and reflecting boundary conditions on the edges with an additional source term, then iterates through time using a 2D explicit update formula, plotting the results.

Uploaded by

Haseeb Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

%Matlab-code for 1-D wave propagation:

clc
clear all
close all

%Domain

%space
Lx=10;
dx=0.1;
nx=fix(Lx/dx);
x=linspace(0, Lx, nx);

%Time
T=20;

%%Field variable
wn=zeros(nx,1);
wnm1=wn;%w at time n-1
wnp1=wn;%w at time n+1

CFL=1;
c=1;
dt=CFL*dx/c;

%Initial condition
wn(49:51)=[0.1 0.2 0.1];
wnp1(:)=wn(:);

%Time loop
t=0;

while(t<T)
%reflecting boundary condition
wn([1 end])=0;

%solution
t=t+dt;
wnm1=wn;
wn=wnp1;
for i=2:nx-1
wnp1(i)=2*wn(i)-wnm1(i)+CFL^2*(wn(i+1)-2*wn(i)+wn(i-1));
end
clf%Clear current figure window
plot(x,wn)
shg; %Show most recent graph window
pause(0.01)
end
%Matlab-code for 2-D wave propagation:
clc
clear all
close all

%Domian

%space
Lx=10;
Ly=10;
dx=0.1;
dy=0.1;
nx=fix(Lx/dx);
ny=fix(Ly/dy);
x=linspace(0, Lx, nx);
y=linspace(0, Ly, ny);

%Time
T=20;

%%Field variable
wn=zeros(nx,ny);
wnm1=wn;%w at time n-1
wnp1=wn;%w at time n+1

CFL=0.5;
c=1;
dt=CFL*dx/c;

%Initial condition
%wn(49:51)=[0.1 0.2 0.1];
wnp1(:)=wn(:);

%Time loop
t=0;

while(t<T)
%reflecting boundary condition
wn(:,[1 end])=0;
wn([1 end],:)=0;
%solution
t=t+dt;
wnm1=wn;
wn=wnp1;

%source
wn(50,50)=dt^2*20*sin(30*pi*t/T);

%absorbing boundary condition

for i=2:nx-1
for j=2:ny-1
wnp1(i,j)=2*wn(i,j)-
wnm1(i,j)+CFL^2*(wn(i+1,j)+wn(i-1,j)-
4*wn(i,j)+wn(i,j+1)+wn(i,j-1));
end
end
clf %Clear current figure window
subplot(2,1,1)
imagesc(x,y,wn'); colorbar;
caxis([-0.02 0.02])
colormap(jet)
subplot(2,1,2)
mesh(x,y,wn'); colorbar;
caxis([-0.02 0.02]) % sets the colormap limits
shg; %Show most recent graph window
pause(0.01)
end

You might also like