Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Tutorial for Lab 3, LS30A (Fall 2020)

Views: 396
License: MIT
Image: ubuntu2004
Kernel: SageMath 9.1

Lab 3 Using Lists and Loops

Example 1: Plotting lists of points using list_plot()

# height over time height=[61, 62, 66, 68, 70] ages = [10, 12, 14, 16, 17] list_plot(height)
Image in a Jupyter notebook
# plot list of pairs of points pairs = [(10, 61), (12, 62), (14, 66), (16, 68), (17, 70)] list_plot(pairs, size=50, color='green', axes_labels=["ages [yr]", "height [in]"], title="My height over time")
Image in a Jupyter notebook
pairs = list(zip(ages, height)) # replacement for typing the above. list_plot(pairs, size=50, color='green', axes_labels=["ages [yr]", "height [in]"], title="My height over time")
Image in a Jupyter notebook
# Convert inches to m height # create an empty list to hold the metric heights metric_heights = [] # repeatedly, for each item in the list height... for h in height: # convert height to meters h_meters = h*0.0254 # append converted height to metric list metric_heights.append(h_meters) # display converted time series. list_plot(list(zip(ages, metric_heights)), size=50, color='green', axes_labels=["ages [yr]", "height [m]"], title="My height over time")
Image in a Jupyter notebook

Example 2: Processing lists of data using for loops

Example 3: editing a trajectory

Pre-created lists xlist and ylist:

times = list(srange(0, 2*pi, 0.1)) xlist = [cos(t) for t in times] ylist = [sin(t) for t in times] print("xlist = {}...".format(xlist[:5])) print("ylist = {}...".format(ylist[:5]))
xlist = [1, 0.995004165278026, 0.980066577841242, 0.955336489125606, 0.921060994002885]... ylist = [0, 0.0998334166468282, 0.198669330795061, 0.295520206661340, 0.389418342308651]...
old_circle = list_plot(list(zip(xlist, ylist)), aspect_ratio=1) show(old_circle)
Image in a Jupyter notebook
new_cos_list = [] for x in xlist: new_cos_list.append(2*x) new_cos_list new_sin_list = [] for y in ylist: new_sin_list.append(2*y) new_sin_list new_circle = list_plot(list(zip(new_cos_list, new_sin_list)), aspect_ratio=1, color='red', plotjoined=True) # overlay both: new_circle + old_circle
Image in a Jupyter notebook