CoCalc Shared FilesLab 3 Iteration.sagews
Views : 68
##### Group 2 Lab 3 Grade: 20/20. Good job!
#Exercise 1
g(x)=19*(x^3)
g(0)
g(10)
g(-4)

0 19000 -1216
#Exercise 2
g(x)=43*(x^100)
g(0)
g(10)
g(-4)

0 430000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 69098335903136581848304369970669991908454728732660091917959168
#Exercise 3
srange(1,101)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
#Exercise 4
srange(0,51,2)

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50]
#Exercise 5
nums = srange(1,11)
for n in nums:
n


1 2 3 4 5 6 7 8 9 10
#Exercise 6
num = srange(10.0,50,10)
for n in num:
sqrt(n)

3.16227766016838 4.47213595499958 5.47722557505166 6.32455532033676
#Exercise 7
mult3 = []
nums = srange (1,6)
for n in nums:
mult3.append(3*n)
mult3
#This made it  so we can make a list of the first five multiples of 3

[3, 6, 9, 12, 15]
#Exercise 8
mult4 = []
nums = srange (1,11)
for n in nums:
mult4.append(4*n)
mult4
#This made it  so we can make a list of the first ten multiples of 4

[4, 8, 12, 16, 20, 24, 28, 32, 36, 40]
#Exercise 9
exer7 = []
nums = srange (1,11)
for n in nums:
exer7.append(g(n))

exer7


[43, 54508975809813864264358237831168, 22161233391476487234567828579921714726190623446043, 69098335903136581848304369970669991908454728732660091917959168, 339210189245035076327043283071598078759478767096879892051219940185546875, 28092700810503048962157681487796486283097179550337059476352094553888377083527168, 139082489913864593627819854071309322866859737552898892240157499249842732764387906580043, 87592546982382901709543164601603260925213140927635258777354039322238395889823478365886087168, 11421401521662615081567596847538523953657038464095963326906723354780977108755993868758542058892043, 430000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]
#Exercise 10
list_plot(exer7, axes_labels=["numbers", "blah"], plotjoined=True, color= 'red')


#Exercise 11
f(x) = 2*x
count = srange(0,10)
val=3
for i in count:
a = f(val)
val=a
val
#Exercise 12
#It  is necessary becuase we need to plug val into f and have that result display after typing in val.

3072
#Exercise 13
f(x) = 2*x
count = srange(3,20)
val=3
for i in count:
a = f(val)
val=a
val

393216
#Exercise 14
f(x) = 2*x
count = srange(5,20)
val=3
for i in count:
a = f(val)
val=a
val

98304
#Exercise 15
g(x) = 3*x
count = srange(2,5)
val=3
for i in count:
a = f(val)
val=a
val

24
#Exercise 16
f(x) = 2*x
count = srange(0,10)
val=3
for i in count:
val = f(val)
val ##### Good. -HK

3072

#Exercise 17: exsamples 3 and 4 are similar becuase we are defining a fuction we want to iterate and then we create a list of numbers telling the amount of times we iterate. Then the diffrence is that in exsample 4 we are making a=vals[i] and then we are making the output b and placing b into the list of the results. Exsample 4 is also appending it  and exsample 3 is not. This is also creating a list plot from it . Where as in exsample 3 there is no list plot and we are just going from a to f(val) from val to equal a.

#Exercise 18- Iterate 3x five times with 2 as the initial value, keeping all the intermediate values.
f(x) = 3*x
count = srange(0,5)
vals = [2]
for i in count:
a = vals[i]
b = f(a)
vals.append(b)
list_plot(vals)

#Exercise 19.- Iterate cosx 10 times with 0.5 as the initial value and plot the result. Does the same thing happen for other initial values?
f(x) = cos(x)
count = srange (0, 10)
vals = [.5]
for i in count:
a = vals[i]
b = f(a)
vals.append(b)
list_plot(vals) ##### Good. -HK


#Exercise 20- Iterate 2.5 cosx 10 times with 0.5 as the initial value and plotthe result. (You may want to use the plotting optionplotjoined=Trueto seethe behavior better.) Try several initial values. Do they act similarly? You willstudy this kind of unpredictable behavior, which is called chaos, later in LS 30.

f(x) = 2.5*cos(x)
count = srange (0, 10)
vals = [.5]
for i in count:
a = vals[i]
b = f(a)
vals.append(b)
list_plot(vals)

f(x) = 2.5*cos(x)
count = srange (0, 10)
vals = [2]
for i in count:
a = vals[i]
b = f(a)
vals.append(b)
list_plot(vals)

#It  has the same behavior with going up and down although depending on the starting value it  is more spread out.

#Exercise 21-
f(x) = 2*x
#Define the function we want to iterate
#Create list of numbers defining how many times to iterate
count = srange(0,10)
#Create a list to hold the results.  The first value is 3.
vals = [3]
for i in count:
a = vals[i]
#Grab the last item in the results list
# Plug a into the function. Put the output in b.
vals.append(f(a)) # Put b into the list of results.
list_plot(vals)

#Displays the plot

#Exercise 22-
f(x) = 2*x
#Define the function we want to iterate
#Create list of numbers defining how many times to iterate
count = srange(0,10)
#Create a list to hold the results.  The first value is 3.
vals = [3]
for i in count:
#Grab the last item in the results list
# Plug a into the function. Put the output in b.
vals.append(f(vals[i])) # Put b into the list of results.
list_plot(vals)

#Displays the plot

#Exercise 23:
#I can make the program 6 lines long. This was possible by making the input ugly rather then making it  equal to a variable like x. by making it  equal to a variable you can make it  as f(x)rather then f(vals[i]).

#Exercise 24:
def iterate():
f(x) = 2*x
count = srange(0,10)
for i in count:
a = vals[i]
b = f(a)
vals.append(b)
return vals


#25
def function(number_times):
f(x) = 2*x
count = srange(0,number_times)
vals = [3]
for i in count:
a = vals[i]
b = f(a)
vals.append(b)
return vals
function(5) ##### Good. -HK

#26
def function1(start_value):
f(x) = 2*x
count = srange(0,start_value)
vals = [3]
for i in count:
a = vals[i]
b = f(a)
vals.append(b)
return vals
function(4)

#27
def descriptive_name(fcn,number_of_times,starting_value):
count = srange(0,number_of_times)
vals=[starting_value]
for i in count:
a = vals[i]
b = fcn(a)
vals.append(b)
return vals

f(x) = 2*x
descriptive_name(f,10,3)

g(x) = x^2
descriptive_name(g,5,2)

[3, 6, 12, 24, 48, 96] [3, 6, 12, 24, 48] [3, 6, 12, 24, 48, 96, 192, 384, 768, 1536, 3072] [2, 4, 16, 256, 65536, 4294967296]