How can I fix "Index exceeds the number of array elements (1)."

1 view (last 30 days)
This is my script.
function [ca, depth] = transientDiffModel(B,Ds,H,ct,dz,t,th)
B= input(' B:degradation rate of antibiotic in biofilm, in s^-1: ');
Ds= input('Ds:diffusion constant of antibiotic, in mm^2/s:');
H= input('H: total depth of biofilm, in mm:');
ct= input('ct: strength of source of antibiotic at z=0, in ug*mm/L:');
dz= input('dz: step size in depth for simulation, in mm:');
t= input('t: time at which concentration profile is being computed, in s:');
th= input('th: threshold below which a concentration is considered to be zero, in ug/L:');
n=1;
z(1)=0;
ca(1)=ct/sqrt(4*pi*Ds*t)*exp(-B*t);
while ca(n)>=th || z(n)<H;
n=n+1;
z(n)=z(n)+dz;
ca(n)=ct/sqrt(4*pi*Ds*t)*exp(-z(n)^2/(4*Ds*t)-B*t);
end
end
When I run the code, it says:
Index exceeds the number of array elements (1).
Error in transientDiffModel (line 33)
z(n)=z(n)+dz;

Answers (1)

KSSV
KSSV on 16 Oct 2020
Edited: KSSV on 16 Oct 2020
Modify it to:
function [ca, depth] = transientDiffModel(B,Ds,H,ct,dz,t,th)
B= input(' B:degradation rate of antibiotic in biofilm, in s^-1: ');
Ds= input('Ds:diffusion constant of antibiotic, in mm^2/s:');
H= input('H: total depth of biofilm, in mm:');
ct= input('ct: strength of source of antibiotic at z=0, in ug*mm/L:');
dz= input('dz: step size in depth for simulation, in mm:');
t= input('t: time at which concentration profile is being computed, in s:');
th= input('th: threshold below which a concentration is considered to be zero, in ug/L:');
n=1;
z(1)=0;
ca(1)=ct/sqrt(4*pi*Ds*t)*exp(-B*t);
while ca(n)>=th || z(n-1)<H;
n=n+1;
z(n)=z(n-1)+dz;
ca(n)=ct/sqrt(4*pi*Ds*t)*exp(-z(n-1)^2/(4*Ds*t)-B*t); % decide here z(n) or z(n-1)
end
end

Categories

Find more on Historical Contests in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!