ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 20 Jul 2020 21:39:53 +0200Recursive problems in Sagehttps://ask.sagemath.org/question/52565/recursive-problems-in-sage/ I've been trying to figure out how to run a basic recursive problem in sage. The basic structure of the problem I have figured out, its just a matter of putting it in terms of a for/ while loop.
#Initializing values
k,k1,vk,beta,vk1=var('k,k1,vk,beta,vk1')
vk= log(k-k1)+beta*log(k1)
#I want to loop this procedure
FOC=vk.diff(k1)
k1star=solve(FOC==0,k1)
vk=(vk).subs(k1star)
vk
Any help is appreciated. Sun, 19 Jul 2020 19:38:18 +0200https://ask.sagemath.org/question/52565/recursive-problems-in-sage/Comment by slelievre for <p>I've been trying to figure out how to run a basic recursive problem in sage. The basic structure of the problem I have figured out, its just a matter of putting it in terms of a for/ while loop.</p>
<pre><code>#Initializing values
k,k1,vk,beta,vk1=var('k,k1,vk,beta,vk1')
vk= log(k-k1)+beta*log(k1)
#I want to loop this procedure
FOC=vk.diff(k1)
k1star=solve(FOC==0,k1)
vk=(vk).subs(k1star)
vk
</code></pre>
<p>Any help is appreciated. </p>
https://ask.sagemath.org/question/52565/recursive-problems-in-sage/?comment=52582#post-id-52582The context is given in [Ask Sage question 52559: Cake-eating problem](https://ask.sagemath.org/question/52559).Mon, 20 Jul 2020 20:16:32 +0200https://ask.sagemath.org/question/52565/recursive-problems-in-sage/?comment=52582#post-id-52582Comment by Emmanuel Charpentier for <p>I've been trying to figure out how to run a basic recursive problem in sage. The basic structure of the problem I have figured out, its just a matter of putting it in terms of a for/ while loop.</p>
<pre><code>#Initializing values
k,k1,vk,beta,vk1=var('k,k1,vk,beta,vk1')
vk= log(k-k1)+beta*log(k1)
#I want to loop this procedure
FOC=vk.diff(k1)
k1star=solve(FOC==0,k1)
vk=(vk).subs(k1star)
vk
</code></pre>
<p>Any help is appreciated. </p>
https://ask.sagemath.org/question/52565/recursive-problems-in-sage/?comment=52574#post-id-52574I don't see the point : the value of `k1` that nullifies $\frac{\partial vk}{\partial k1}$ is *by definition* an expression that does *not* depend on `k1`.
#Initializing values
k,k1,beta,vk1=var('k,k1,beta,vk1')
vk(k1)= log(k-k1)+beta*log(k1)
S1=vk.diff(k1).solve(k1)
At this point :
sage: S1
[k1 == beta*k/(beta + 1)]
sage: vk(k1=S1[0].rhs())
beta*log(beta*k/(beta + 1)) + log(-beta*k/(beta + 1) + k)
But, of course :
sage: vk(k1=S1[0].rhs()).diff(k1)
0
So there's *nothing* to iterate.
Aren't you trying to implement something like Newton's method of root-finding when `solve` doesn't give a explicit answer ?Mon, 20 Jul 2020 10:10:33 +0200https://ask.sagemath.org/question/52565/recursive-problems-in-sage/?comment=52574#post-id-52574Comment by EconJohn for <p>I've been trying to figure out how to run a basic recursive problem in sage. The basic structure of the problem I have figured out, its just a matter of putting it in terms of a for/ while loop.</p>
<pre><code>#Initializing values
k,k1,vk,beta,vk1=var('k,k1,vk,beta,vk1')
vk= log(k-k1)+beta*log(k1)
#I want to loop this procedure
FOC=vk.diff(k1)
k1star=solve(FOC==0,k1)
vk=(vk).subs(k1star)
vk
</code></pre>
<p>Any help is appreciated. </p>
https://ask.sagemath.org/question/52565/recursive-problems-in-sage/?comment=52584#post-id-52584@EmmanuelCharpentier I see the issue with my approach. Though I actually made a video on solving a planners problem which is similar to this that uses value function iteration, https://www.youtube.com/watch?v=Bv_tP3GP5Qg&list=PLLAPgKPWbsiQ0Ejh-twYC3Fr8_WA9BKCc&index=4. I just want to be able to code something like this.Mon, 20 Jul 2020 21:12:31 +0200https://ask.sagemath.org/question/52565/recursive-problems-in-sage/?comment=52584#post-id-52584Answer by jaydfox for <p>I've been trying to figure out how to run a basic recursive problem in sage. The basic structure of the problem I have figured out, its just a matter of putting it in terms of a for/ while loop.</p>
<pre><code>#Initializing values
k,k1,vk,beta,vk1=var('k,k1,vk,beta,vk1')
vk= log(k-k1)+beta*log(k1)
#I want to loop this procedure
FOC=vk.diff(k1)
k1star=solve(FOC==0,k1)
vk=(vk).subs(k1star)
vk
</code></pre>
<p>Any help is appreciated. </p>
https://ask.sagemath.org/question/52565/recursive-problems-in-sage/?answer=52586#post-id-52586Below is what I was able to come up with. I checked the first couple iterations to make sure they were correct. After that, it looks right at a glance, but I haven't verified. This is from my old SAGE 8.4 installation (I have 9.0 on a different computer), so the `print` statement will be formatted differently in version 9.0.
The key is to substitute the result back into `v0` *before* substituting back into `vk`.
sage: v0 = log(k)
sage: 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[0].rhs())
....:
(0, [k1 == beta*k/(beta + 1)])
(1, [k1 == (beta^2 + beta)*k/(beta^2 + beta + 1)])
(2, [k1 == (beta^3 + beta^2 + beta)*k/(beta^3 + beta^2 + beta + 1)])
(3, [k1 == (beta^4 + beta^3 + beta^2 + beta)*k/(beta^4 + beta^3 + beta^2 + beta + 1)])
(4, [k1 == (beta^5 + beta^4 + beta^3 + beta^2 + beta)*k/(beta^5 + beta^4 + beta^3 + beta^2 + beta + 1)])
Mon, 20 Jul 2020 21:23:37 +0200https://ask.sagemath.org/question/52565/recursive-problems-in-sage/?answer=52586#post-id-52586Comment by EconJohn for <p>Below is what I was able to come up with. I checked the first couple iterations to make sure they were correct. After that, it looks right at a glance, but I haven't verified. This is from my old SAGE 8.4 installation (I have 9.0 on a different computer), so the <code>print</code> statement will be formatted differently in version 9.0.</p>
<p>The key is to substitute the result back into <code>v0</code> <em>before</em> substituting back into <code>vk</code>.</p>
<pre><code>sage: v0 = log(k)
sage: 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[0].rhs())
....:
(0, [k1 == beta*k/(beta + 1)])
(1, [k1 == (beta^2 + beta)*k/(beta^2 + beta + 1)])
(2, [k1 == (beta^3 + beta^2 + beta)*k/(beta^3 + beta^2 + beta + 1)])
(3, [k1 == (beta^4 + beta^3 + beta^2 + beta)*k/(beta^4 + beta^3 + beta^2 + beta + 1)])
(4, [k1 == (beta^5 + beta^4 + beta^3 + beta^2 + beta)*k/(beta^5 + beta^4 + beta^3 + beta^2 + beta + 1)])
</code></pre>
https://ask.sagemath.org/question/52565/recursive-problems-in-sage/?comment=52587#post-id-52587I'm genuinely surprised how little code is needed for this problem. Thank you!Mon, 20 Jul 2020 21:39:53 +0200https://ask.sagemath.org/question/52565/recursive-problems-in-sage/?comment=52587#post-id-52587