# Plotting multiple functions in Sage from a for loop

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')

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]


Im interested in plotting each instance of k1 from my loop. how would I go about doing this?

edit retag close merge delete

Hello, @EconJohn! I am adding a solution below that plots the solutions you found. I am not 100% sure that this is what you want. Please, let me know if you require some different kind of plot.

Sort by » oldest newest most voted

Hello, @EconJohn!

What you can do is to create a Graphics object (let's call it p) before entering the loop, then superimpose the plots of each k1 over the previous one and store the result in p, and then show the plot outside of the loop. Since yourk1s are defined as symbolic equalities, you will need to use the rhs() (right-hand side of the equality) method inside the plot() command.

Before writing the final version of the code, I would like to make a few observations: You don't need to define v0 and beta as symbolic variables, since you later assign to beta the value 0.6, which overwrites the effect of the var() command, converting it into a numerical variable; similarly, you assign v0 = log(k), which overwrites the definition made by var() (in this case, v0 IS automatically made a symbolic variable, since it is the logarithm of the symbolic variable k.) For a similar reason, it is unnecessary to define vk with var(). Finally, you define but never use the vk1 variable.

Here is the code:

#Step1: Name variables
k,k1 = var('k,k1')

beta = 0.6
v0 = log(k)
cols = ['red', 'darkgreen', 'blue', 'magenta', 'yellowgreen']
p = Graphics()

#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())
p += plot(k1star.rhs(), color=cols[n], legend_label=str(k1star))
p.show()


I have taken the liberty of defining a list cols with some different colors in order to differentiate the plots. The instruction p = Graphics() defines p as an empty plot. Inside the loop, I have added the instruction

p += plot(k1star.rhs(), color=cols[n], legend_label=str(k1star))


Remember that superimposing plots is equivalent to "adding" the plots, so this command generates a plot for every iteration and adds it to p. I have used the $n$-th color in the cols list. I have also added a label, indicating which color and formula correspond to which plot. The last line, p.show() finally shows the plot.

I hope this helps! Please, let me know if you need some additional help.

more

Would you know how to reset the color if I were to say do 10 iteration instead of 5?

1

All of Sage's predefined colors are stored inside the colors variable. You could write something like

p += plot(k1star.rhs(), color=list(colors)[n], legend_label=str(k1star))


By the way, colors is a Python dictionary, so I first convert it to a list in the previous line, in order to access its elements.

Now, the first few colors that are predefined are too light for my particular taste, so I would prefer to write

print(list(colors))


and select a few new ones that make good contrast.

Finally, you can rotate the five colors in the cols variable that i defined in my answer with this code:

p += plot(k1star.rhs(), color=cols[n%5], legend_label=str(k1star))


Notice I have used cols[n%5]. The syntax n%5 is the Sage equivalent of $n \pmod 5$.