pde matlab code for wave equation
11 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Sigurd Askeland
on 26 Apr 2018
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 Comments
See Also
Categories
Find more on Geometry and Mesh in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!