errors in this code - 1D PDE with Boundary Condition

조회 수: 2 (최근 30일)
tian
tian 2011년 4월 19일
i am trying to solve ade and getting errors. plz help.
K = 0.15;
D = 100;
U = 1;
dt = 0.005; dx = 1;
L = 10;
t = 50; T = 100;
c0 = 100; C0 = 100; %cin = 100mg/m^3
nx = 1+L/dx; nt = 1+t/dt;
c = zeros(nx,nt);
NT = 1+T/dt;
C = zeros(nx,NT);
c(1,:) = c0; C(1,:) = C0;
for k = 1:NT
CN = U*dt/dx;
DN = D*dt/dx^2;
C(1,k) = c0;
for i = 1:nx
if (i == 1)
C(i,k+1) = (2*DN)*c0+c(i,k+1)*(1-(2*DN)-(k*dt)-((DN-CN/2)*(2*dx*U/D)))+((DN-(CN/2)*(2*dx*c0*U/D)));
elseif (i == nx)
C(i,k+1) = 2*DN*c(i-1,k)+(1-2*DN-k*dt)*c(i,k);
else
C(i,k+1) = (DN+CN/2)*C(i-1,k) + (1-2*DN-K*dt)*C(i,k) +(DN-CN/2)*C(i+1,k);
end
end
end
x = [0:dx:L];
plot(x,C(:,NT),'b');
return

답변 (1개)

Matt Tearle
Matt Tearle 2011년 4월 19일
I don't know exactly what you're trying to do with t and T (and associated nt and NT), but the result is that c and C have different numbers of columns. Your calculation of C(i,k+1) depends on c(i,k), and k runs from 1 to NT. Given that NT > nt, you end up referencing columns of c that don't exist -- ie k gets to nt+1 and then referencing c(i,k) must fail.
  댓글 수: 1
tian
tian 2011년 4월 19일
yes exactly! my error message is this
??? Attempted to access c(1,10002); index out of
bounds because size(c)=[11,10001].
Error in ==> trying at 49
C(i,k) =.......
how to calculate in such a way that it calculates within matrix

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Boundary Conditions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by