Array indices must be positive integers or logical values.

Hello,
I have been trying to run the code below. I have gotten everything to work for lines 14 & 15, but I cannot figure out what is wrong with line 16 (NC_t). I receieve the follow error upon running the program:
"Array indices must be positive integers or logical values."
May someone help explain where something is going wrong? I would greatly appreciate the help.
Thanks!
clc;
clear all;
n=51;
NA_0=100; % Initial Condition for N1
NB_0=0; % Initial Condition for N2
NC_0=0; % Inital Condition for N2
Lambda_0=0.3429; % Decay Constant for N_1 (hr^-1)
Lambda_1=.076154; % Decay Constant for N_2 (hr^-1)
t(1)=0; % (hr)
dt=1; % (hr)
for i=1:n
t(i)=i*dt-dt;
NA_t(i)=NA_0*exp(-1*Lambda_0*t(i));
NB_t(i)=NB_0*exp(-1*Lambda_1*t(i))+(Lambda_0*NA_0)/(Lambda_1-Lambda_0)*(exp(-1*Lambda_0*t(i))-exp(-1*Lambda_1*t(i)));
NC_t(i)=NC_0+NB_0*(1-exp(-1*Lambda_1*t(i)))+(NA_0/(Lambda_1-Lambda_0))*(Lambda_1(1-exp(-1*Lambda_0*t(i)))-Lambda_0(1-exp(-1*Lambda_1*t(i))));
end
Array indices must be positive integers or logical values.
plot(t,NB_t,t,NA_t);

2 Comments

When you show a PICTURE of code, what happens is now we cannot even paste in your code nd execute it for ourselves. Everything there was purely text, so trivial to paste in directly. But now we need to read small, fuzzy pictures of text, to try to find the error in your code.
Why would you deliberately make it more difficult for someone to help you?
That would make more sense. I appologize. I'm very new to coding, especially MATLAB and never posted something like this before so I didn't really think of that at the time. I just updated the original post with the code. I hope this helps. Thanks

Sign in to comment.

 Accepted Answer

In the last line of your for loop:
(Lambda_1(1-exp(-1*Lambda_0*t(i)))-Lambda_0(1-exp(-1*Lambda_1*t(i))))
It's very unlikely 1-exp(-1*Lambda_0*t(i)) will be exactly an integer value that is suitable for use as an index into Lambda_1. I'm guessing you're probably missing a multiplication there. The same holds for the second term in that snippet I cited.

2 Comments

I agree. A bold guess:
NC_t(i) = NC_0 + NB_0 * (1 - exp(-Lambda_1 * t(i))) + ...
(NA_0 / (Lambda_1 - Lambda_0)) * ...
(Lambda_1 * (1 - exp(-Lambda_0 * t(i))) - ...
Lambda_0 * (1 - exp(-Lambda_1 * t(i))));
@Joseph Wunschel: Do you see the benefit of using spaces around operators? Everything, which assists during debugging, is the best friend of the programmer.
Thank you both for the comments. Putting a multiplication sign bewteen the variables worked perfectly and corrected the error I was getting:
(Lambda_1*(1-exp(-1*Lambda_0*t(i)))-Lambda_0*(1-exp(-1*Lambda_1*t(i))))
I appreciate the help.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2021b

Tags

Community Treasure Hunt

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

Start Hunting!