# Issues with Cake Eating Problem

A while back I asked about simulating the cake eating problem in sage and the algorithm seems like an excellent way to work with things. So far the code looks as follows for obtaining a soloution to the cake eating problem:

#Step1: Name variables
k,k1,v0,beta,vk,vk1=var('k,k1,v0,beta,vk,vk1')

#Step2: Initialize your values
beta=0.6
v0=log(k)

#Step 3: The Loop to Obtain Policy function
for n in range(5):
vk = log(k-k1) + beta*v0(k=k1)
FOC = vk.diff(k1)
k1star = solve(FOC==0, k1)
print(n, k1star)
v0 = (vk).subs(k1=k1star.rhs())


The results I get are as follows:

0 [k1 == 3/8*k]
1 [k1 == 24/49*k]
2 [k1 == 147/272*k]
3 [k1 == 816/1441*k]
4 [k1 == 4323/7448*k]


Initially I thought that it would be a matter of just setting k to some value however this breaks the loop.

If I wanted to obtain values for k1 (and then plot them) how would I go about doing this?

edit retag close merge delete

Sort by » oldest newest most voted

I'm not sure to understand your question, but...

k is a symbolic variable. You can't "set" it stricto sensu (setting the python variable kwould destroy your only reference to the symbolic variable k) but you can substitute it with another value ; for example, you could write your "print" line as print(n, klstar.subs(k1==<SomeValue>).n()) (if you want numeric values).

Collecting the successive values of n and k1(n) is left as an exercise for the reader (there are a large number of ways to do this, not all of them worth exposition...).

more

Hey I tried your print command and I got nothing. Could you direct me to a resource where I could learn how to collect successive values?

What precisely" did you try to "get nothing" ? What was on the screen ? Did you account for the fact that you are working on a *list of solutions (I forgopt that in my quick answer...).

See any Python tutorial on lists and iterations...

My code is as follows:

#Step 3: The Loop to Obtain Policy function for n in range(5): vk = log(k-k1) + beta*v0(k=k1) FOC = vk.diff(k1) k1star = solve(FOC==0, k1) print(n, klstar.subs(k1==100).n()) v0 = (vk).subs(k1=k1star.rhs())