CoCalc Shared FilesLab 4.sagews
Views : 40
##### Group 6 Lab 4 Grade: 16/20
#exercise 1
def iterate(fcn,number_of_times,starting_value):
count = srange(0,number_of_times) # how many times to iterate
vals=[starting_value]             # initialize the list to hold the results
for i in count:
a = vals[i]# grab the last item in the output list
b=fcn(a)# plug it into the function
vals.append(b)# Put b into the list
return vals
var('N') #N is our variable
h=0.1  #we have a step size of 0.1
g(N)=N+h*0.2*N*(1-N/100) #the update for Euler to get the new value of N
count=int(round(100/h)) #how many steps to take to get to 100 from 0
iterate(g,100,10) ##### You were supposed to plot the results for Exercise 1, but I see you did it for a later exercise so I won't take off any points.

N [10, 10.1800000000000, 10.3628735200000, 10.5486531608817, 10.7373714073976, 10.9290606065975, 11.1237529455809, 11.3214804285737, 11.5222748533262, 11.7261677868336, 11.9331905403773, 12.1433741438903, 12.3567493196484, 12.5733464552916, 12.7931955761805, 13.0163263170940, 13.2427678932773, 13.4725490708478, 13.7056981365714, 13.9422428670207, 14.1822104971285, 14.4256276881541, 14.6725204950778, 14.9229143334436, 15.1768339456718, 15.4343033668624, 15.6953458901155, 15.9599840313958, 16.2282394939672, 16.5001331324319, 16.7756849164029, 17.0549138938481, 17.3378381541397, 17.6244747908507, 17.9148398643370, 18.2089483641508, 18.5068141713282, 18.8084500206003, 19.1138674625768, 19.4230768259530, 19.7360871797947, 20.0529062959569, 20.3735406116931, 20.6979951925157, 21.0262736953682, 21.3583783321730, 21.6943098338204, 22.0340674146637, 22.3776487375902, 22.7250498797374, 23.0762652989248, 23.4312878008740, 23.7901085072900, 24.1527168248781, 24.5191004153711, 24.8892451666427, 25.2631351649825, 25.6407526686094, 26.0220780824990, 26.4070899346026, 26.7957648535318, 27.1880775477853, 27.5840007865922, 27.9835053824451, 28.3865601753961, 28.7931320191857, 29.2031857692746, 29.6166842728451, 30.0335883608386, 30.4538568420897, 30.8774464996198, 31.3043120891448, 31.7344063398528, 32.1676799575012, 32.6040816298816, 33.0435580346937, 33.4860538498691, 33.9315117663792, 34.3798725035564, 34.8310748269554, 35.2850555687743, 35.7417496508515, 36.2010901102477, 36.6630081274186, 37.1274330569767, 37.5942924610362, 38.0635121451277, 38.5350161966658, 39.0087270259437, 39.4845654096256, 39.9624505367008, 40.4423000568551, 40.9240301312145, 41.4075554854027, 41.8927894648554, 42.3796440923232, 42.8680301274912, 43.3578571286387, 43.8490335162540, 44.3414666385172, 44.8350628385566]

#exercise 2
def iterate(fcn,number_of_times,starting_value):
count = srange(0,number_of_times) # how many times to iterate
vals=[starting_value]             # initialize the list to hold the results
for i in count:
a = vals[i]# grab the last item in the output list
b=fcn(a)# plug it into the function
vals.append(b)# Put b into the list
return vals
var('N') #N is our variable
h=0.1  #we have a step size of 0.1
g(N)=N+h*0.2*N*(1-af2f-ef75067976a0

#exercise 3
def iterate(fcn,number_of_times,starting_value):
count = srange(0,number_of_times) # how many times to iterate
vals=[starting_value]             # initialize the list to hold the results
for i in count:
a = vals[i]# grab the last item in the output list
b=fcn(a)# plug it into the function
vals.append(b)# Put b into the list
return vals

var('N') #N is our variable
h=0.1  #we have a step size of 0.1
g(N)=N+h*0.2*N*(1-N/100) #the update for Euler to get the new value of N
count=int(round(100/h)) #how many steps to take to get to 20 from 0
Ns=iterate(g,count,10) #call our iterator, or the inputs, store the list in Ns
ts=srange(0,100+h,h)    #make a list of outputs in ts
list_plot(zip(ts,Ns),plotjoined=True,axes_labels=["t","N"],ymin=0,ymax=100) #use zip to out the times with the Ns. Label the plot. Set the scale
##### Good.-HK


N
#Exercise 4
def iterate (fcn, num_times, starting_value):
count = srange(0,num_times) # how many times to iterate
vals=[starting_value]             # initialize the list to hold the results
for i in count:
a = vals[i]# grab the last item in the output list
b=fcn(a)# plug it into the function
vals.append(b)# Put b into the list
return vals

def euler_4_function (diff_eq,initial,h,num_times):
func(N)=N+h(diff_eq(N))
ex4out=iterate(num_times, initial, func)
return ex4out
var('N')
N_prime(N)=(0.2)(N)(1-N/100)
h=0.1
out4=euler_4_function (N_prime, 10, h, 1000)

N
Error in lines 14-14 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1191, in execute flags=compile_flags), namespace, locals) File "", line 1, in <module> TypeError: 'sage.rings.real_mpfr.RealLiteral' object is not callable
#exercise 5
def iterate (fcn, num_times, starting_value):
count = srange(0,num_times) # how many times to iterate
vals=[starting_value]             # initialize the list to hold the results
for i in count:
a = vals[i]# grab the last item in the output list
b=fcn(a)# plug it into the function
vals.append(b)# Put b into the list
return vals

def euler_5_function (diff_eq, pairs_time, h, state_value):
func(N)=N+h*(diff_eq(N))
ex5out = iterate(pairs_time, state_value, h) ##### This line isn't working properly. Your iterate function requires an input of function, numer of times, and starting value IN THAT ORDER. You aren't inputting your function into iterate at all. Furthermore, you sould be zipping the time and population values inside your euler_5_function. (-2 points)
return ex5out
var("N")
N_prime(N)=(0.2)(N)(1-N/100)
h=0.1
out5=euler_5_function (N_prime, 10, h, 1000)

N
Error in lines 14-14 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1191, in execute flags=compile_flags), namespace, locals) File "", line 1, in <module> TypeError: 'sage.rings.real_mpfr.RealLiteral' object is not callable
#exercise 6
def ex_6_function(g,initial_condition,h,tfinal):
count=srange(0,int(round(tfinal/h))
vals=[initial_condition]
ts=srange(0,100,+h,h)
for i in count:
vals,append(val[i]+h*g(vals[i]))
return zip(ts,vals)
out=ex_6_function(diff_eq,50,0.2,1000)

Error in lines 1-7 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1191, in execute flags=compile_flags), namespace, locals) File "<string>", line 3 vals=[initial_condition] ^ SyntaxError: invalid syntax
#exercise 7

##### Where is exercise 7? (-2 points)
︠716d8936-7dab-4287-94e7-d5f108825bcf︠
#exercise 8

︠095d51d0-4b55-464b-8808-695617336f23︠