# Plotting 2D graph error: Index exceeds

1 view (last 30 days)
Lujain Almarhabi on 31 Jan 2021
Commented: darova on 31 Jan 2021
Hello,
I have the following code with some simple calculations, I am trying to plot eventually two plots: 1) Cpm/Rm VS T and 2) a2/a2ref1 (& a2/a2ref2 at same plot) VS T
I tried the follolwing for one plot and I got the error:
Index exceeds the number of array elements (1).
I know it's a simple question but would appreciate the help.
Code:
T = (300:100:2500);
%Set Constants
R = 8314.3; %universal gas const, R hat with units, J/mol*K
Mo2 = 31.99; %molecular weight/mass O2, in g/mol
Mn2 = 28.014; %molecular weight/mass N2, in g/mol
g0 = 3; %degenercy factor in the electronic excitation term
g1 = 2;
theta1 = 11390;
thetao2 = 2270; %vibration parameter for O2, in Kelvin
thetan2 = 3390; %vibration parameter for N2
Ro2 = R/Mo2; %R for each species, O2
Rn2 = R/Mn2;
Rm = (0.21*Ro2)+(0.79*Rn2); %Universal gas constant for mixture N2, O2
Cvn2 = (2.5)*Rn2 + Rn2*((thetan2./2*T)./sinh(thetan2./2*T)).^2; %Cv for N2, neglecting the electronic excitation
Cvo2 = (2.5)*Ro2 + Ro2*((thetao2./2*T)./sinh(thetao2./2*T)).^2 + Ro2*((g1*theta1*exp(-theta1./T))/(g0+g1*exp(-theta1./T))); %Cv for O2, include the electronic excitation
Cpn2 = Cvn2 + Rn2; %Cp for N2
Cpo2 = Cvo2 + Ro2; %Cp for O2
%Specific heats for the mixture
Cvm = (0.21)*(Cvo2) + (0.79)*(Cvn2);
Cpm = (0.21)*(Cpo2) + (0.79)*(Cpn2);
%Sound-speed
a2 = (Cpm/Cvm)*Rm*T;
a2ref1 = (1.4)*Rn2*T;
a2ref2 = (1.4)*Ro2*T;
%Plot a2/a2ref VS T
%Plot Cp/R VS T for mixture (Cpm/Rm VS T)
for i = 1:length(T)
C(i) = ((0.21)*(Cpo2(i)) + (0.79)*(Cpn2(i)))./((0.2*Ro2(i))+(0.79*Rn2(i)));
end
figure;
plot(T,C(T), 'r');
Thanks

KSSV on 31 Jan 2021
Error is simple.....the variables Ro2, Rn2 are scalars i.e. they are constants and they have sinlge value. In the line:
C(i) = ((0.21)*(Cpo2(i)) + (0.79)*(Cpn2(i)))./((0.2*Ro2(i))+(0.79*Rn2(i)));
You tried accessing Ro2, Rn2 as an array. So, when loop takes i = 2, Ro2(2) do not exist, thus error pops. You need to think of your code.
##### 2 CommentsShowHide 1 older comment
darova on 31 Jan 2021