Plotting an increasing real sequence

4 ビュー (過去 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 Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by