Clear Filters
Clear Filters

Plotting an increasing real sequence

1 view (last 30 days)
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

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 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

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!