I get the error of "Array indices must be positive integers or logical values"
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Mohamed T. Adam
le 30 Déc 2022
Réponse apportée : Image Analyst
le 31 Déc 2022
Iam trying to run this code but i am getting this following error "Array indices must be positive integers or logical values", how can i fix it?
My code
clc; clear all;close all;
% Define the geometry and flow conditions for the nozzle
Tt = 3000; % total temperature in the combustion chamber, in Kelvins
pt = 1500e3; % total pressure in the combustion chamber, in pascals
m = 10; % mass flow rate through the nozzle, in kilograms/second
gamma = 1.4; % ratio of specific heats for the gas
cp = 1000; % specific heat capacity at constant pressure, in joules/kilogram-kelvin
% Define the grid for the nozzle
x_min = 0.0; % minimum x coordinate of the grid, in meters
x_max = 1.0; % maximum x coordinate of the grid, in meters
nx = 100; % number of grid points in the x direction
dx = (x_max - x_min) / nx; % grid spacing in the x direction, in meters
% Initialize the flow field
u = zeros(nx, 1); % x-component of velocity, in meters/second
p = zeros(nx, 1); % pressure, in pascals
rho = zeros(nx, 1); % density, in kilograms/meter^3
T = zeros(nx, 1); % temperature, in Kelvins
% Set the inlet boundary conditions
p(1) = pt;
T(1) = Tt;
% Iterate over the characteristics to solve for the flow field
for i = 1:nx
% Compute the x-component of the characteristic direction
char_x = u(i) * rho(i);
% Compute the flow properties at the next point along the characteristic
u(i+1) = u(i) - (dx / char_x) * (p(i) - p(i-1));
rho(i+1) = rho(i) * (u(i) / u(i+1));
T(i+1) = T(i) * (rho(i) / rho(i+1))^(gamma-1);
% Compute the pressure at the next point along the characteristic
p(i+1) = p(i) + rho(i) * (u(i) - u(i+1)) * dx;
end
% Compute the flow properties at the exit of the nozzle
exit_velocity = u(nx);
exit_density = rho(nx);
exit_pressure = p(nx);
exit_temperature = T(nx);
% Print the results to the console
fprintf('Exit velocity: %f m/s\n', exit_velocity);
fprintf('Exit density: %f kg/m^3\n', exit_density);
fprintf('Exit pressure: %f Pa\n', exit_pressure);
fprintf('Exit temperature: %f K\n', exit_temperature);
% Compute the Mach number at each point in the nozzle
Mach = u ./ sqrt(gamma * cp * T);
% Plot the flow properties as contour plots
figure;
subplot(2,2,1);
contourf(Mach);
xlabel('x (m)');
ylabel('y (m)');
title('Mach number');
subplot(2,2,2);
contourf(p/1e3);
xlabel('x (m)');
ylabel('y (m)');
title('Pressure (kPa)');
subplot(2,2,3);
contourf(T); % Plot the flow properties as contour plots
figure;
subplot(2,2,1);
contourf(Mach);
xlabel('x (m)');
ylabel('y (m)');
title('Mach number');
subplot(2,2,2);
contourf(p/1e3);
xlabel('x (m)');
ylabel('y (m)');
title('Pressure (kPa)');
subplot(2,2,3);
contourf(T);
xlabel('x (m)');
ylabel('y (m)');
title('Temperature (K)');
subplot(2,2,4);
contourf(rho);
xlabel('x (m)');
ylabel('y (m)');
title('Density (kg/m^3)');
% Plot the velocity magnitude as a line plot
figure;
plot(sqrt(u.^2 + v.^2));
xlabel('x (m)');
ylabel('Velocity magnitude (m/s)');
title('Velocity magnitude');
0 commentaires
Réponse acceptée
Sulaymon Eshkabilov
le 30 Déc 2022
Make these corrections in your code's loop iteration:
...
for i = 2:nx
% Compute the x-component of the characteristic direction
char_x = u(i-1) * rho(i-1);
% Compute the flow properties at the next point along the characteristic
u(i) = u(i-1) - (dx / char_x) * (p(i) - p(i-1));
rho(i) = rho(i-1) * (u(i-1) / u(i));
T(i) = T(i-1) * (rho(i-1) / rho(i))^(gamma-1);
% Compute the pressure at the next point along the characteristic
p(i) = p(i-1) + rho(i-1) * (u(i-1) - u(i)) * dx;
end
2 commentaires
Plus de réponses (1)
Image Analyst
le 31 Déc 2022
Did you search for the error? We get asked this several times per week so you would have found it. It's a FAQ:
0 commentaires
Voir également
Catégories
En savoir plus sur Engines & Motors dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!