Finite difference numerical method no flux boundary

4 views (last 30 days)
Hi, I am trying to inplement a FTCS scheme which finds the concentration C at certain time tEnd. The boundary condition at w (end of y) have no flux. This is what i have come up with so far, but it is giving me an error. How do I fix this? Thanks!
Index in position 1 exceeds array bounds (must not exceed 1).
Error in numerical2d (line 24)
C(i,n+1) = C(i,n)+sigma*(C(i-1,n)-2*C(i,n)+C(i+1,n));
y=linspace(0,w,ny+1); %ny=128
dy=y(2)-y(1);
dt= dy^2/(2*D);
nt=tEnd/dt+1; %calculate the number of timesteps required
sigma = (D*dt)/(dy^2);
C = zeros(length(ny),length(nt));
C(1,:)=C0; %boundary
C(:,1) =0; %initial
for n = 2:nt-1
% Loop over internal points
for i= 2:ny-1
C(i,n+1) = C(i,n)+sigma*(C(i-1,n)-2*C(i,n)+C(i+1,n));
end
C(ny,n+1) = C(ny-1,n+1);
%boundary condition at w
end

Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!