Plotting an increasing real sequence

4 vues (au cours des 30 derniers jours)
Benjamin Wilson
Benjamin Wilson le 12 Oct 2023
Commenté : Benjamin Wilson le 12 Oct 2023
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

Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 12 Oct 2023
"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 commentaire
Benjamin Wilson
Benjamin Wilson le 12 Oct 2023
Thanks for replying! Dyuman you are the best!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by