Array indices must be positive integers or logical values error
Show older comments
clear all; clc;
timestart=0;
timeEnd=600;
timespan=[timestart timeEnd]; % Range for the independent variable time
y0=[0.67 0.1 0.01 0.71 1 1 1]; % assumed initial values for the compositions which are dependant variables
v=0.6; % superficial gas phase velocity in m/s
e= 0.5; % fixed bed void fraction
d=6000 ;% density of solid OSM in kg/m^3
P= 1 ;% pressure in atm
T=1073; % temperature in kelvin
R= 0.08206; % gas constant in L.atm/K.mol
M=67 ;% molar mass of solid OSM in g/mol
z2= 2 ;% lenght of the reactor in meters
z1=1;
z0=0;
[t y]=ode45(@(t,y)rxn(t,y,v,e,d,P,T,R,M,z2,z1),timespan,y0);
plot(t,y(:,1),t,y(:,2));
legend('Xa','Xb');
ylabel('species a');
xlabel('time (s)');
axis([0 600 0 1]);
title('species a with time')
function f = rxn(t,Y,v,e,d,P,T,R,M,z2,z1,z0)
Xa=Y(1);
Xb=Y(2);
Xc=Y(3);
Xd=Y(4);
Xe=Y(5);
Xf=Y(6);
Xg=Y(7);
% explicit equations
Ct= P/(R*T); % total gas concentration
gc=(d*(1-e))/(Ct*e);
% rate laws (assumptions used)
ra= 20;
rb=20;
rc=20;
rd=20;
re=20;
rf=200;
rg=20;
dXadt= (-v/e)*((Xa(1)-Xa(0))/(z1-z0))+gc*ra % equation for gas components A
dXadt= (-v/e)*((Xa(2)-Xa(1))/(z2-z1))+gc*ra
dXbdt= (-v/e)*((Xb(1)-Xb(0))/(z1-z0))+gc*rb % equation for gas components B
dXbdt= (-v/e)*((Xb(2)-Xb(1))/(z2-z1))+gc*rb
dXcdt= (-v/e)*((Xc(1)-Xc(0))/(z1-z0))+gc*rc % equation for gas components C
dXcdt= (-v/e)*((Xc(2)-Xc(1))/(z2-z1))+gc*rc
dXddt= (-v/e)*((Xd(1)-Xd(0))/(z1-z0))+gc*rd % equation for gas components D
dXddt= (-v/e)*((Xd(2)-Xd(1))/(z2-z1))+gc*rd
dXedt= M*re % equation for solid components E
dXfdt= M*rf % equation for solid components F
dXgdt= M*rg % equation for solid components G
f=[dXadt; dXbdt; dXcdt; dXddt; dXedt, dXfdt; dXgdt]
end
I'm getting the following error and I don't understand why
Array indices must be positive integers or logical values.
Error in rxn (line 45)
dXadt= (-v/e)*((Xa(1)-Xa(0))/(z1-z0))+gc*ra % equation for gas components A
Answers (1)
The error is clear....in MATLAB the indices of an array cannot be negative, zero and fractions.
Example:
A = rand(10,1) ;
A(1)
A(3)
A(-) % error, as index is negative
A(0) % error, as index is zero
A(0.4) % error as index in a fraction
So this line:
dXadt= (-v/e)*((Xa(1)-Xa(0))/(z1-z0))+gc*ra
has index as zero in Xa. So you have to change it.
4 Comments
Maisha Khan
on 30 Oct 2020
KSSV
on 30 Oct 2020
Yes..you cannot use tha too..your Xa is a scalar quantity..it has only one value...and you are trying to get Xa(2)..so error.
It should be either (Xa-Ab) or (Xa-Xe) you have not used Xe, Xf, Xg..you left them like that..
Maisha Khan
on 30 Oct 2020
Maisha Khan
on 30 Oct 2020
Categories
Find more on Gas Library in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!