import numpy as np
import matplotlib.pyplot as plt
= np.linspace(0, 4*np.pi, 50)
x = np.sin(x)
y
plt.plot(x, y)'x');
plt.xlabel('sin(x)');
plt.ylabel(1) plt.grid(
38 Problem class 1: an introduction to Noteable
During this module, we will perform various computational experiments using Python. In order to make it easy for students to code in Python from a variety of devices (desktops, laptops, tablets, and even a phone), we use a cloud-computing system called Noteable that allows you to code from within a web browser. Students who like to code using their own Python setup on their computer can also do so. The goal of this problem class is to quickly get you up to speed with the basic Noteable system, and how to manipulate outputs. We should cover:
- A walkthrough of setting up with Noteable
- Basic plotting using matplotlib and numpy
- How to export graphics and worksheets
38.1 Getting started with Noteable
Use your Moodle course page to access the Noteable Python interface by clicking on the appropriate link in the course materials. Follow the instructions on the Moodle page to add the Git repository to your directory.
Navigate to the welcome screen for MA30287 and follow the instructions in
Welcome.ipynb
andFirstTimeSetup.ipynb
in order to create your own local directory at/MA30287_workspace/
Navigate to the workspace folder. Click
New -> Notebook
. If asked, select the kernelPython 3 (ipykernel)
.In the first line of input, select, in toolbar
Code -> Markdown
. This allows you to annotate your notebook with Markdown-style text input.In the first line of input, type
# Problem class 1
. Then either typeShift + Enter
or press the play button which will execute the line(s) of input. Your markdown text should render as a nicely formatted entry.Rename the file to something appropriate, like
problemclass01
. You can do this by right-clicking the filename in the file manager and selectingRename
.Proceed to the next question.
38.2 Mathematical plotting
Three key Python packages used throughout this course are numpy, matplotlib, and scipy. Numpy
provides array functionality, allowing you to naturally manipulate vectors and matrices. Matplotlib
is a general plotting package for Python, allowing for the creation of (typically) 2D and 3D plots. And scipy
provides algorithms for differential and integral operations allowing e.g. the solution of differential equations.
Here is a script to generate a graph of a sine curve.
Here is a script to generate a 3D plot.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Define the function
def f(x, y):
return np.sin(np.sqrt(x**2 + y**2)) * np.exp(- (x**2 + y**2) / 10)
# Create grid
= np.linspace(-5, 5, 50)
x = np.linspace(-5, 5, 50)
y = np.meshgrid(x, y)
X, Y = f(X, Y)
Z
# Plot the surface
= plt.figure(figsize=(8, 6))
fig = fig.add_subplot(111, projection='3d')
ax ='viridis')
ax.plot_surface(X, Y, Z, cmap
# Labels and title
"x")
ax.set_xlabel("y")
ax.set_ylabel("z")
ax.set_zlabel( plt.show()
38.3 Debugging Jupyter code
It would be good to demonstrate a workflow to understand how errors are diagnosed and studied (e.g. by printing simple variables throughout the script). Also, it would be good to show students how graphs can be zoomed in/out and/or rotated within the browser interface (this may be tricky in Noteable).
38.4 Outputting and saving
During the problem class, we will also show how to:
- Create a zip file of your workspace using the terminal so that you can save a copy of your files onto your local filesystem. This can be done by running the script
ExportMA30287.ipynb
in the root directory of the Noteable system, and then downloading the result. - Create a copy of your script in PDF form so that it can be sent around as a single document. This can be done by going to File -> Save and Export Notebook As…
38.5 Challenge coding exercise
Create an animation of the 2D or 3D plot, e.g. by making it so the waves ‘ripple’. Using a for
loop, animate the wave. Then output the animation as a video file.