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

 採用された回答

Star Strider
Star Strider 2018 年 1 月 17 日

0 投票

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

6 件のコメント

ektor
ektor 2018 年 1 月 17 日
編集済み: ektor 2018 年 1 月 17 日
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?
Steven Lord
Steven Lord 2018 年 1 月 17 日
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 2018 年 1 月 17 日
thanks. HOwever, how can I write (in code) m(t) as a function of past and current values of a?
Star Strider
Star Strider 2018 年 1 月 17 日
@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 2018 年 1 月 17 日
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 2018 年 1 月 17 日
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’.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2018 年 1 月 17 日

コメント済み:

2018 年 1 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by