1  Week 1 - Intro to Numerical Analysis

For this course, there will be a weekly participation notebooks. During each class session, you will have time to complete portions of these notebooks. The goal is for you to complete at least 90% of the notebook in class and this should very rarely be an additional ‘homework’ assignment. If you miss class, it is your responsibility to complete the notebook, ask questions, and turn it in by Friday.


To begin all assignments (whether participation or homework), please save a copy of this notebook to your Google Drive by clicking the Google Colab buttom at the top of this notebook. Be sure to check Canvas for due dates and reminders.


1.1 Philosophies for this Course

  • TIMTOWDI (Tim Toady)
  • Write Working Code First, Optimize Second

1.1.1 Best practices

  1. For coding environments, provide detailed comments for interpretability and reproducibility. This will also help the future you when you come back to past assignments/code.

  2. Read the error message when your code does not work. They contain important information on how to debug your code.

  3. If you do something more than twice, automate that action.

  4. Write code that works first, then optimize!

  5. Notebooks can be split if they get too long. You want to be able to come back to a notebook without too much trouble and searching.

1.2 Introduction to Python and Colab

This introduction to Jupyter Notebook is based on tutorials developed by Jessica Hamrick.

The notebook consists of a series of cells. For example, this text is in what is called a “Markdown cell”.

These cells support \(\LaTeX\) and a variety of other features (see toolbar above).

Practice: Add a Markdown cell below and use at least 3 of the options from the toolbar (for example, bold, inserting a link, a picture, LaTeX, etc.)

The following cell is a “code cell”:

# this is a code cell
# this is a comment - comments are for you or for anyone else reading your work

# multiply example
29 * 21

# division
28/7

1.2.1 Executing cells

Code cells can contain any valid Python code in them. When you run the cell, the code is executed and any output is displayed.

You can execute cells with Ctrl-Enter (which will keep the cell selected), or Shift-Enter (which will select the next cell).

Try running the following cell and see what it prints out:

# print() is a function that prints what is in the parentheses (usually in quotes)
print("Printing the numbers from 1-10:")

# this is a for loop which repeats the command inside of the indented space
for i in range(1, 11):
  # print using f strings
  print(f"{i}")
# print another message
print("Done printing numbers.")

Practice: Copy and paste the code from above into the code cell below and change the numbers in the parentheses of range. What do you observe?

1.2.2 Python Kernel

When you first start a notebook, you are also starting what is called a kernel. This is a special program that runs in the background and executes code (by default, this is Python, but it could be other languages too, like R!). Whenever you run a code cell, you are telling the kernel to execute the code that is in the cell, and to print the output (if any).

Just like if you were typing code at the Python interpreter, you need to make sure your variables are declared before you can use them. What will happen when you run the following cell? Try it and see:

a

What does the error tell you?

Let’s modify the cell above so that a is declared first.

# here, we define a
a = 4
a

Now let’s write a comment on the code cell above.

Some random/built-in functions are

  • abs()
  • help()
  • print()
# if I wanted more about abs(), I could use the help() function
help(abs)

Practice: What are some other built-in functions in Python (feel free to look them up)? Get help on one of the ones that are not listed above.

abs(-4)
# other functions people searched
# ascii, bin, sum

1.3 Intro to Numerical Analysis

Practice

By hand (no computers!) compute the first 12 terms of this sequence with the initial condition \(x_0 = 1/10\).

\[ x_{n+1} = \left\{ \begin{array}{ll} 2x_n, & x_n \in [0,\frac{1}{2}] \\ 2x_n - 1, & x_n \in (\frac{1}{2},1] \end{array} \right. \]

\[ 1/10, 2/10, 4/10, 8/10, 6/10, 2/10, 4/10, 8/10, 6/10, 2/10, \]

Practice

Now use a spreadsheet and to do the computations. Do you get the same answers?

[ Put your response here + a link to your Google Sheet ]

Practice Finally, solve this problem with Python. Some starter code is given to you below.

# define x
x = 1.0/10
# calculate the first 50 terms using this rule
for n in range(50):
  # check if the first condition is met
    if :
        # put the correct assignment here
        
  # if the first condition is not met, do this
    else:
        # put the correct assigment here
    print(x)

Practice

What do you notice? What do you think happened on the computer and why did it give you a different answer? What, do you suppose, is the cautionary tale hiding behind the scenes with this problem?

What I noticed is. …

Practice

Now what happens with this problem when you start with \(x_0 = 1/8\)?

1.4 Arithmetic in Base 2

Practice

Express the following binary numbers in base-10.

    1. \(111_2\)
    1. \(10,101_2\)

Practice

Discussion: With your group discuss how you would convert a base-10 number into its binary representation. Once you have a proposed method, apply it to the number \(237_{10}\) who’s base-2 expression is \(11,101,101_2\)?

Practice

Convert the base 10 fraction \(1/10\) into binary. Use your solution to fully describe what went wrong in problems

# calculating binary of a number + bit size of a variable
a = 7
bin(a)
a.bit_length()