Code
Name = "YOUR NAME HERE"Assignment 2: Floating point numbers, conditioning, and solving nonlinear equations in 1d
Published: Tue 22 Sept
The assignment is worth 100 pts. The symbols ✍️, 💻 indicate theory (\(\LaTeX\)), practice (code), respectively.
Name = "YOUR NAME HERE"The following cell contains some code that you may find useful: simple_iteration is the function from Lecture 4 and order_table(x, ξ; α) prints the errors \(e_n := |x_n - \xi|\) together with \(\frac{e_{n}}{e_{n-1}^\alpha}\) and \(\frac{\log e_{n}}{\log e_{n-1}}\) (which are useful when estimating the asymptotic error constant and the order of convergence). You may need to ignore the last few rows of the table once \(e_n\) is of the order of machine precision.
using Plots, LaTeXStrings, Printf
# simple iteration x_{n+1} = g(x_n) (from Lecture 4)
function simple_iteration(g, x1; N=100, tol=1e-10, flag=false)
x = Vector{Float64}(undef, N)
x[1] = x1
for n ∈ 2:N
x[n] = g(x[n-1])
if !isfinite(x[n])
return x[1:n], false
end
# compute relative error
rel_err = abs(x[n] - x[n-1]) / max(abs(x[n]), 1e-12)
if rel_err < tol
return x[1:n], true
end
end
if flag
@warn "Fixed-point iteration did not converge within $N iterations."
end
return x, false
end
# prints the errors e_n = |x_n - ξ| together with
# e_n / e_{n-1}^α (\to μ if the order of convergence is α)
# log(e_n) / log(e_{n-1}) (\to order of convergence)
function order_table(x, ξ; α=1)
e = @. abs(x - ξ)
@printf("%4s %22s %12s %16s %20s\n", "n", "x_n", "e_n", "e_n/e_{n-1}^α", "log e_n/log e_{n-1}")
@printf("%4d %22.16f %12.3e\n", 1, x[1], e[1])
for n ∈ 2:length(x)
@printf("%4d %22.16f %12.3e %16.6f %20.6f\n", n, x[n], e[n], e[n]/e[n-1]^α, log(e[n])/log(e[n-1]))
end
endorder_table (generic function with 1 method)
Exercise 1. (Harmonic numbers)
The harmonic numbers are defined as \(H_N := \sum_{k=1}^N \frac1k\).
💻 Compute \(H_{N}\) for \(N = 10^7\) in Float32 (single precision) by summing forwards (\(k = 1, 2, \dots, N\)) and backwards (\(k = N, N-1, \dots, 1\)). What are the relative errors of these two approximations?
✍️ Which approximation is more accurate? Explain why.
Hint: You may use y defined below as the “exact” value of \(H_{10^7}\). You can use Float32(1) or 1f0 for \(1\) in Float32. Use typeof( x ) to check the type of variable x.
y = 16.695311365859851815399118939540451884249869752373080462785
# your code hereAnswer.
Exercise 2. Conditioning vs stability
Let \(f(x) := \sqrt{x+1} - \sqrt{x}\) for \(x > 0\).
Answer.
Float64 and compute the relative errors. You may use sqrt(big(x)+1) - sqrt(big(x)) (which uses higher precision BigFloat arithmetic) as the “exact” value of \(f(x)\).# your code hereAnswer.
Answer.
# your code hereExercise 3.
Let \(f(x) := x^3 + 4x^2 - 10\) and consider
\[\begin{align} g_1(x) &:= x - x^3 - 4x^2 + 10, \nonumber\\ g_2(x) &:= \frac12 \sqrt{10 - x^3}, \nonumber\\ g_3(x) &:= \sqrt{\frac{10}{4+x}}. \nonumber \end{align}\]
Answer.
Answer.
# your code hereAnswer.
Answer.
order_table). Which iteration gains more digits of accuracy per iteration and approximately how many?Answer.
# your code hereGoal: Reflect on your learning strategies, track your workload, and help us improve future course material.
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 two weeks:
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 these two weeks 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: