How do I fix "Index in position 2 exceeds array bounds. Index must not exceed 1." as error?

조회 수: 27 (최근 30일)
Hi! My task is to complete the code to calculate the power of a turbine using certain conditions. I also have to write the code to trace the figure of the power according to the temperature at the beginning of the turbine.
My error is due to the conditions in this table, which i can't seem to fix.
No d’essai 1 T (°C) 2 p (kPa) Titre 2 x
1 262 35 0.78
2 293 40 0.82
3 301 52 0.85
4 325 76 0.88
5 336 118 0.94
6 358 122 0.96
7 410 132 0.99
Here is the error:
Index in position 2 exceeds array bounds. Index must not
exceed 1.
Error in untitled3 (line 23)
p_i = data(i,2); % pression en kPa
Here is the code:
% Importer les données des essais
data = importdata('Conditions.txt');
data = data';
% Importer les tables de propriétés de l'eau
t3 = importdata('TableT3.txt');
t4 = importdata('TableT4_4MPa.txt');
% Données constantes pour tous les essais
p1 = 4e6; % pression à l'entrée de la turbine
d2 = 0.5; % diamètre de la sortie de la turbine
mdot = 13.80; % débit massique
% Initialisation des vecteurs de puissance et de température
P = zeros(size(data,1),1);
T = zeros(size(data,1),1);
% Boucle pour chaque essai
for i = 1:size(data,1)
% Extraire les données de l'essai i
T_i = data(i,1); % température
p_i = data(i,2); % pression en kPa
x_i = data(i,3); % titre
% Trouver l'enthalpie spécifique de l'eau à l'état 1 (vapeur saturée)
h1 = interp1(t4(:,1),t4(:,4),p1);
% Trouver l'enthalpie spécifique de l'eau à l'état 2 (mélange vapeur-eau saturée)
h2 = x_i*interp2(t3(:,1),t3(1,:),t3(:,2:end),p_i,T_i,'spline') + (1-x_i)*interp1(t4(:,1),t4(:,4),p_i);
% Calcul de la puissance
v2 = (mdot/(pi*(d2^2)/4))*(1-x_i)*sqrt(2*(h1-h2)); % vitesse à la sortie
h2s = h2 + (v2^2)/2; % enthalpie spécifique à la sortie
P_i = mdot*(h1 - h2s); % puissance développée
% Ajouter la puissance et la température à leurs vecteurs respectifs
P(i) = P_i/1000000; % convertir en MW
T(i) = T_i;
end
% Tracer la courbe de puissance en fonction de la température
plot(T,P,'o-')
xlabel('Température (°C)')
ylabel('Puissance (MW)')
title('Puissance de la turbine en fonction de la température')
grid on

채택된 답변

the cyclist
the cyclist 2023년 3월 12일
Notice that when you used import data, it came is as a structure. So, you would need to access data.data.
data = importdata('Conditions.txt');
data
data = struct with fields:
data: [7×3 double] textdata: {'% T1' 'p2 (kPa)' 'x2'} colheaders: {'% T1' 'p2 (kPa)' 'x2'}
I think it would be easier to use readmatrix:
data = readmatrix('Conditions.txt')
data = 7×3
262.0000 35.0000 0.7800 293.0000 40.0000 0.8200 301.0000 52.0000 0.8500 325.0000 76.0000 0.8800 336.0000 118.0000 0.9400 358.0000 122.0000 0.9600 410.0000 132.0000 0.9900
i = 1; % Just using the first row
p_i = data(i,2) % % pression en kPa
p_i = 35
It's unclear to me why you did the transpose
data = data';
I'm pretty sure you shouldn't do that.
  댓글 수: 4
the cyclist
the cyclist 2023년 3월 12일
Your code is extremely confusing:
  • you read in the file Conditions.txt multiple times
  • the second time you read it, you use fscanf (why??), but get an empty char array
  • you are still using importdata instead of readmatrix, so I expect t3 and t4 and not the matrices you expect
So, T and P end up being zero, which is why you get the plot you get.
I haven't been able to figure out how to fix this.
% Importer les données des essais
data = readmatrix('Conditions.txt');
i = 1; % Just using the first row
p_i = data(i,2); % % pression en kPa
% Importer les tables de propriétés de l'eau
t3 = importdata('TableT3.txt');
t4 = importdata('TableT4_4MPa.txt');
% Données constantes pour tous les essais
p1 = 4e6; % pression à l'entrée de la turbine
d2 = 0.5; % diamètre de la sortie de la turbine
mdot = 13.80; % débit massique
% Initialisation des vecteurs de puissance et de température
P = zeros(size(data,1),1);
T = zeros(size(data,1),1);
% Ouvrir le fichier texte pour lecture
fid = fopen('Conditions.txt','r');
% Lire les données dans une matrice appelée data
data = fscanf(fid,'%3 %7',[2 Inf]); % les données sont stockées en colonnes
% Fermer le fichier
fclose(fid);
% Transposer la matrice pour avoir les données stockées en rangées
data = data'
data = 0×0 empty char array
% Boucle pour chaque essai
for i = 1:size(data,1)
% Extraire les données de l'essai i
T_i = data(i,2); % température
p_i = data(i,3)*1000; % pression en Pa
x_i = data(i,4); % titre
% Trouver l'enthalpie spécifique de l'eau à l'état 1 (vapeur saturée)
h1 = interp1(t4(:,1),t4(:,4),p1);
% Trouver l'enthalpie spécifique de l'eau à l'état 2 (mélange vapeur-eau saturée)
h2 = x_i*interp2(t3(:,1),t3(1,:),t3(:,2:end),p_i,T_i,'spline') + (1-x_i)*interp1(t4(:,1),t4(:,4),p_i);
% Calcul de la puissance
v2 = (mdot/(pi*(d2^2)/4))*(1-x_i)*sqrt(2*(h1-h2)); % vitesse à la sortie
h2s = h2 + (v2^2)/2; % enthalpie spécifique à la sortie
P_i = mdot*(h1 - h2s); % puissance développée
% Ajouter la puissance et la température à leurs vecteurs respectifs
P(i) = P_i/1000000; % convertir en MW
T(i) = T_i;
end
T
T = 7×1
0 0 0 0 0 0 0
P
P = 7×1
0 0 0 0 0 0 0
% Tracer la courbe de puissance en fonction de la température
plot(T,P,'o-')
xlabel('Température (°C)')
ylabel('Puissance (MW)')
title('Puissance de la turbine en fonction de la température')
grid on

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

the cyclist
the cyclist 2023년 3월 12일
편집: the cyclist 2023년 3월 12일
It would be easier for us to help you debug this if you upload the data. You can use the paper clip icon in the INSERT section of the toolbar.
The problem is probably the importdata is not bringing in your data as you expect, and it is probably somehow an Nx1 vector instead of an Nx3 array. You could try using readtable or readmatrix instead, but it is not possible to know for sure without seeing the input file.

카테고리

Help CenterFile Exchange에서 Combustion and Turbomachinery에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by