MATLAB Answers

I'm trying to solve 1d wave equation using finite difference method. But i'm not sure that this solution is correct. Can somebody tell me what i'm doing wrong ?

조회 수: 1(최근 30일)
Artem Sulim
Artem Sulim 2021년 6월 20일
clear all
close all
clc
Lx = 10;
dx = 0.1;
Nx = Lx/dx;
T = 100;
dt = 0.1;
t = 0;
a = 0.2;
C = a*dt/dx;
un = zeros(1,Nx);
un(1,[1 end])=0;
unm1 = un;
unp1 = un;
%--------------------------------------------------------------------------
n = 23;
if mod(n,2)~=0
x = linspace(0,Lx/2, n);
un0 = zeros(1,Nx);
un0([Nx/2-(n-1)/2:Nx/2+(n-1)/2])= 0.8*exp(-(2*x-Lx/2).^2);
else
x = linspace(0,Lx/2, n);
un0 = zeros(1,Nx);
un0([Nx/2-fix((n-1)/2):Nx/2+(n)/2])= 0.8*exp(-(2*x-Lx/2).^2*2);
end
%--------------------------------------------------------------------------
F = figure()
while t<T
if t==0
un = un0;
end
unm1 = un;
un = unp1
t = t+dt;
for i = 2:Nx-1
unp1(i)=2*un(i)-unm1(i)+C^2*(un(i+1)-2*un(i)+un(i-1));
end
if isvalid(F)
plot(unp1,'-b')
grid on
ylim([-10 10])
pause(0.005)
else
break
end
end

답변(0개)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by