Bellman equation with vector input
Show older comments
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 Comment
madhan ravi
on 17 Dec 2018
V(c(1)-1,t+1)) ==> c(1)=1 1-1 == 0 zero cannot be an index in matlab!!
Answers (1)
Jan
on 18 Dec 2018
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.
Categories
Find more on Programming 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!