Index exceeds the number of array elements (4).

1 view (last 30 days)
function [therm,chem,r_c,mfuel,mC02,ddt] = FourStrokeCycle(D,d,rpm,fuel,N)
AirMatrix= readmatrix('AirProperties.xlsx');
pa=101325
Ta=273
sa=3796
cpi= AirMatrix(3,:)
Mi= AirMatrix(2,:)
nin= AirMatrix(1,:)
FuelMatrix= readmatrix('FuelProperties.xlsx');
ReactionMatrix= readmatrix('ReactionRatios.xlsx');
fuel= input("indicate fuel type:",'s');
while fuel ~= "gasoline" & fuel~="Gasoline" & fuel~="diesel" & fuel~="Diesel"
fuel= input("Unrecognized entry, indicate fuel type:",'s');
end
switch fuel
case {"gasoline","Gasoline"}
Mf= FuelMatrix(1,1)
rhof= FuelMatrix(1,2)
Tai= FuelMatrix(1,3)
Hf= FuelMatrix(1,4)
Si= ReactionMatrix(1,:)
case {"diesel","Diesel"}
Mf= FuelMatrix(2,1)
rhof= FuelMatrix(2,2)
Tai= FuelMatrix(2,3)
Hf= FuelMatrix(2,4)
Si= ReactionMatrix(2,:)
end
%test inputs
rpm=1500
d=.02
N=6
V0=0.00006
L=0.1
D=0.02
PhiI=pi/6
PhiE=pi/6
omega= rpm* 0.10472
% convert rpm to omega rad/s
k=[1:1:N+1]
PhiK=(k-1)*(4*pi)/N
tK=(k-1)*(4*pi)/(N*omega)
PhiT= omega.*tK
x=(D/2)*cos(PhiT)+sqrt(((4*L^2)/D^2)-sin(PhiT).^2)
Vt= V0+(pi.*d^2)./4.*(L+(D./2)-x)
r_c= (V0+D*(pi*d^2)/4)/V0
therm = zeros(8,N+1)
P=zeros(1,N+1)
T=zeros(1,N+1)
P(1)=101325
Ru=8.3145
Mbar=Mi*nin(1)/nin
R=Ru/Mbar
m=nin.*Mi
mu=((nin.*Mi)./m)
n(1)=(P(1)*Vt(1))/(R(1)*T(1))
v(1)=Vt(1)/m(1)
c_p=mu(1)*cpi
c_v=c_p-R
for k=1:N
if PhiK(k)< pi
P(k+1)=P(k)
T(k+1)=T(k)
mu(k+1)=mu(k)
nin(k+1)=nin(k)
n(k+1)=n(k)*Vt(k+1)/Vt(k)
m(k+1)=m(k)*Vt(k+1)/Vt(k)
v(k+1)=Vt(k+1)/m(k+1)
elseif PhiK(k)<2*pi- PhiI
nin(k+1)=nin(k)
mu(k+1)=mu(k)
n(k+1)=n(k)
m(k+1)=m(k)
v(k+1)=Vt(k+1)/m(k+1)
P(k+1)=P(k)*(v(k)/v(k+1))^(c_p/c_v)
T(k+1)=(P(k+1)*v(k+1))/R
elseif PhiK(k)>=2*pi-PhiI
if n(k+1)>=0 %ERROR LINE
CF=(1+10*(rpm-800)/5200)*(r_c-6.854)
mdot=CF/1000
ndot=mdot/M
nin(k+1)=nin(k)+ndot*Si*(tK(k+1)-tK(k))
Index exceeds the number of array elements (4).
Error in test (line 113)
if n(k+1)>=0
  1 Comment
Walter Roberson
Walter Roberson on 2 May 2023
We do not have your data and do not know the answers to the input() statements, so we cannot test your code. Also, you stopped posting in the middle of a if elseif chain, so the code is not complete so we cannot invent our own data to test with.

Sign in to comment.

Answers (1)

Gokul Nath S J
Gokul Nath S J on 22 May 2023
Hi Carig,
It seems that your code is throwing out an error in the form of incorrect inputs. From the limited information, it seems obvious that the index values of n(k+1) is exeeding the limit it can intake. Further note that variable n can only take 4 array elements and you are accessing a value which is the 5th one. This might be a logical error that would have taken place. Please check for the condition of end of the array by using the length function and input the variable once the verification is passed. A reduced code will looking something like this.
lengthn = length(n);
if k + 1 < n
if n(k+1) >= 0
% code
end
end
with regards,
Gokul Nath S

Tags

Community Treasure Hunt

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

Start Hunting!