CoCalc Shared FilesLab 3 / Lab3-turnin.sagewsOpen in CoCalc with one click!
Author: alli hinmon
Views : 6
# Lab 3: # Name: Alexandria Hinmon # I worked on this code with: # Please do all of your work for this week's lab in this worksheet. If # you wish to create other worksheets for scratch work, you can, but # this is the one that will be graded. You do not need to do anything # to turn in your lab. It will be collected by your TA at the beginning # of (or right before) next week’s lab. # Be sure to clearly label which question you are answering as you go and to # use enough comments that you and the grader can understand your code.
# 1 g(x)=19*x^3 g(0) g(10) g(-4)
0 19000 -1216
# 2 f(x)=5*x+3*x+5 f(1) f(2) f(3)
13 21 29
# 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]
# 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]
# 5 nums=srange(0,11) for n in nums: n
0 1 2 3 4 5 6 7 8 9 10
# 6 roots=srange(10.,41.,10) for n in roots: sqrt(n)
3.16227766016838 4.47213595499958 5.47722557505166 6.32455532033676
mult4=[] nums=srange(1,11) for n in nums: mult4.append(4*n) mult4
[4, 8, 12, 16, 20, 24, 28, 32, 36, 40]
# 7 # The line mult3.append(3*n) takes a number from srange (1,2,3,4, or 5) and multiplies it by 3. It then adds the new value to the list mult3.
# 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]
# 9 f(x)=5*x+3*x+5 result=[] nums=srange(1,6) # This will put 5 numbers into my function. for n in nums: result.append(f(n)) # This adds the number into my empty list. result
[13, 21, 29, 37, 45]
# 10 list_plot(result, axes_labels=["X","Y"], size=30)
# 11 # The final value is 3072. f(x)=2*x count=srange(0,10) val=3 for i in count: a=f(val) val=a val
3072
# 12 # the line val=a to continue the iteration. It is the line that physically makes the output of the current input the next input.
# 13 f(x)=2*x count=srange(0,20) val=3 for i in count: a=f(val) val=a val
3145728
# 14 f(x)=2*x count=srange(0,20) val=5 for i in count: a=f(val) val=a val
5242880
# 15 g(x)=3*x count=srange(0,5) val=2 for i in count: a=g(val) val=a val
486
# 16 f(x)=2*x count=srange(0,10) val=3 for i in count: val=f(val) val
# 17 # In Example 3, no list is made that records each step. When the cell is executed, only the final value is displayed. In Example 4, since a list is used, it places every value calculated at each step into the list, and prints that list when the cell is executed.
# 18 f(x)=3*x count=srange(0,5) vals=[2] for i in count: a=vals[i] b=f(a) vals.append(b) vals
[2, 6, 18, 54, 162, 486]
# 19 # When the initial value is changed, the plot still shows osciallatory behavior, with the points going up and down, and funneling closer to one point. However, the individual values of the points change depending on the intial value. f(x)=cos(x) count=srange(0,10) vals=[0.5] for i in count: a=vals[i] b=f(a) vals.append(b) list_plot(vals, axes_labels=["X","Y"],size=30)
f(x)=cos(x) count=srange(0,10) vals=[1] for i in count: a=vals[i] b=f(a) vals.append(b) list_plot(vals, axes_labels=["X","Y"],size=30)
f(x)=cos(x) count=srange(0,10) vals=[0.75] for i in count: a=vals[i] b=f(a) vals.append(b) list_plot(vals, axes_labels=["X","Y"],size=30)
# 20 # Each initial value affects the behavior of the graph. While all three graphs oscillate up and down, the sixes of the peaks and troughs is different for each graph, showing that the initial value influences the behavior of the graph. f(x)=2.5*cos(x) count=srange(0,10) vals=[0.5] for i in count: a=vals[i] b=f(a) vals.append(b) list_plot(vals, axes_labels=["X","Y"],plotjoined=True)
f(x)=2.5*cos(x) count=srange(0,10) vals=[0.75] for i in count: a=vals[i] b=f(a) vals.append(b) list_plot(vals, axes_labels=["X","Y"],plotjoined=True)
f(x)=2.5*cos(x) count=srange(0,10) vals=[1] for i in count: a=vals[i] b=f(a) vals.append(b) list_plot(vals, axes_labels=["X","Y"],plotjoined=True)
# 21 f(x)=2*x # This defines a function 2*x. count=srange(0,10) # This creates a list of numbers that is how many times the iteration will be done. vals=[3] # This is the initial value for i in count: b=f(vals[i]) # This puts the last value in the list into the function, and names that value b. vals.append(b) # This adds b to the end of the list. list_plot(vals, plotjoined=True, axes_labels=["X","Y"],color="purple") # This plots the values of the steps of the iteration.
# 22 count=srange(0,10) # This creates a list of numbers that is how many times the iteration will be done. vals=[3] # This is the initial value for i in count: b=2*(vals[i]) # This puts the last value in the list into the function by providing the function in the line, and names that value b. vals.append(b) # This adds b to the end of the list. list_plot(vals, plotjoined=True, axes_labels=["X","Y"],color="purple") # This plots the values of the steps of the iteration.
# 23 vals=[3] # This is the initial value for i in srange(0,10): vals.append(2*(vals[i])) # This puts the last value in the list into the function, names that value b, and adds b to the end of the list. list_plot(vals, plotjoined=True, axes_labels=["X","Y"],color="purple") # This plots the values of the steps of the iteration.
# 24 def iterate(): # This defines a function called iterate that takes no inputs. vals=[3] # This sets the initial value to 3. count=srange(0,10) # This makes 10 iterations. for i in count: vals.append(2*(vals[i])) # This takes number at index i in the list and puts it into the function and then adds it to the end of the list. return vals iterate()
[3, 6, 12, 24, 48, 96, 192, 384, 768, 1536, 3072]
# 25 def iterate(numofiterate): # This defines a function that takes "numofiterate", which is the number of iterations. vals=[3] # This sets the initial value to 3. for i in srange(0,numofiterate): #This is how many iterations will be done, and will change depending on the number that is put into iterate when it is called. vals.append(2*(vals[i])) # This takes number at index i in the list and puts it into the function and then adds it to the end of the list. return vals iterate(10)
[3, 6, 12, 24, 48, 96, 192, 384, 768, 1536, 3072]
iterate(5)
[3, 6, 12, 24, 48, 96]
iterate(4)
[3, 6, 12, 24, 48]
# 26 def iterate(numofiterate,startvalue): # This defines a function that takes "numofiterate", which is the number of iterations, and "startvalue", which is the beginning number. vals=[startvalue] # This is the starting value, and changes depending on what is input into iterate. for i in srange(0,numofiterate): # This is the number of iterations, and changes depending on what is input into iterate vals.append(2*(vals[i])) # This takes number at index i in the list and puts it into the function and then adds it to the end of the list. return vals iterate(10,3) iterate(5,4) iterate(3,5)
[3, 6, 12, 24, 48, 96, 192, 384, 768, 1536, 3072] [4, 8, 16, 32, 64, 128] [5, 10, 20, 40]
# 27 def iterate(function,numofiterate,startvalue): # This defines a function that takes "numofiterate", which is the number of iterations, "startvalue", which is the beginning number, and "function" which is the function used for the iteration. vals=[startvalue] # This is the starting value, and changes depending on what is input into iterate. for i in srange(0,numofiterate): # This is the number of iterations, and changes depending on what is input into iterate. vals.append(function(vals[i])) # This takes number at index i in the list and puts it into the function and then adds it to the end of the list. return vals iterate(3*x,5,2)
[2, 6, 18, 54, 162, 486]