Plotting an increasing real sequence
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Benjamin Wilson
il 12 Ott 2023
Commentato: Benjamin Wilson
il 12 Ott 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
0 Commenti
Risposta accettata
Dyuman Joshi
il 12 Ott 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
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!