Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

2026-08-24 Functions

Last time

  • Class organization and policies; syllabus

Today

  • Using Julia

  • Plotting

  • Intro to floating point

  • Summing series

  • Relative vs absolute errors

  • On reliability, verification and validation

Reminders

Please link your GitHub username so we can distribute repositories.

Please share availability for drop-in hours

Julia

Julia is a relatively new programming language. Think of it as MATLAB done right, open source, and fast. It’s nominally general-purpose, but mostly for numerical/scientific/statistical computing. There are great learning resources. We’ll introduce concepts and language features as we go.

# The last line of a cell is output by default
x = 3
y = 4
4
println("$x + $y = $(x + y)") # string formatting/interpolation
4;  # trailing semicolon suppresses output
3 + 4 = 7
@show z = x + y
x * y
z = x + y = 7
12

Numbers

3, 3.0, 3.0f0, big(3.0) # integers, double precision, and single precision
(3, 3.0, 3.0f0, 3.0)
typeof(3), typeof(3.0), typeof(3.0f0), typeof(big(3.0))
(Int64, Float64, Float32, BigFloat)
# automatic promotion
@show 3 + 3.0
@show 3.0 + 3.0f0
@show 3000000000 + 3.0f0;
3 + 3.0 = 6.0
3.0 + 3.0f0 = 6.0
3000000000 + 3.0f0 = 3.0f9
# floating and integer division
@show 4 / 3
@show -5 ÷ 3; # type `\div` and press TAB
4 / 3 = 1.3333333333333333
-5 ÷ 3 = -1

Arrays

[1, 2, 3]
3-element Vector{Int64}: 1 2 3
# explicit typing
Float64[1, 2, 3]
3-element Vector{Float64}: 1.0 2.0 3.0
# promotion rules similar to arithmetic
[1,2,3.0] + [1,2,3]
3-element Vector{Float64}: 2.0 4.0 6.0
x = [10., 20, 30]
x[2] # one-based indexing
20.0
x[2] = 3.5;
# multi-dimensional array
A = [10 20 30; 40 50 60]
2×3 Matrix{Int64}: 10 20 30 40 50 60
A = np.array([[10, 20, 30], [40, 50, 60]])

Functions

function f(x, y; z=3)
    zero = 0.0
    sqrt(x*x + y*y) + z + zero
end
f (generic function with 1 method)
f(big(3.0), 4, z=5)
10.0
g(x, y) = sqrt(x^2 + y^2)
g(3, 4)
5.0
((x, y) -> sqrt(x^2 + y^2))(3, 4)
5.0

Functions of functions

function slope_over(a, b, f)
    (f(b) - f(a)) / (b - a)
end
slope_over (generic function with 1 method)
slope_over(0, 1, x -> sin(x)*x)
0.8414709848078965

Loops

# ranges
typeof(1:50000000:5)
collect(1:5)
5-element Vector{Int64}: 1 2 3 4 5
x = 0
for n in 1:500000
    x += 1/n^2
end
@show x
x - pi^2/6 # trivia you can easily look up if needed
x = 1.644932066850254
-1.999997972346179e-6
# list comprehensions
sum([1/n^2 for n in 1:1000])
1.6439345666815612

Poll: What is floating point arithmetic?

  1. fuzzy arithmetic

  2. exact arithmetic, correctly rounded

  3. the primary focus of numerical analysis

0.1 + 0.2 - 0.3
5.551115123125783e-17
using Plots
default(linewidth=4)
plot(x -> (1 + x) - 1, xlims=(-1e-15, 1e-15),
xlabel="x", ylabel="y")
plot!(x -> x)
Loading...

Machine epsilon

We approximate real numbers with floating point arithmetic, which can only represent discrete values. In particular, there exists a largest number, which we call ϵmachine\epsilon_{\text{machine}}, such that

1⊕x=1for all ∣x∣<ϵmachine.1 \oplus x = 1 \quad \text{for all}\ \lvert x \rvert < \epsilon_{\text{machine}}.

The notation ⊕,⊖,⊙,⊘\oplus, \ominus, \odot, \oslash represent the elementary operation carried out in floating point arithmetic.

eps = 1
while 1 + eps != 1
    eps = eps / 2
end
eps
1.1102230246251565e-16
eps = 1.f0
while 1 + eps != 1
    eps = eps / 2
end
eps 
5.9604645f-8

Beating exp

ex=1+x+x22!+x33!+⋯e^x = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \dotsb

Suppose we want to compute f(x)=ex−1f(x) = e^x - 1 for small values of xx.

f1(x) = exp(x) - 1
y1 = f1(1e-8)
9.99999993922529e-9
f2(x) = x + x^2/2 + x^3/6
y2 = f2(1e-8)
1.000000005e-8

Which answer is more accurate?

@show (y1 - y2)        # Absolute difference
@show (y1 - y2) / y2;  # Relative difference
y1 - y2 = -1.1077470910720506e-16
(y1 - y2) / y2 = -1.1077470855333152e-8
f1(big(1e-8))
1.00000000500000003758922774768741543940607483039462726343113165886778197508307e-08

We now turn attention to two terms of art that guide our understanding of epistemic soundness, or the extent to which a model can be “right for the right reasons”.

Verification

solving the problem right

Validation

solving the right problem

Verification is a mathematical activity that can, in principle, be completed (though one must always establish sufficient resolution). Validation is always ongoing, subject to tolerances and regime.