Array indices must be positive integers or logical values.
조회 수: 5 (최근 30일)
이전 댓글 표시
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
plot(t,NB_t,t,NA_t);
댓글 수: 2
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?
채택된 답변
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
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!