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

 Respuesta aceptada

Star Strider
Star Strider el 17 de En. de 2018

0 votos

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

6 comentarios

ektor
ektor el 17 de En. de 2018
Editada: ektor el 17 de En. de 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.
ektor
ektor el 17 de En. de 2018
thanks. HOwever, how can I write (in code) m(t) as a function of past and current values of a?
Star Strider
Star Strider el 17 de En. de 2018
@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.
ektor
ektor el 17 de En. de 2018
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.
Star Strider
Star Strider el 17 de En. de 2018
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’.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 17 de En. de 2018

Comentada:

el 17 de En. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by