How to solve 'Index number exceeds number of array elements(1)'

function substrate_glucose()
% Effect of carbon sources on the growth of Lactoccus lactis. Ibrahim et. al (2010)
% Parameters
u = 0.57 ; % specific growth rate h-1
y = 0.52 ; % cell yield coefficient (g/g)
s = 5.12 ; % substrate utilization
% Initial Value
x0 = 0.3 ; % Initial cell concentration (g/L)
XSOL(1) = x0 ; % Store initial value in solution vector
X = x0 + y*s ; % Calculated maximum X
s0 = 10 ; % Initial carbon source
SSOL(1) = s0 ; % Store initial value of s
% Time information
dt = 1 ; % Time step
TFinal = 8 ; % Final simulation time
t=0 ; % Initialize time to zero
tVec(1) =0; % store first time-value
% Solve equation/ March forward in time
ct = 1 ; % counter
while t < TFinal
% Euler's time-step
SSOL(ct+1) = SSOL(ct) - dt*u*(1/y)*(XSOL(ct)*(1-XSOL(ct)/X));
% Store time-value
tVec(ct+1) = t + dt;
% Update counter and time
t = t+dt ;
ct = ct+1 ;
end
got error in SSOL(ct+1) = SSOL(ct) - dt*u*(1/y)*(XSOL(ct)*(1-XSOL(ct)/X)); where it exceeds the number of array elements (1).
Can someone please help me?

 채택된 답변

Alan Stevens
Alan Stevens 2021년 11월 9일

0 개 추천

You don't update XSOL, so when ct is 2, XSOL(ct) doesn't exist.

댓글 수: 6

understood. do you have any suggestion to solve the problem?
You need an equation that updates XSOL.
I have seperate the XSOL and update it like this,
XSOL(ct+1) = XSOL(ct) + dt*u*XSOL(ct)*(1-XSOL(ct) / X);
but I'm not sure how to use it for SSOL.
Just have a for loop in advance of your while loop where you create XSOL
for ct = 1 : tFinal-1
XSOL(ct+1) = XSOL(ct) + dt*u*XSOL(ct)*(1-XSOL(ct) / X);
end
ct = 1 ; % counter
for ct = 1 : TFinal-1
XSOL(ct+1) = XSOL(ct) + dt*u*XSOL(ct)*(1-XSOL(ct) / X);
end
while t < TFinal
SSOL(ct+1) = SSOL(ct) - dt*u*(1/y)*XSOL(ct);
end
I did it like this but it still have error at SSOL(ct+1) = SSOL(ct) - dt*u*(1/y)*XSOL(ct);
index exceed the number of arrays.
Like this:
% Effect of carbon sources on the growth of Lactoccus lactis. Ibrahim et. al (2010)
% Parameters
u = 0.57 ; % specific growth rate h-1
y = 0.52 ; % cell yield coefficient (g/g)
s = 5.12 ; % substrate utilization
% Initial Value
x0 = 0.3 ; % Initial cell concentration (g/L)
XSOL(1) = x0 ; % Store initial value in solution vector
X = x0 + y*s ; % Calculated maximum X
s0 = 10 ; % Initial carbon source
SSOL(1) = s0 ; % Store initial value of s
% Time information
dt = 1 ; % Time step
TFinal = 8 ; % Final simulation time
t=0 ; % Initialize time to zero
tVec(1) =0; % store first time-value
% Solve equation/ March forward in time
ct = 1 ; % counter
ct = 1 ; % counter
for ct = 1 : TFinal-1
XSOL(ct+1) = XSOL(ct) + dt*u*XSOL(ct)*(1-XSOL(ct) / X);
SSOL(ct+1) = SSOL(ct) - dt*u*(1/y)*XSOL(ct);
% Store time-value
tVec(ct+1) = t + dt;
% Update counter and time
t = t+dt ;
ct = ct+1 ;
end
subplot(2,1,1)
plot(tVec,XSOL),grid
xlabel('t'), ylabel('XSOL')
subplot(2,1,2)
plot(tVec,SSOL),grid
xlabel('t'), ylabel('SSOL')

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

제품

릴리스

R2021a

태그

Community Treasure Hunt

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

Start Hunting!

Translated by