Index exceeds the number of array elements (1).

Hi community Matlab, My programme is generating the following error,
if true
function dxdt = semibrx(t,x,k,Kc,V0,v0,Cb0)
% x(1)= Ca, x(2) = Cb, x(3) = Cc, x(4) = Cd;
rA = -k*(x(1)*x(2)-(1/Kc)*x(3)*x(4));
V = V0+v0*t;
dxdt = [
rA-(v0/V)*x(1);
rA + (v0/V)*(Cb0-x(2));
-rA-(v0/V)*x(3);
-rA-(v0/V)*x(4)
]
end
if true
clear all;
k = 9e-5; v0= 0.05 ; V0 = 200; Cb0 = 10.93; Ca0= 7.72;
Kc = 1.08;
x0 = [Ca0 0 0 0 ]; tspan = [0 1e2];
[t x] = ode45(@equilibrio,tspan,Kc,x0,[],k,v0,V0,Cb0);
Ca = x(:,1);
Cb = x(:,2);
Cc = x(:,3);
Cd = x(:,4) ;
V = V0 + v0*t; Xa = (Ca0*V0-Ca.*V)/(Ca0*V0); rA = -k*(x(1)*x(2)-(1/Kc)*(x(3)*x(4)));
%subplot(1,2),
plot(t,Ca,t,Cb,':',t,Cc,'.-',t,Cd,'--');
xlabel('t(s)'),ylabel('Concentration(mol/dm^3)'), legend ('C_A','C_B','C_C','C_D')
%subplot(1,2),
%plot(t,rA), xlabel('t(s)'),ylabel ('Reaction rate (mol/dm^3s)')
%plot(t,Xa),xlabel('t(s)'),ylabel('Conversão')
end
end
The program shows error: Index exceeds the number of array elements(1).
Would you help me please?
Thanks,
Guilherme Lopes de Campos

5 comentarios

Guillaume
Guillaume el 11 de Nov. de 2018
When you report an error always give us the full text of the error message, everything in red. In particular, we're missing the line that causes the error.
In any case, the error message is clear, you're trying to index something that has only one element with an index greater than 1. Check the size of that something and make sure that it's big enough for what you want. I suspect the something is x.
If you remove the
if true
and put function declerations in the end of the script, your code will be more readable.
what line of code does your error come from?
if true
Index exceeds the number of array elements (1).
Error in equilibrio (line 3) rA = -k*(x(1)*x(2)-(1/Kc)*x(3)*x(4));
Error in odearguments (line 90) f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115) odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in equilibrio2 (line 5) [t x] = ode45(@equilibrio,tspan,Kc,x0,[],k,v0,V0,Cb0);
end
madhan ravi
madhan ravi el 11 de Nov. de 2018
See my answer below
Thank you very much Community, for help!

Iniciar sesión para comentar.

 Respuesta aceptada

madhan ravi
madhan ravi el 11 de Nov. de 2018
Editada: madhan ravi el 11 de Nov. de 2018
clear all;
Ca0= 7.72;
x0 = [Ca0 0 0 0 ]; tspan = [0 1e2];
[t x] = ode45(@equilibrio,tspan,x0); %FUNCTION CALLING
Ca = x(:,1);
Cb = x(:,2);
Cc = x(:,3);
Cd = x(:,4) ;
figure
plot(t,Ca)
xlabel('t(s)')
ylabel('Concentration(mol/dm^3)')
legend ('C_A')
figure
plot(t,Cb,':')
xlabel('t(s)')
ylabel('Concentration(mol/dm^3)')
legend('C_B')
figure
plot(t,Cc,'.-')
xlabel('t(s)')
ylabel('Concentration(mol/dm^3)')
legend ('C_C')
figure
plot(t,Cd,'--')
xlabel('t(s)')
ylabel('Concentration(mol/dm^3)')
legend ('C_D')
function dxdt = equilibrio(t,x)
k = 9e-5; v0= 0.05 ; V0 = 200; Cb0 = 10.93;
Kc = 1.08;rA = -k*(x(1)*x(2)-(1/Kc)*x(3)*x(4));
V = V0+v0*t;
dxdt = [rA-(v0/V)*x(1);
rA + (v0/V)*(Cb0-x(2));
-rA-(v0/V)*x(3);
-rA-(v0/V)*x(4)]
end

2 comentarios

Thank you very much Madhan!
madhan ravi
madhan ravi el 11 de Nov. de 2018
Anytime :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by