A1: Notebooks

Assignment 1: Jupyter Notebooks, LaTeX, Julia & Prerequisites
Published: Wed 9 Sept

Note:
  • Complete the following and submit to Canvas before Wed 16 Sept, 23:59.
  • Late work will receive 0%.
  • Each assignment is worth the same.
  • Please get in contact with the TA/instructor in plenty of time if you need help.
  • Before submitting your work, make sure to check everything runs as expected. Click Kernel > Restart Kernel and Run All Cells. Submit your notebook in .pdf format by clicking File > Export Notebook As > PDF or by printing to PDF in the browser.
  • Feel free to add more cells to experiment or test your answers.
  • I encourage you to discuss the course material and assignment questions with your classmates. However, unless otherwise explicitly stated on the assignment, you must complete and write up your solutions on your own.
  • The use of GenAI is prohibited as outlined in the course syllabus. If suspected of cheating, you may be asked to complete a written or oral exam on the content of this assignment.

A: Jupyter Notebooks

Tip: You may do this part with the help from your classmates

Exercise 1.

  • Follow the steps here to install Julia and Jupyter,
  • Upload and run this .ipynb file,
  • Double click on the following cell to replace YOUR NAME HERE with your name
Code
Name = "YOUR NAME HERE"
"YOUR NAME HERE"

B: \(\LaTeX\)

Exercise 2. Taylor expansion

  1. Write down the degree \(n\) Taylor polynomial of \(f:\mathbb R \to \mathbb R\) about some point \(a\).

Answer.

\[\begin{align} T_nf(x) = .... \nonumber \end{align}\]

  1. Write down your favourite form of the remainder \(R_n(x) := f(x) - T_nf(x)\)

Answer.

\[\begin{align} R_n(x) = .... \nonumber \end{align}\]

  1. Consider the function \(f(x) := \frac{e^x - 1}{x}\). Use Taylor expansion of \(e^x\) to evaluate \(\lim_{x\to 0} f(x)\).

Answer.

C. Julia

Code
using Plots

Exercise 3. Archimedes’ method for computing (lower bounds to) \(\pi\).

Recall that \(\pi\) is half the circumference of the unit circle. Let \(P_n\) be the inscribed regular \(n\)-gon inside the unit circle and notice that its perimeter satisfies \(2 \pi_n \leq 2 \pi\).

  • ✍️ Explain why \(\pi_n = n \sin \frac{\pi}{n}\),
  • 💻 What is the absolute error when approximating \(\pi\) with \(\pi_{2026}\)?
  • ✍️ Use Taylor expansion of \(\sin\) to explain why \((\pi_n)\) converges with rate \(O(n^{-2})\),
  • 💻 Verify this rate of convergence with a plot.
  • ✍️ For what \(n\) is \(\pi_n\) equal to \(\pi\) up to the first \(2026\) significant decimal digits?

Answer.

Code
# your answer here

Exercise 4. Subtractive Cancellation

Take \(f(x) := \frac{e^x - 1}{x}\). Implementing this function directly is a bad idea due to subtractive cancellation (we will go through this in detail in week 2). Julia has the built-in function expm1 which uses the Taylor expansion of \(e^x\) (as you did in Exercise 2) to evaluate \(e^x-1\) without subtractive cancellation.

Here are two ways of evaluating \(f(10^{-15})\):

Code
g(x) = (exp(x)-1)/x    
h(x) = expm1(x)/x

g_x, h_x = g(1e-15), h(1e-15)
(1.1102230246251565, 1.0000000000000007)

What are the relative errors between the mathematical value of \(f(10^{-15})\) and the approximations g_x (naive formula) and h_x (Taylor expansion).

You may use y defined in the code below as the “exact” value of \(f(10^{-15})\) (which is correct to the first 100 significant digits).

Code
y = 1.000000000000000500000000000000166666666666666708333333333333341666666666666668055555555555555753968

# YOUR CODE HERE
1.0000000000000004

D. Reflection

Goal: Reflect on your learning strategies, track your workload, and help us improve future course material.
Format: Short answer (Graded on completion: Complete / incomplete).
Time: take 5-10 mins (this shouldn’t take too long)

Estimate the approximate hours you spent:
* Assignment: ____
* Reading notes/materials: ____
* Exercises: ____

(Reminder: you should be spending around 8 hours per week on this course outside of the 4 hours of lectures)

Any other comments:

Take a moment to reflect on your learning over the past week:

What was the easiest topic or task and why? Where did you struggle, and how did you overcome those challenges? Which resources (e.g. lecture notes, practice exercises, peer discussions, …) helped you the most? How did you manage your time and workload (e.g., all at once, spread out over several days, or working with peers)? Looking back, what would you do differently if you were starting this week over today?

Comments:

(Optional) Any changes that would improve the lectures, exercises, or assignments for the future? You can also suggest things anonymously here (opens in a new tab)

Comments:

(E: Bonus exercises - not for credit)

Catch up with the exercises from the lectures. If you have time, think about the following questions:

In Exercise 4, \(y\) is defined as \(y = 1.0000000000000005....\) but the output is \(y = 1.0000000000000004\). Why? How should you implement numbers with higher precision?

Think about why the following method for computing \(f(10^{-15})\) does not suffer from subtractive cancellation:

Code
j(y) = (y-1)/log(y) 
j_x = j(exp(1e-15))
1.0000000000000004
Timer
00:00
min