MATLAB Answers

Index position 2 exceeds array bounds

2 views (last 30 days)
Kali Sanders
Kali Sanders on 19 Feb 2020
Answered: Hiro on 19 Feb 2020
I am trying to run my code and I keep getting this error:
Index in position 2 exceeds array bounds.
Error in exp01_matlab (line 26)
x = A(:,1)*in2cm
Attached my code to open in matlab and I also attached my file I am using for my data
Here is my code:
clc
clear all
close all
format long
%
% Specify constants
g = 9.81; % gravity (m/s2)
mu_w = 0.001002; % water dynamic viscosity (N-s/m2)
rho_w = 998.0; % water density (kg/m3)
D_t = 0.6225*0.0254; % throat diameter (m) (based on station No.)
%
% Conversion factors
in2cm = 2.54; % inches to centimeters
in2m = 0.0254; % inches to meters
lb2newton = 4.44822; % pounds to newtons
% ---------------------------------------------------------------------------
% Import rawdata from excel
filename = 'Lab1dataSheet_Venturi_Even.xlsx'; % specify file name/directory
hRange = 'A1:I11'; % specify range of pressure data
tRange = 'K1:X5'; % specify range of flowrate data
[A,~,~] = xlsread(filename,3,hRange); % import presssure rawdata
[B,~,~] = xlsread(filename,3,tRange); % import flowrate data
%
% Sort rawdata into separate data arrays
x = A(:,1)*in2cm; THIS IS WHERE MY ERROR IS!!!!!
D = A(:,2)*in2m;
h1 = A(:,3)*in2cm;
h2 = A(:,4)*in2cm;
h3 = A(:,5)*in2cm;
h4 = A(:,6)*in2cm;
h5 = A(:,7)*in2cm;
h6 = A(:,8)*in2cm;
h1_act = h1 - h1(4);
h2_act = h2 - h2(4);
h3_act = h3 - h3(4);
h4_act = h4 - h4(4);
h5_act = h5 - h5(4);
h6_act = h6 - h6(4);
% Sort time rawdata into separate data arrays
W = B(:,2)*lb2newton;; % water weight (lbs)
t1 = B(:,3); % flow rate time in run#1 (seconds)
t2 = B(:,5); % flow rate time in run#2 (seconds)
t3 = B(:,7); % flow rate time in run#3 (seconds)
t4 = B(:,9); % flow rate time in run#4 (seconds)
t5 = B(:,11); % flow rate time in run#5 (seconds)
t6 = B(:,13); % flow rate time in run#6 (seconds)
% ----------------------------------------------------------------------------
%
% DATA PROCESSING
% ---------------------------------------------------------------------------
%
% QUESTION No. 01 - PRESSURE DISTRIBUTION
% Theoretical and actual volumetric flowrate
% Actual volumetric flowrate
V = W/(rho_w*g); % compute volume of water
a1=polyfit(t1,V,1); % fit a linear curve through data w/ polyfit
Q_act1 = a1(1); % set the slope of the curve equal to the flowrate
a2=polyfit(t2,V,1); % fit a linear curve through data w/ polyfit
Q_act2 = a2(1); % set the slope of the curve equal to the flowrate
a3=polyfit(t3,V,1); % fit a linear curve through data w/ polyfit
Q_act3 = a3(1); % set the slope of the curve equal to the flowrate
a4=polyfit(t4,V,1); % fit a linear curve through data w/ polyfit
Q_act4 = a4(1); % set the slope of the curve equal to the flowrate
a5=polyfit(t5,V,1); % fit a linear curve through data w/ polyfit
Q_act5 = a5(1); % set the slope of the curve equal to the flowrate
a6=polyfit(t6,V,1); % fit a linear curve through data w/ polyfit
Q_act6 = a6(1); % set the slope of the curve equal to the flowrate
%
% Theoretical and actual pressure distibution
Q_theo1 = 0.25*pi*D(1)^2.*D_t^2.*sqrt(0.02*g*(h1(1)-h1(4))/(D(1)^4.-D_t^4.)); %run #1
Q_theo2 = 0.25*pi*D(1)^2.*D_t^2.*sqrt(0.02*g*(h2(1)-h2(4))/(D(1)^4.-D_t^4.)); %run #2
Q_theo3 = 0.25*pi*D(1)^2.*D_t^2.*sqrt(0.02*g*(h3(1)-h3(4))/(D(1)^4.-D_t^4.)); %run #3
Q_theo4 = 0.25*pi*D(1)^2.*D_t^2.*sqrt(0.02*g*(h4(1)-h4(4))/(D(1)^4.-D_t^4.)); %run #4
Q_theo5 = 0.25*pi*D(1)^2.*D_t^2.*sqrt(0.02*g*(h5(1)-h5(4))/(D(1)^4.-D_t^4.)); %run #5
Q_theo6 = 0.25*pi*D(1)^2.*D_t^2.*sqrt(0.02*g*(h6(1)-h6(4))/(D(1)^4.-D_t^4.)); %run #6
%With matlab for loop to calculate every theoretical height at every piezometer location (total )
for j=1:11
h1_theo(j) = (8.*Q_theo1^2.)/(pi^2.*g)*(1./D_t^4. - 1./D(j)^4.)*100.; % (cm) run #1
h2_theo(j) = (8.*Q_theo2^2.)/(pi^2.*g)*(1./D_t^4. - 1./D(j)^4.)*100.; % (cm) run #2
h3_theo(j) = (8.*Q_theo3^2.)/(pi^2.*g)*(1./D_t^4. - 1./D(j)^4.)*100.; % (cm) run #3
h4_theo(j) = (8.*Q_theo4^2.)/(pi^2.*g)*(1./D_t^4. - 1./D(j)^4.)*100.; % (cm) run #4
h5_theo(j) = (8.*Q_theo5^2.)/(pi^2.*g)*(1./D_t^4. - 1./D(j)^4.)*100.; % (cm) run #5
h6_theo(j) = (8.*Q_theo6^2.)/(pi^2.*g)*(1./D_t^4. - 1./D(j)^4.)*100.; % (cm) run #6
end
%
% QUESTION No. 02 - DISCHARGE COEFFICIENT
% Discharge coefficient and Reynolds number
Rd=zeros(6,1) %initialize vector for Roynold number list
Cd=zeros(6,1) %initialize vector for discharge coefficient list
Cd(1) = Q_act1/Q_theo1; % discharge coefficient
Re(1) = 4.*rho_w*Q_act1/(pi*mu_w*D_t); % reynolds number
Cd(2) = Q_act2/Q_theo2; % discharge coefficient
Re(2) = 4.*rho_w*Q_act2/(pi*mu_w*D_t); % reynolds number
Cd(3) = Q_act3/Q_theo3; % discharge coefficient
Re(3) = 4.*rho_w*Q_act3/(pi*mu_w*D_t); % reynolds number
Cd(4) = Q_act4/Q_theo4; % discharge coefficient
Re(4) = 4.*rho_w*Q_act4/(pi*mu_w*D_t); % reynolds number
Cd(5) = Q_act5/Q_theo5; % discharge coefficient
Re(5) = 4.*rho_w*Q_act5/(pi*mu_w*D_t); % reynolds number
Cd(6) = Q_act6/Q_theo6; % discharge coefficient
Re(6) = 4.*rho_w*Q_act6/(pi*mu_w*D_t); % reynolds number
%
% %
% QUESTION No. 03 - HEAD LOSS
% Head loss
H_T (1) = h1(1) - h1(11);
H_T (2) = h2(1) - h2(11);
H_T (3) = h3(1) - h3(11);
H_T (4) = h4(1) - h4(11);
H_T (5) = h5(1) - h5(11);
H_T (6) = h6(1) - h6(11);
Q_act = [Q_act1,Q_act2,Q_act3,Q_act4,Q_act5,Q_act6]
% %
% ----------------------------------------------------------------------------
%
% OUTPUT PLOTS FOR DATA REDUCTION
% ---------------------------------------------------------------------------
%
% QUESTION No. 01 - PRESSURE DISTRIBUTION
%plot actual pressure vs. x location of piezometers.
figure(1);
plot(x,h1_act,'ro', x,h2_act,'yo', x,h3_act,'bo', x,h4_act,'co', x,h5_act,'mo', x,h6_act,'ko');
hold on
%plot theoretical pressure vs. x location of piezometers.
plot(x,h1_theo,'r--', x,h2_theo,'y--', x,h3_theo,'b--', x,h4_theo,'c--', x,h5_theo,'m--', x,h6_theo,'k--');
hold off
xlabel('Axial position (cm)')
ylabel('Pressure head (cm of H_2O)')
legend('10 in H_2O','8 in H_2O','6 in H_2O', '4 in H_2O', '2 in H_2O','0.6 in H_2O');
%
% QUESTION No. 02 - DISCHARGE COEFFICIENT
figure(2);
plot(Re/10000,Cd,'bo-')
xlabel('Reynolds number \times10^4')
ylabel('Discharge coefficient') % %
% %
% % QUESTION No. 03 - HEAD LOSS
%QUESTION No. 03 - HEAD LOSS
figure(3);
plot(Q_act*10000,H_T,'bo-')
xlabel('Volumetric flow-rate \times10^2 (cm^3/s)')
ylabel('Head loss (cm of H_2O)')
% ----------------------------------------------------------------------------
%
% END OF FILE
% ----------------------------------------------------

Accepted Answer

Hiro
Hiro on 19 Feb 2020
I had a look at it quickly - you were obviously failing in importing the pressure and the flowrate.
xlsread(filename, 3, hRange)
there are just images on the 3rd sheet; there are no numbers to be imported.
You should change to either, I guess, 1 or 2, which I've tried.
However you are still having a problem which is associated with the matrix B.
I guesss the way you specified the hRange, and tRange was not correct.
You should modify them appropriately.

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!