solving an equation recursively

Dear all,
I want to solve the following equation
m(t)=a(t)+k*m(t-1); t=2,...T
for the entire path m(t), with the initial condition
m(1)=a(1)+k*ee;
where ee=4;
In other words, If we solve the above equation, each m(t) will depend on past and current values of a's.
The vector a in the above equations is known, say a=randn(T,1) and k=0.4.
Any way of doing this?
Thank in advance

 Accepted Answer

Star Strider
Star Strider on 17 Jan 2018
Yes. Use a for (link) loop.
This seems to be homework, so I’ll not post the solution I coded.

6 Comments

ektor
ektor on 17 Jan 2018
Edited: ektor on 17 Jan 2018
Ok, just to be sure
for t=1:T
if t==1
m(1)=a(1)+k*ee;
else
m(t)=a(t)+k*m(t-1);
end
end
Right?
That would work. I'd probably preallocate m, fill in the first value, and run the for loop from 2 to T.
m = zeros(1, T);
m(1) = a(1)+k*ee;
for t = 2:T
m(t) = a(t)+k*m(t-1);
end
That avoids having to check if the if condition is true during each iteration of the for loop body.
thanks. HOwever, how can I write (in code) m(t) as a function of past and current values of a?
@Steven Lord — Thank you!
@Staf — Note that ‘a’ is determined completely before the loop, and according to your initial post, never changes. You can access the entire vector whenever you want to. The ‘m(t)’ assignment in the for loop stores all the values of the ‘m’ vector as the loop executes. You can access all of its values after the loop completes. So ‘m’ is already a function of the past and current values of ‘a’ in each iteration.
Basically, each element of 'a' changes in each iteration of my code. Should I open another thread for this if the solution is different from what you proposed above. I assumed fixed 'a' to simplify the analsis.
How does ‘a’ change in each iteration? You never mentioned that! I was using the information you provided.
You would create and update ‘a’ as ‘a(t)’ the same way you create and update ‘m(t)’, using whatever rule you want to create and define it it. Preallocate ‘a’ as well as ‘m’.

Sign in to comment.

More Answers (0)

Categories

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

Asked:

on 17 Jan 2018

Commented:

on 17 Jan 2018

Community Treasure Hunt

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

Start Hunting!