Index exceeds matrix dimensions (Thermal 1d model)

2 ビュー (過去 30 日間)
Aditya Khuller
Aditya Khuller 2017 年 6 月 28 日
コメント済み: Aditya Khuller 2017 年 7 月 5 日
I'm trying to write a 1D thermal model using a finite difference scheme. I'm still an amateur programmer so I apologize for my ignorance. I keep getting this error: Index exceeds matrix dimensions. I know this means that I'm trying to access an element in an array using an index that exceeds the dimension of the array but I don't know what to do with that information.
N = 15;
% Geometrically increasing grid spacing
z = 0;
zs = [kappa*P/pi]^(1/2);
deltaz = zs/10;
for i = 1:N
deltaz(i+1) = deltaz(i)*(1+1/5);
z(i+1) = z(i)+deltaz(i);
T0 = [[(S0/R^2).*(1-A)]/(e*(5.67*10^-8))]^(1/4);
TN = T0/((2)^(1/2));
T(i+1) = TN - (TN-T0)*exp(-z(i+1)/0.06);
zor(i+1) = deltaz(i+1)*deltaz(i)*(deltaz(i+1)+deltaz(i));
alpha_model(i+1) = 2*K*deltaz(i+1)/zor(i+1);
beta_model(i+1) = 2*K*deltaz(i)/(zor(i+1));
for n= 1:N
T(i+1,n+2) = T(i+1,n+1) + [dt/(rho*cp)].*[alpha_model(i+1).*T(i,n+1)-(alpha_model(i+1) + beta_model(i+1)).*T(i+1,n+1) + beta_model(i+1).*T(i+2,n+1)];
end
end
  1 件のコメント
Jan
Jan 2017 年 6 月 28 日
編集済み: Jan 2017 年 6 月 28 日
Start with posting the complete message. Then the readers do not have to guess, which line is failing.
We cannot run your code, because
zs = [kappa*P/pi]^(1/2);
is failing due to unknown kappa. By the way:
zs = sqrt(kappa * P / pi);
is nicer and faster. Do not use square brackets without any reasons. See https://www.mathworks.com/matlabcentral/answers/35676-why-not-use-square-brackets . [] is the concatenation operator.

サインインしてコメントする。

採用された回答

Jan
Jan 2017 年 6 月 28 日
You define
T(i+1) = TN - (TN-T0)*exp(-z(i+1)/0.06);
but access
T(i+1, n+1)
  1 件のコメント
Aditya Khuller
Aditya Khuller 2017 年 7 月 5 日
Thank you for your help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by