CoCalc Shared FilesLab 2 / Lab2-turnin.sagews
Author: Kelly Truong
Views : 60
# Lab 2: # Name: Kelly Truong # 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. #1Enter the following code into SageMath and compare the outputs. a=5 show(a) b="5" show(b)
$\displaystyle 5$
5
#Exercise 2. What happens when you add 1 to a? To b? a+1 b+1
6
Error in lines 2-2 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1234, in execute flags=compile_flags), namespace, locals) File "", line 1, in <module> File "sage/rings/integer.pyx", line 1792, in sage.rings.integer.Integer.__add__ (build/cythonized/sage/rings/integer.c:12193) return coercion_model.bin_op(left, right, operator.add) File "sage/structure/coerce.pyx", line 1207, in sage.structure.coerce.CoercionModel.bin_op (build/cythonized/sage/structure/coerce.c:10894) raise bin_op_exception(op, x, y) TypeError: unsupported operand parent(s) for +: '<type 'str'>' and 'Integer Ring'
#b could not add 1 because it is trying to add an integer to a string #3. Give another example of a type of SageMath object for which addition is defined and explain what addition means for that type of data. #you can add together plots, and doing so puts both functions on the same plot #4. Find the types of the number 0.2 and the graph of f(x) = x^2 type(0.2) type(x^2)
<type 'sage.rings.real_mpfr.RealLiteral'> <type 'sage.symbolic.expression.Expression'>
#0.2 is type real literal and x^2 is type expression #5 A bacteria population is doubling every hour. Its sizes at differenttimes are 100, 200, 400 and 800. Make a list of these values. list1 = [100, 200, 400, 800]
#6 Assign the list of bacteria population sizes to the variablebacteria. (You can just copy and paste the list.) bacteria = list1
#7 type(bacteria)
<type 'list'>
#8 What is the value of bacteria[1]? What about bacteria[0]? First, answer without entering the command into SageMath. Then, use SageMath to check your answers. #bacteria[1]'s value is 200 and bacteria[0] is 100 bacteria[1] bacteria[0]
200 100
#9 . Append the number 1600 to bacteria and call it to display itsvalue. Don’t paste or retype any output. [100, 200, 400, 800, 1600] bacteria.append(1600) bacteria[4]
1600
#10 Exercise 10. What is the next value of the population? Append it to the list. bacteria.append(3200) bacteria[5]
3200
#11 What would happen in the example above if we did kingdoms.append(“Archaea”) twice before viewing kingdoms? Try this out and explain why you got the result that you did #the last 2 elements would be arachaea
#12 odds=[3,5,7,9,11] list_plot(odds)
#13 evens=(2,4,6,8,10) eo = zip(evens,odds) list_plot(zip(evens,odds),axes_labels=["evens","odds"])
#14 hippos=[62,81,7,90,67] crocodile = [20,34,18,25,31] zoo = zip(hippos,crocodile) list_plot(zoo,color="red",axes_labels=["hippos","crocodiles"])
#15The list in Exercise 10 gives the size of a population of bacteriaat one-hour intervals. Since one hour is 1/24 of a day, create a list of time pointsfor these observations with time in days. Then, plot a time series graph of the opulation bacteria =[100,200,400,800,1600,3200] time=[1.0/24,2.0/24,3.0/24,4.0/24,5.0/24,6.0/24] btime=zip(time,bacteria) list_plot(btime, axes_labels=["time","bacteria"]) #do axes
#16 . Plot time series of the rat’s heart rate and body temperature,using different colors. Make sure the plot uses the given time values, not just 0, 2.... and that your axes are labeled.
wt5_time =[0,0.25,0.5,0.75,1,1.25,1.5,1.75,2,2.25,2.5,2.75,3,3.25,3.5,3.75,4,4.25,4.5,4.75,5,5.25,5.5,5.75,6,6.25,6.5,6.75,7,7.25,7.5,7.75,8,8.25,8.5,8.75,9,9.25,9.5,9.75,10,10.25,10.5,10.75,11,11.25,11.5,11.75,12,12.25,12.5,12.75,13,13.25,13.5,13.75,14,14.25,14.5,14.75,15,15.25,15.5,15.75,16,16.25,16.5,16.75,17,17.25,17.5,17.75,18,18.25,18.5,18.75,19,19.25,19.5,19.75,20,20.25,20.5,20.75,21,21.25,21.5,21.75,22,22.25,22.5,22.75,23,23.25,23.5,23.75,24,24.25,24.5,24.75,25,25.25,25.5,25.75,26,26.25,26.5,26.75,27,27.25,27.5,27.75,28,28.25,28.5,28.75,29,29.25,29.5,29.75,30,30.25,30.5,30.75,31,31.25,31.5,31.75,32,32.25,32.5,32.75,33,33.25,33.5,33.75,34,34.25,34.5,34.75,35,35.25,35.5,35.75,36,36.25,36.5,36.75,37,37.25,37.5,37.75,38,38.25,38.5,38.75,39,39.25,39.5,39.75,40,40.25,40.5,40.75,41,41.25,41.5,41.75,42,42.25,42.5,42.75,43,43.25,43.5,43.75,44,44.25,44.5,44.75,45,45.25,45.5,45.75,46,46.25,46.5,46.75,47,47.25,47.5,47.75,48,48.25,48.5,48.75,49,49.25,49.5,49.75,50,50.25,50.5,50.75,51,51.25,51.5,51.75,52,52.25,52.5,52.75,53,53.25,53.5,53.75,54,54.25,54.5,54.75,55,55.25,55.5,55.75,56,56.25,56.5,56.75,57,57.25,57.5,57.75,58,58.25,58.5,58.75,59,59.25,59.5,59.75,60,60.25,60.5,60.75,61,61.25,61.5,61.75,62,62.25,62.5,62.75,63,63.25,63.5,63.75,64,64.25,64.5,64.75,65,65.25,65.5,65.75,66,66.25,66.5,66.75,67,67.25,67.5,67.75,68,68.25,68.5,68.75,69,69.25,69.5,69.75,70,70.25,70.5,70.75,71,71.25,71.5,71.75,72] wt5_temp = [35.77,37.23,37.32,36.75,36.09,35.68,35.46,35.35,35.3,35.21,35.21,35.25,35.4,35.92,36.52,36.56,36.07,35.6,35.39,35.27,35.09,34.91,34.85,34.81,34.78,34.85,34.88,34.96,35.05,34.96,34.8,34.76,34.73,34.74,35.18,35.91,36.31,36.39,36.12,35.59,35.27,35.17,35,34.69,34.53,34.85,35.49,35.98,35.89,35.34,35,35.37,36.25,36.62,36.51,36.77,37.32,37.76,37.95,38.01,38.03,38.05,38.02,37.95,37.93,37.88,37.69,37.53,37.51,37.56,37.61,37.6,37.6,37.65,37.61,37.51,37.14,36.4,35.72,35.44,35.36,35.27,35.32,35.65,36.2,36.61,36.96,37.33,37.62,37.8,37.86,37.84,37.82,37.81,37.75,37.68,37.63,37.52,37.3,36.82,36.13,35.68,35.52,35.57,35.66,35.62,35.42,35.28,35.64,36.13,36.01,35.61,35.37,35.29,35.2,35.15,35.18,35.18,35.07,35.02,35.05,35,35.02,35.1,35.02,35.08,35.49,35.89,35.78,35.38,35.11,34.96,34.97,35.09,35.07,34.85,34.69,34.99,35.45,35.38,35.02,34.81,34.83,35.34,36.18,36.83,37.14,37.06,36.5,35.94,36.06,36.65,37.23,37.66,37.86,37.84,37.73,37.68,37.67,37.63,37.48,37.3,37.45,37.7,37.78,37.8,37.78,37.71,37.68,37.75,37.75,37.69,37.64,37.48,36.91,36.01,35.3,35.09,35.12,35.21,35.41,35.84,36.59,37.13,37.42,37.64,37.71,37.77,37.91,37.96,37.92,37.87,37.76,37.59,37.27,36.75,36.08,35.53,35.23,35.05,34.95,35.06,35.26,35.35,35.39,35.26,35.13,35.37,36.06,36.48,36.11,35.6,35.31,35.09,34.94,34.96,34.97,34.89,34.88,34.96,35.07,34.99,34.77,34.62,34.67,34.85,35.08,35.5,35.63,35.25,34.9,34.8,34.81,34.85,34.87,34.84,34.92,35.35,36.03,36.64,37.08,37.23,36.85,36.07,35.64,35.75,35.89,36.23,36.96,37.56,37.87,37.97,37.98,37.93,37.81,37.65,37.58,37.62,37.49,36.99,36.27,35.89,36.29,37.03,37.51,37.71,37.77,37.76,37.74,37.79,37.85,37.8,37.78,37.68,37.45,37.31,37.2,36.81,36.15,35.69,35.62,35.64,35.5,35.35,35.4,35.93,36.72,37.25,37.47] wt5_heartrate = [331.47,410.62,463.32,480.56,473.31,459.9,452.98,454.75,461.56,470.87,484.46,505.84,534.55,562.83,578.89,574.82,552.25,520.8,491.4,470.81,460.3,457.16,456.96,456.11,453.75,451.48,451.06,452.29,453.44,453.55,454.49,461.06,478.03,505.51,536.27,558.56,563.27,549.66,525.12,499.9,481.87,475.01,480.79,498.93,525.28,549.9,560.69,552.6,533.76,520.3,523.28,540.41,560.34,573.63,578.49,578.02,574.9,570.02,564.09,558.59,554.81,552.92,552.29,552.27,552.39,552.24,551.57,550.63,550.19,550.88,552.73,555.04,556.38,554.57,547.06,532.59,512.76,491.6,473.03,459.32,452.36,455.58,472.62,503.18,540.53,574.07,595.23,601.76,597.62,589.4,582.49,579.13,578.75,579.31,578.58,574.91,567.41,556.08,541.99,527.21,514.02,503.74,496.21,490.82,488.54,492.6,505.66,525.36,543.11,549.4,541.17,523.96,506.83,495.72,491.37,491.71,494.49,497.69,498.8,495.44,487.4,478.19,473.99,480.22,497.57,520.13,537.52,540.95,529.09,508.91,490.2,479.05,476.1,479.64,488.66,501.97,515.35,521.91,517.48,506.06,498.71,505.08,525.26,549.53,565.84,568.35,560.79,553.23,554.81,567.03,582.81,592.37,590.95,581.33,569.71,560.1,552.83,546.82,542.14,540.11,541.58,545.37,548.68,549.15,546.78,543.85,543.01,545.34,549.85,554.01,554.52,548.03,532.35,508.52,481.79,459.69,448.17,449.67,464.37,491.41,526.74,561.1,583.36,587.92,578.53,564.48,553.59,548.63,548.51,551.01,554.27,556.99,557.78,554.81,546.51,532.93,516.34,499.85,485.46,473.6,464.31,458.29,456.62,460.16,469.8,486.63,509.82,533.63,548.04,545.19,526.28,501.47,481.99,472.29,469.38,467.99,465.25,461.21,457.09,454.15,453.41,455.56,461.25,471.6,487.59,507.33,524.65,532.4,528.34,516.93,504.55,494.12,485.64,481.11,486.37,506.26,538.25,571.75,593.88,596.72,580.97,555.18,532.03,522.91,532.44,556.03,582.79,602.15,609.44,606.79,599.74,593.11,588.75,585.99,583.12,578.26,569.53,556.26,541.45,532.31,535.95,552.76,574.56,590.46,594.96,590.47,583.31,578.29,576.74,577.61,579.03,579.39,577.66,573.25,565.19,551.64,530.99,504.17,475.89,452.9,440.63,441.7,457.24,487.39,528.77,572.1,605.2,619.96,616.62] heart = zip(wt5_time,wt5_heartrate) temp = zip(wt5_time,wt5_temp) list_plot(heart,color="red",axes_labels=["time","heart rate"]) list_plot(temp,color="blue", axes_labels=["time","temperature"])
#Exercise 17. Compare the plots and describe any relationships you observe. #the plots both oscillate, it looks like temperature varies more in the minimum and maximum values but it is because it has a different range compared to heart rate which is scaled bigger #18 Plot the data as a trajectory in temperature-heart rate space. ake sure to label your axes. tmphrt = zip(wt5_temp,wt5_heartrate) list_plot(tmphrt,plotjoined=true, axes_labels=["temperature","heart rate"]) #redo axes
#Exercise 19. Print out sentences listing five of your favorite books, songs or ovies using a for loop. favsong = ["cry baby", "daddy issues", "R U Mine", "why'd you only call me when you're high"] for song in favsong: print song + " is one of my favorite songs"
cry baby is one of my favorite songs daddy issues is one of my favorite songs R U Mine is one of my favorite songs why'd you only call me when you're high is one of my favorite songs
#20 Exercise 20. Use a for loop to print your name vertically. name = ["k","e","l","l","y"] for letter in name: print letter
k e l l y
#21Exercise 21. Use a for loop to square the numbers 15, 27, 39 and 84 num = [15,27,39,84] for digit in num : print digit^2
225 729 1521 7056
#22Make a list containing the first five multiples of 3. mult = [3,6,9,12,15]
#23 Write a loop that makes a list of the squares of the numbers 0.1,0.2, ..., 0.7. Then, plot your list. newsq = [] sq = [0.1,0.2,0.3,0.4,0.5,0.6,0.7] for deci in sq : newsq.append(sqrt(deci)) list_plot(newsq)
#24 Create a function and apply it to the numbers 0 through 5,inclusive. Plot the list of resulting values. exp = [] for x in [0,1,2,3,4,5]: exp.append(x^2); list_plot(exp)
#25 The time in wt5_time is measured in hours. Create another list n which it is given in minutes. min = [] for x in wt5_time: min.append(x/60.0)
#Exercise 26. Convert the temperatures in wt5_temp from Celsius to Fahrenheit. (The formula is F =9/5C + 32.) cel = [] for x in wt5_temp: cel.append((9/5)*x+32)
#Exercise 27. Plot a time series of your transformed data. metric = zip(min,cel) list_plot(metric)
#28 Exercise 28. Plot a trajectory of the transformed temperature data and the riginal heart rate data traj = zip(cel,wt5_heartrate) list_plot(traj, plotjoined=true)
#29 Change the animation in Example 8 to make the line green rather han blue plots = [] slopes = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5] for m in slopes: p=plot(m*x, (x,-10,10), ymin=-50, ymax=50,color="green") plots.append(p) a=animate(plots) show(a)
#Exercise 30. Change the previous animation to make the slope range from -3 o 3 in steps of 0.5. plots = [] slopes = [-3,-2.5,-2,-1.5,-1,-0.5,0,0.5,1,1.5,2,2.5,3] for m in slopes: p=plot(m*x, (x,-3,3), ymin=-50, ymax=50, color="green") plots.append(p) a=animate(plots) show(a)
#Exercise 31. Rewrite the animation in Exercise 29 so that the slope of the lineplotted is always 1 but the y-intercept ranges between -5 and 5. plots = [] yint = [-5,-4,-3,-2,-1,0,1,2,3,4,5] for b in yint: p=plot(1*x+b, (x,-10,10), ymin=-50, ymax=50) plots.append(p) a=animate(plots) show(a)
#further exercises #1. Animate xn for values of x ranging between 0 and 5 and values of n ranging between 2 and 3 in steps of 0.1 or less. (Smaller step sizes make better-looking animations but take longer to generate.) plots=[] n=[2,2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,3] for exp in n: p=plot(x^exp,(x,0,5)) plots.append(p) a=animate(plots) show(a)
#22. Pick a function that has a parameter you can modify and use both an animation and an interactive to show the results of modifying it. @interact def expnt(m=(0,6)): p= plot(x*m+5, (x,-5,5)) show(p) plots=[] m=[1,2,3,4,5,6] for slp in m: b=plot(x*slp+5,(x,0,5)) plots.append(b) bee=animate(plots) show(bee)