CoCalc Shared FilesLab 3.sagews
Views : 42
##### Group 6 Grade: 18.5/20
#lab 3
#exercise 1
g(x) = 19*x^3
g(0)
g(10)
g(-4)


0 19000 -1216
#exercise 2
f(x) = 13*x^2 ##### Good. -HK
f(3)
f(5)
f(7)

117 325 637
#exercise 3
srange (1,101)
#exercise 4
srange (0,51)


[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] [0, 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]
#exercise 5
srange(1,11)    iterate()


[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#exercise 6
nums = srange(10.0,41.0,10.0)
for n in nums:
sqrt(n)

3.16227766016838 4.47213595499958 5.47722557505166 6.32455532033676
#exercise 7
# mult3.append(3*n) creates a the framework for the list, it tells it what will go into the list

#exercise 8
mult4=[]
nums=srange (1,11)
for n in nums:
mult4.append(4*n)
mult4

[4, 8, 12, 16, 20, 24, 28, 32, 36, 40]

#exercise 9
multE2=[]
nums=[0,25,50,75,100,150]
for n in nums:
multE2.append(13*n^2)
multE2

[0, 8125, 32500, 73125, 130000, 292500]

#exercise 10
list_plot(multE2)

Error in lines 1-1 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> File "/ext/sage/sage-8.9_1804/local/lib/python2.7/site-packages/sage/misc/decorators.py", line 493, in wrapper return func(*args, **options) File "/ext/sage/sage-8.9_1804/local/lib/python2.7/site-packages/sage/plot/plot.py", line 3010, in list_plot data = [(z.real(), z.imag()) for z in [CC(z[1]) for z in data]] File "/ext/sage/sage-8.9_1804/local/lib/python2.7/site-packages/sage/rings/complex_field.py", line 387, in __call__ return Parent.__call__(self, x) File "sage/structure/parent.pyx", line 900, in sage.structure.parent.Parent.__call__ (build/cythonized/sage/structure/parent.c:9197) return mor._call_(x) File "sage/structure/coerce_maps.pyx", line 162, in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4556) raise File "sage/structure/coerce_maps.pyx", line 157, in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4448) return C._element_constructor(x) File "/ext/sage/sage-8.9_1804/local/lib/python2.7/site-packages/sage/rings/complex_field.py", line 426, in _element_constructor_ return x._complex_mpfr_field_( self ) File "sage/symbolic/expression.pyx", line 1366, in sage.symbolic.expression.Expression._complex_mpfr_field_ (build/cythonized/sage/symbolic/expression.cpp:10682) return self._eval_self(R) File "sage/symbolic/expression.pyx", line 1227, in sage.symbolic.expression.Expression._eval_self (build/cythonized/sage/symbolic/expression.cpp:10001) raise TypeError("Cannot evaluate symbolic expression to a numeric value.") TypeError: Cannot evaluate symbolic expression to a numeric value.
#exercise 11
f(x) = 2*x #Define the function we want to iterate
count = srange(0,10) #Create list of numbers defining how many times to iterate
val=3 #val is the variable that will hold our intermediate #calculations.  We start with val being 3.
for i in count:
a = f(val)
val = a #Plug val into f and assign the result to val
val #Show the final result
#the final result is 3072


6 12 24 48 96 192 384 768 1536 3072
#exercise 12
#without val=a each output is 3 meaning this line makes the next input the output of the last function.
f(x)=2*x  #Define the function we want to iterate
count = srange(0,10) #Create list of numbers defining how many times to iterate
val=3  #val is the variable that will hold our intermediate #calculations.  We start with val being 3.
for i in count:
a = f(val)
val=a  #Plug val into f and assign the result to val
val  #Show the final result

6 12 24 48 96 192 384 768 1536 3072
#excercise 13
f(x)=2*x   #Define the function we want to iterate
count = srange(3,20)  #Create list of numbers defining how many times to iterate
val=3   #val is the variable that will hold our intermediate #calculations.  We start with val being 3.
for i in count:
a = f(val)
val=a  #Plug val into f and assign the result to val
val #Show the final result

6 12 24 48 96 192 384 768 1536 3072 6144 12288 24576 49152 98304 196608 393216
#exercise 14
f(x)=2*x  #Define the function we want to iterate
count = srange(5,20)  #Create list of numbers defining how many times to iterate
val=3  #val is the variable that will hold our intermediate #calculations.  We start with val being 3.
for i in count:
a = f(val)  #Plug val into f and assign the result to val
val=a
val #Show the final result

6 12 24 48 96 192 384 768 1536 3072 6144 12288 24576 49152 98304
#exercise15
g(x) = 3*x #Define the function we want to iterate
count = srange(0,5) #Create list of numbers defining how many times to iterate
val=2 #val is the variable that will hold our intermediate #calculations.  We start with val being 2
for i in count:
a = f(val) #Plug val into f and assign the result to val
val=a
val#Show the final result

4 8 16 32 64
#exercise 16
f(x) = 2*x #Define the function we want to iterate
count = srange(0,10) #Create list of numbers defining how many times to iterate
val=3  #val is the variable that will hold our intermediate #calculations.  We start with val being 3.
for i in count:
val = f(val) #Plug val into f and assign the result to val
val#Show the final result

3072
#exercise 17
#The difference between example three and four is that in three, we made val be three. In example four, we are still assigning three to val but in addition will create a list to hold the results, so we place brackets around three. Also in >>>for i in count, instead of having just a=f(val) and val = a (ex three) we have changed it to a=vals[i] which grabs the last item in the results list and then plugging a into the function and placing the b as the output (b=f(a))and then place b into the list of results by placing val.append(b) and append b to the list.

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

#exercise 19
f(x) = cos(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 0.5.
vals = [0.5]
for i in count:
a = vals[i] #Grab the last item in the results list
b = f(a) # Plug a into the function.  Put the output in b.
vals.append(b) # Put b into the list of results.
list_plot(vals) # Plot the results
##### Good. -HK

#exercise 20
f(x) = 2.5*cos(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 = [0.5]
for i in count:
a = vals[i] #Grab the last item in the results list
b = f(a) # Plug a into the function.  Put the output in b.
vals.append(b) # Put b into the list of results.
list_plot(vals) # Plot the results
plotjoined=True
f(x) = 2*cos(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 = [0.5]
for i in count:
a = vals[i] #Grab the last item in the results list
b = f(a) # Plug a into the function.  Put the output in b.
vals.append(b) # Put b into the list of results.
list_plot(vals) # Plot the results
plotjoined=True
#with different initial values, it causes a change in the value of the element, changing only the y axis for the element but not x axis.

#exercise 21
f(x) = 2*x # stating the function we want to iterate.
count = srange(0,10) # Create a list saying how many times to iterate and to hold the results.
vals = [3] #this makes the first value 3.
for i in count:
b = f(vals[i]) # This grabs the last result of the last line, plugs it into the function, puts the output in b.
vals.append(b) # puts b into a list of results
list_plot(vals) # Plot the result

#exersice 22
f(x) = 2*x #stating the function we want to iterate.
count = srange(0,10) #create a list saying  how many times to iterate and to hold the results.
vals = [3] #this makes the first value 3
for i in count:
vals.append(f(vals[i])) #This will grabs the last result of the last line and plugs it into the function, putting the output in b and from that placing b in the list of results
list_plot(vals) #plot the result

#Exercise 23
f(x) = 2*x # defines the function
vals = [3] # makes the first value 3
for i in srange(0,10): #For i in srange, it create a list saying  how many times to iterate and to hold the results
vals.append(f(vals[i])) #This will grabs the last result of the last line and plugs it into the function, putting the output in b and from that placing b in the list of results
list_plot(vals) # plot the result.

#exercise 24
def iterate():
f(x) = 2*x # define the function we plan to iterate
count = srange(0,10) # create list of numbers defining how many times to iterate
vals = [3]  # initialize the list to hold the results with the starting value
for i in count:
a = vals[i]  # grab the last item in the output list
b = f(a) # plug it into the function
vals.append(b) # Put b into the list
return vals
iterate()

[3, 6, 12, 24, 48, 96, 192, 384, 768, 1536, 3072]
#Exercise 25
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
f(x)=2*x
iterate(f,10,3) ##### number_of_times should be the only input for this problem. (-1.5 points)

[3, 6, 12, 24, 48, 96, 192, 384, 768, 1536, 3072]
()
#Exercise 26
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
f(x)=2*x
iterate(f,5,0)

[0, 0, 0, 0, 0, 0]
#exercise 27
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
f(x)=2*x
iterate(f,10,3)
g(x)=x^2
iterate (g,5,2)
d(x)=sqrt(x)
iterate (d,15,2)

[3, 6, 12, 24, 48, 96, 192, 384, 768, 1536, 3072] [2, 4, 16, 256, 65536, 4294967296] [2, sqrt(2), 2^(1/4), 2^(1/8), 2^(1/16), 2^(1/32), 2^(1/64), 2^(1/128), 2^(1/256), 2^(1/512), 2^(1/1024), 2^(1/2048), 2^(1/4096), 2^(1/8192), 2^(1/16384), 2^(1/32768)]