MATLAB Answers

Index exceeds number of array elements (1)?

2 views (last 30 days)
Jorge Luis Valle Verduzco
Answered: Chidvi Modala on 7 Jun 2021
I got Index exceeds the number of array elements (1), and error in ph3 (line 90) [t, xsim] = ode45(@(t,x) g(t,x), [0 100], X_0);
Please help me :(
%Define experimental conditions
pH = 3;
I = 30; %Current density mA
F = 96500; %Faraday constant
Vwork = 12; %Work potential of the cell Volts
V = 365; %Volume mL
A = 57.17; %Surface area cm2
km = 1.2; %Mass transfer coefficient
kj = 0.9; %
Sred = 0; %0
%Define initial conditions
initial_paraquat = 10; %mg/L
%pH < 3
%Define stochiometry coefficients for each chemical reaction
%OC/TPi/TPi'/TPj/TPj'/CO2/M(OH)/M(HClO)/Cl2
psi = [0 0 0 0 0 0 1 0 0;
0 0 0 0 0 0 1 1 0;
0 0 0 0 0 0 0 1 1;
1 1 0 0 0 0 0 0 1;
1 0 1 0 0 0 0 0 1;
0 1 0 1 0 0 0 0 1;
0 0 1 0 1 0 0 0 1;
0 0 0 1 0 1 0 0 1;
0 0 0 0 1 1 0 0 1;
0 0 1 0 0 1 0 0 1;
0 0 0 0 0 0 1 0 0];
%Transpose psi matrix
psi = transpose(psi);
%Kinetic rate coefficients (base zone)
k = [0;
0;
0;
0;
1;
0;
1;
0;
1;
1;
0];
%Potential coffecients
V_reactions = [1;
1;
1;
1;
NaN;
1;
NaN;
1;
NaN;
NaN;
1];
%Computation of electrons proportions
DiffWork = Vwork - V_reactions;
TotalWork = sum(Vwork - V_reactions, 'omitnan');
alpha = DiffWork/TotalWork;
%Initial conditions [anode; cathode; base]
X_0 = [0; 0; initial_paraquat; %Paraquat
0; 0; 0; %TPi
0; 0; 0; %TPi'
0; 0; 0; %TPj
0; 0; 0; %TPj'
0; 0; 0; %CO2
0; 0; 0; %M(OH)
0; 0; 0; %M(HClO)
0; 0; 0]; %Cl2
%Define function that models the system of ODEs
syms t x
g= @(t,x) [A/V*(km(x(3) - x(1)) + psi(1,4)*I/F*alpha(4) + kj*(x(25)*x(1))); A/V*(km*(x(3) - x(2)) + kj*(Sred*x(2))); A*(km*(x(1) - x(3)) + km*(x(2) - x(3)) + psi(1,5)*k(5)*x(27)*x(3)); %Paraquat
A/V*(km(x(6) - x(4)) + psi(2,4)*I/F*alpha(4) + psi(2,6)*I/F*alpha(6) + kj*(x(25)*x(4))); A/V*(km*(x(6) - x(5)) + kj*(Sred*x(5))); A*(km*(x(4) - x(6)) + km*(x(5) - x(6))); %TPi
A/V*(km(x(9) - x(7)) + kj*(x(25)*x(7))); A*(km*(x(9) - x(8)) + kj*(Sred*x(8))); A/V*(km*(x(7) - x(9)) + km*(x(8) - x(9)) + psi(3,5)*k(5)*x(27)*x(3) + psi(3,7)*k(7)*x(27)*x(9) + psi(3,10)*k(10)*x(27)*x(9)); %TPi'
A/V*(km(x(12) - x(10)) + psi(4,6)*I/F*alpha(6) + psi(4,8)*I/F*alpha(8) + kj*(x(25)*x(10))); A/V*(km*(x(12) - x(11)) + kj*(Sred*x(11))); A*(km*(x(10) - x(12)) + km*(x(11) - x(12))); %TPj
A/V*(km(x(15) - x(13)) + kj*(x(25)*x(13))); A*(km*(x(15) - x(14)) + kj*(Sred*x(14))); A/V*(km*(x(13) - x(15)) + km*(x(14) - x(15)) + psi(5,7)*k(7)*x(27)*x(9) + psi(5,9)*k(9)*x(27)*x(15)); %TPj'
A/V*(km(x(18)-x(16)) + psi(6,8)*I/F*alpha(8) + kj*(x(25)*x(16))); A/V*(km*(x(18) - x(17)) + kj*(Sred*x(17))); A*(km*(x(16) - x(18)) + km*(x(17) - x(18)) + psi(6,9)*k(9)*x(27)*x(15) + psi(6,10)*k(10)*x(27)*x(9)); %CO2
A/V*(psi(7,1)*I/F*alpha(1) + psi(7,2)*I/F*alpha(2) + psi(7,11)*I/F*alpha(11) + kj*(x(25)*x(19)));0;0; %M(OH)
A/V*(psi(8,2)*I/F*alpha(2) + psi(8,3)*I/F*alpha(3) + kj*(x(25)*x(22)));0;0; %M(HClO)
A/V*(km(x(27)-x(25)) + psi(9,3)*I/F*alpha(3) + psi(9,4)*I/F*alpha(4) + psi(9,6)*I/F*alpha(6) + psi(9,8)*I/F*alpha(8)); A*(km*(x(27) - x(26))); A*(km*(x(25) - x(27)) + km*(x(26) - x(27)) + psi(9,5)*k(5)*x(27)*x(3) + psi(9,7)*k(7)*x(27)*x(9)) + psi(9,9)*k(9)*x(27)*x(15) + psi(9,10)*k(10)*x(27)*x(9)]; %Cloro
[t, xsim] = ode45(@(t,x) g(t,x), [0 100], X_0);
figure
plot(t, xsim(:,1))
  1 Comment
Daniel Pollard
Daniel Pollard on 4 Jun 2021
I suspect that it's trying to solve the ODE for some values of x, and you calling x(3) (for example) in the function makes it confused.
What does the entire error message say?

Sign in to comment.

Answers (1)

Chidvi Modala
Chidvi Modala on 7 Jun 2021
When defining g, you are trying to use km(x(3) - x(1)). Here MATLAB assumes '()' is used for accessing elements in an array. You might need to replace km(x(3) - x(1)) with km*(x(3) - x(1)) wherever applicable.

Community Treasure Hunt

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

Start Hunting!