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 Center および File ExchangeBoundary Conditions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by