pde matlab code for wave equation

11 次查看(过去 30 天)
nandine
nandine 2018-4-26
评论: Torsten 2018-4-26
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 个评论
nandine
nandine 2018-4-26
R is my time step,and I need my scheme to start at t=0 to t=1. In in increments of dt
Torsten
Torsten 2018-4-26
You use for n=2:R-1, thus for n=2:0, thus the loop is never executed.

请先登录,再进行评论。

回答(1 个)

Sigurd Askeland
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.

类别

Help CenterFile Exchange 中查找有关 Geometry and Mesh 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by