Before you turn this problem in, make sure everything runs as expected. First, restart the kernel (in the menubar, select Kernel → Restart) and then run all cells (in the menubar, select Cell → Run All).

Make sure you fill in any place that says BEGIN SOLUTION and END SOLUTION, as well as your name and collaborators below:

[ ]:
NAME = ""
COLLABORATORS = ""

2025-09-05 Taylor Series

Suppose we wish to sum the series

\[\sum_{n = 1}^{\infty} \frac{1}{n^2} = 1 + \frac{1}{2^2} + \frac{1}{3^2} + \frac{1}{4^2} + \cdots\]

We cannot sum an infinite number of terms, but we can sum the first \(k\) terms and see what happens as \(k\) increases. In other words, we will compute

\[y_k = \sum_{n = 1}^{k} \frac{1}{n^2}\]

and estimate what happens in the limit as \(k \rightarrow \infty\).

[ ]:
using Plots

function series_n2(k)
    sum = 0
    for n in 1:k
        sum += 1 / n^2
    end
    sum
end

series_n2(10)

This series converges to \(\pi^2 / 6\). (Proof is beyond the scope of this course.)

How fast does the series converge?

[ ]:
known_limit = π^2 / 6
ks = 2 .^ (0:10) # [1, 2, 4, ..., 1024]
plot(ks, [series_n2, k -> known_limit], label=["partial sum" "limit"])
[ ]:
# Lets swap to a log scale
plot(ks, [series_n2, k -> known_limit], xscale=:log10, label=["partial sum" "limit"])

Error

If we have an approximation \(y_k\) of an exact value \(y_*\), then we can compute the absolute error as

\[y_k - y_*\]
[ ]:
# Plot error
plot(ks,  k -> series_n2(k) - known_limit, xscale=:log10, label="absolute error")
[ ]:
# Hmm, let's compare to a reference line
plot(ks,
    [k -> series_n2(k) - known_limit, k -> 1/k],
    xscale=:log10,
    label=["absolute error" "1/k"]
)

Conclusion?

This plot suggests that the error is proportional to \(1 / k\), or

\[\lvert \sum_{n = 1}^{k} \frac{1}{n^2} - \pi^2 / 6 \rvert \sim \frac{1}{k}\]

Notation

The notation \(f \sim g\) means that

\[a g \left( x \right) \leq f \left( x \right) \leq b g \left( x \right)\]

for two positive numbers \(a\) and \(b\) in some asymptotic regime (\(x\) is sufficiently large or small).

In this case, the ‘asymptotic regime’ is large \(k\) (the variable ‘\(x\)’ for this series), but sometimes we will also investigate small limits.

Case Study - exponential function

Recall that one definition of the exponential function is

\[e^x = \sum_{n = 0}^\infty \frac{x^n}{n!}\]

Modify this function

Modify this function so at most k terms are summed.

(Hint: if and break may help, or a modification to the while condition)

[ ]:
function myexp(x, k)
    sum = 0
    term = 1
    n = 1
    # modify so at most k terms are summed
    while sum + term != sum
        sum += term
        term *= x / n
        ### BEGIN SOLUTION

        ### END SOLUTION
        n += 1
    end
    sum
end

myexp(1, 10)

Investigation

Why did the original loop terminate (before any changes you made)? Can you write an equivalent expression in terms of sum, term, and \(\epsilon_\text{machine}\)? (Discuss on Zulip)

[ ]:
@assert myexp(1, 100)  exp(1)
@assert myexp(1, 3) == 2.5
@assert abs(myexp(1, 4)  - 2.6666666666666665) < eps()
@assert abs(myexp(1, 10) - 2.7182815255731922) < eps()

Relative Error

If we have a function \(\tilde{f} \left( x \right)\) that approximates \(f \left( x \right)\), then we can compute the relative error as

\[\frac{\tilde{f} \left( x \right) - f \left( x \right)}{\lvert f \left( x \right) \rvert}\]
[ ]:
relative_error(x, k) = abs(myexp(x, k) - exp(x)) / exp(x)
relative_error(1, 20)
[ ]:
# Plot relative error as k increases
plot(ks,
    k -> relative_error(1, k),
    xscale=:log10,
    yscale=:log10,
    xlabel="k",
    ylabel="relative error",
    label=:none
)
[ ]:
# Plot relative error as k increases
plot(ks,
    k -> relative_error(10, k),
    xscale=:log10,
    yscale=:log10,
    xlabel="k",
    ylabel="relative error",
    label=:none
)

More Plotting

Plot the relative error in myexp(-20, x) as k increases.

[ ]:
### BEGIN SOLUTION

### END SOLUTION

Final Accuracy

The intermediate accuracy can be quite bad. How good is the final result?

[ ]:
bottom = 1e-20 # To avoid having the plotter attempt log(0)
plot(x -> relative_error(x, 1000) + bottom,
    xlims=(-20, 20),
    yscale=:log10,
    xlabel="x",
    ylabel="relative error",
    label=:none
)

Exploration

Select 1 or more of these questions to explore.

  1. Plot each of the terms in the series expansion for computing \(e^{-20}\).

    • Where are rounding errors committed? Think about the size of those terms and the size of the final result (\(e^{-20}\) is a small number).

    • How would you explain the large relative errors using the concept of conditioning?

    • How is this different different from computing \(e^x\) with a positive argument?

  2. Use the identity \(e^{-x} = 1 / e^x\) to write a new function that calls myexp() and is accurate for both positive and negative arguments x. Demonstrate this improvement with a plot and explain.

  3. Use the identity \(e^x = \left( e^{x / 2} \right) ^2\) to reduce the number of terms that need to be summed. Recall that squaring is easy – it is just multiplication. Demonstrate this improvement with a plot and explain.

[ ]:
## Exploration 1

### BEGIN SOLUTION

### END SOLUTION
[ ]:
## Exploration 2

### BEGIN SOLUTION

### END SOLUTION
[ ]:
## Exploration 3

### BEGIN SOLUTION

### END SOLUTION