Array indices must be positive integers or logical values.

조회 수: 5 (최근 30일)
Joseph Wunschel
Joseph Wunschel 2021년 10월 2일
댓글: Joseph Wunschel 2021년 10월 3일
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
John D'Errico
John D'Errico 2021년 10월 2일
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?
Joseph Wunschel
Joseph Wunschel 2021년 10월 2일
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

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

채택된 답변

Steven Lord
Steven Lord 2021년 10월 2일
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
Jan
Jan 2021년 10월 2일
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.
Joseph Wunschel
Joseph Wunschel 2021년 10월 3일
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.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by