Array indices must be positive integers or logical values error

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

I understand now! But once I fix that, I'm getting an error of
Index exceeds the number of array elements (1).
Error in rxn (line 45)
dXadt= (-v/e)*((Xa(2)-Xa(1))/(z1-z0))+gc*ra % equation for gas components A
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..
okay understood, I am trying to solve the differential equation but don't have all the initial values so I was getting confused with how to incorporate it,
thanks a lot!!
I actually have a follow up question if you can help. Over here I'm trying to solve a differential equation and in the following equation I don't have values of Xb at point z0,z1 & z2 which is what I'm trying to solve. How can I do that as currently I only have it as an array?
dXbdt= (-v/e)*((Xb(2)-Xb(1))/(z1-z0))+gc*rb
dXbdt= (-v/e)*((Xb(3)-Xb(2))/(z2-z1))+gc*rb

Sign in to comment.

Categories

Tags

Asked:

on 30 Oct 2020

Commented:

on 30 Oct 2020

Community Treasure Hunt

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

Start Hunting!