Matlab PDE有限方法

时间:2018-04-26 21:59:02

标签: matlab

有人可以告诉我如何更改my code,因为它似乎不是迭代而只是绘制初始条件?

% PARAMETERS
Nx=50;
x = linspace(0,10,Nx-1)';
dx = 10\Nx;
tmax = 1;
dt =dx\(c);
R = round(tmax\dt);
c = 1/5*ones(Nx-1,1); %  vector  of ones , size Nx-1

% 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);

% ITERATIE 

for n = 2:R
   u(:,n+1) = u(:,n) - 0.5*dt/dx * c.*(Dx*u(:,n)) + 0.5*(dt/dx)^2 * c.^2*(Dxx*u(:,n)) ...
   + 0.125*(dt/dx)^2 * c.*(Dx*c).*(Dx*u(:,n));
end
figure;
plot(x,u(:,end),'o - r');
xlabel('x','FontSize',30);
ylabel('u','FontSize',30);
title('Solution at t=1','FontSize',30)

0 个答案:

没有答案