Bellman equation with vector input
6 次查看(过去 30 天)
显示 更早的评论
Hello,
I am trying to solve a dynamic programming problem with the help of the Bellman equation and backward recursion (meaning that optimum value must be found backwards, starting at the end)
My code looks like this:
function [V] = Bellman(K,B,P,G,T)
C = [B K-B]
for t=(T+1):-1:1
for c=[1 1]:C
if t==T+1
V(c,t) = 0
else
V(c,t) = 0.5*max(V(c,t+1),P+V(c(1)-1,t+1)) + (t/(2*T))*max(V(c,t+1),G+V(c,t+1)) + 1-0.5-(t/(2*T))*V(c,t+1)
end
end
end
The value V should be calculated for each period t and for different capacity levels c. However each c consists of two elements (e.g.: c=[1 1]). Obviously, when I run my code, I get the error: Subscript indices must either be real positive integers or logicals, because either I have subscript indices of value 0 or my code isn't iterating over the vector c, whose element should increase by 1 seperatly (e.g. c=[1 1, 2 1, 2 2, 3 2, 3 3,...]).
I hope my problem is clear and someone can help me out.
Thanks !
1 个评论
回答(1 个)
Jan
2018-12-18
Replace:
C = [B K-B];
for c=[1 1]:C
...
by
C = [B K-B];
for c1 = 1:B
for c2 = 1:K-B
c = [c1, c2]
...
end
end
But the problem remains:
V(c(1)-1,t+1))
Here c(1)-1 is 0, as madhan has mentioned already.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!