pde matlab code for wave equation
12 次查看(过去 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!