Plotting an increasing real sequence

조회 수: 1 (최근 30일)
Benjamin Wilson
Benjamin Wilson 2023년 10월 12일
댓글: Benjamin Wilson 2023년 10월 12일
I am trying to plot an increasing sequence n(t) against t over an interval given in the function:
function Initialproblem(N,T,p)
n=zeros(T,1);
for t = 1:T
s=t-1;
n(t) = N*exp(-p)+n(s)*(1-exp(-p));
end
fplot(0:1:T,n_t)
I think there is an issue when I call n(s) but I need to access the element prior.
The error I am getting is "Array indices must be positive integers or logical values." Clearly a function including exp will not be a integer, but I don't know how else to perform this task.
Any help would be great

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 10월 12일
"I think there is an issue when I call n(s)"
You are right. When t == 1, s = t-1 == 0. And as you are using s as an index, it gives the error.
Indexing in MATLAB starts from 1 (as can be inferred from the error message).
The solution is to define the value of 1st element manually and start the for loop from t == 2.
If the value is 0, you can remove the assignment, as you have already assigned it to zero.
function Initialproblem(N,T,p)
n=zeros(T,1);
n(1) = value_of_starting_point;
for t = 2:T
s = t-1;
n(t) = N*exp(-p)+n(s)*(1-exp(-p));
end
fplot(0:1:T,n_t)
end
  댓글 수: 1
Benjamin Wilson
Benjamin Wilson 2023년 10월 12일
Thanks for replying! Dyuman you are the best!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by