pde matlab code for wave equation
조회 수: 11 (최근 30일)
이전 댓글 표시
this is my code so far, however it seems matlab is not going through my iteration and simply plotting the initial condition. can someone please check to see where i am going wrong?
% setup and parameters
Nx = 50; % number of mesh points
dx = 10/Nx; % spactial mesh
Tend = 1;% solve from t=0..tmax
c = 0.2*ones(Nx-1,1); % vector of ones , size Nx-1
dt = 0.95*dx/max(c);% time step
t = 0:dt:Tend; % time mesh
R = round(Tend/dt); % number of timesteps
x = linspace(0,10,Nx-1);% create spatial coordinates vector for plots
% set up differentiation matrices
e = ones(Nx-1,1); % vector of ones of same size x
Dx =(spdiags([-e e],[-1 1],Nx-1,Nx-1)); % 1st order matrix
Dxx = (spdiags([e -2*e e], [-1 1], Nx-1,Nx-1)); % 2nd order matrix
% initialization and initial conditions, u = zero at boundaries
u = exp(-100 * (x-5).^2);
data = u; % store data
for n = 2:R-1 % iteratre, step in time
for j= 2:Nx-1
u(j,n+1) = u(j,n) - 0.5*dt/dx * c.*(Dx*u(j,n)) + 0.5*(dt/dx)^2 * c.^2*(Dxx*u(j,n)) ...
+ 0.125*(dt/dx)^2 * c.*(Dx*c).*(Dx*u(j,n));
end
end
(plot(x,data,'o - r'));xlabel('x'); ylabel('u'); title('Solution at t=1')
댓글 수: 3
답변 (1개)
Sigurd Askeland
2018년 4월 26일
It seems you are plotting data in stead of u. When you set data = u before the for loop, any subsequent changes to u are not reflected in data. data is a copy of u, and does not point to the same memory address.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Geometry and Mesh에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!