I am getting Index exceeds the number of array elements(1) error?

1 回表示 (過去 30 日間)
Ajaykumar MST
Ajaykumar MST 2021 年 6 月 19 日
編集済み: Jan 2021 年 6 月 19 日
I have attached my code
close all
clear all
clc
% Input values
n = 31;
x = linspace(0,3,n);
dx = x(2)-x(1);
gamma = 1.4;
% Calculate initial profile
rho = 1-0.3146*x; % Density
t = 1-0.2314*x; % Temperature
v = (0.1+1.09*x).*t.^0.5; % Velocity
a = 1+2.2*(x-1.5).^2; %Area
% Time steps
nt = 100;
dt = 0.001;
% Outer time loop
for k=1:nt
rho_old = rho;
v_old = v;
t_old = t;
% Predictor step
% Continuity Equation
for j = 2:n-1
%drho_dt_p = -rhodv_dx-rhovdloga_dx-vdrho_dx main equation
% Sepearting the terms for easy debugging
dv_dx = (v(j+1)-v(j))/dx;
dloga_dx = (log(a(j+1))-log(a(j)))/dx;
drho_dx = (rho(j+1)-rho(j))/dx;
dt_dx = (t(j+1)-t(j))/dx;
drho_dt_p = -rho(j)*(dv_dx-rho(j))*v(j)*(dloga_dx-v(j))*(drho_dx);
% Momentum Equation
dv_dt_p = -v(j)*dv_dx-(1/gamma)*((dt_dx)+(t(j)/rho(j))*drho_dx);
% Energy Equation
dt_dt_p = -v(j)*dt_dx-(gamma-1)*t(j)*((drho_dx)+v(j)*dloga_dx);
% Solution Update
rho(j) = rho(j)+drho_dt_p(j)*dt;
v(j) = v(j)+dv_dt_p(j)*dt;
t(j) = t(j)+dt_dt_p(j)*dt;
end
end
  1 件のコメント
Jan
Jan 2021 年 6 月 19 日
You do get an error message, which tells, in which line the error occurs. It helps to solve your problem, if you share this important information with the readers.

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

回答 (1 件)

Jan
Jan 2021 年 6 月 19 日
編集済み: Jan 2021 年 6 月 19 日
With guessing where the error occurs:
dv_dt_p = -v(j)*dv_dx-(1/gamma)*((dt_dx)+(t(j)/rho(j))*drho_dx);
dt_dt_p = -v(j)*dt_dx-(gamma-1)*t(j)*((drho_dx)+v(j)*dloga_dx);
Both variables are scalars. Then indexing them with j does not work:
rho(j) = rho(j)+drho_dt_p(j)*dt;
% ^^^ omit this
v(j) = v(j)+dv_dt_p(j)*dt;
% ^^^ omit this
t(j) = t(j)+dt_dt_p(j)*dt;
% ^^^ omit this
Use the debugger to find the cause of such problems:
dbstop if error
Then run the code until Matlab stops. Now check, which of the indices is out of range:
j
size(v)
size(dv_dt_p)
size(drho_dt_p)
  1 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2021 年 6 月 19 日
Yes sir, I noticed that ..thanks for clarification.

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by