I am getting Index exceeds the number of array elements(1) error?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Jan
el 19 de Jun. de 2021
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.
Respuestas (1)
Jan
el 19 de Jun. de 2021
Editada: Jan
el 19 de Jun. de 2021
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 comentario
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!