4  Mathematics

4.1 Mappings

4.1.1 Functions \(\mathbb{R} \to \mathbb{R}\)

x = parameter(0 .. 0.05)
fplot(sin(1 / x), yscale=0.05, npoints=11)
x = parameter(0 .. 2π)
s = sin(x)
c = cos(x)

f = 2s
g = s + f'
h = 1 / 10 * (0.5 - x) * (pi / 2 - x) * (3pi / 2 - x) * (2pi - x)

p = fplot(c, s, g, h)
scatter!([0.5, pi / 2, 3pi / 2, 2pi], zeros(4), color=:tomato)
p
x = parameter(-1 .. 1)
f = exp(x)
g = exp(-x)
h = exp(x - 1)

fplot(f, g, h, -f, -g, -h)
x = parameter(-1 .. 1)
f = x
F = antiderivative(f)
fplot(f, F)
integrate(f, 0 .. 1)
0.5
p = [0, 1, 2]
f = fplot(fromroots(p, -0.1 .. 2.1))
scatter!(p, 0 * p, color=:tomato)
f

Lagrange polynomials

p = [0, 1.5, 2]
f = fplot(lagrangepolynomials(p, 0 .. 2)...)
scatter!(p, [0, 0, 0], color=:tomato)
scatter!(p, [1, 1, 1], color=:blue)
f

Maclaurin polynomials

function maclaurin_polynomial(f, n)
    x = parameter(domain(f))
    sum([derivativeat(f, 0.0, k) / factorial(k) * x^k for k = 0:n])
end

f = Sin(-pi .. pi)
p1 = maclaurin_polynomial(f, 1)
p3 = maclaurin_polynomial(f, 3)
p5 = maclaurin_polynomial(f, 5)
fplot(f, p1, p3, p5)

Cubic Hermite interpolation polynomials

I = -1 .. 2
m = monomials(0:3, I)
s = vcat([[ValueAtLF(x), DerivativeAtLF(x)] for x = endpoints(I)]...)
A = [sⱼ(mᵢ) for mᵢ = m, sⱼ = s]
h = inv(A) * m

fplot(h...)

4.1.2 Parametric curves

Lissajous curve

x = parameter(0 .. 2π)
u = ParametricCurve(cos(x), sin(2x))
v = 0.075 * sin(100x) * UnitNormal(u)
w = 0.025 * ParametricCurve(sin(200x), cos(200x))
fplot(u, u + v, u + w)

Interpolate points

points = stack([[0.0, 0.0], [1.5, 1.0], [2.0, 0.0], [1.5, -1.0], [0.0, 0.0]])
u = polynomialinterpolation(points)

fig, ax = fplot(u)
scatter!(ax, points, color=:tomato)
fig

4.1.3 Monomials

fplot(monomials(0:100, 0 .. 1)...)

4.1.4 Functions of two variables

Create the function \(f : [0, 5\pi]^2 \to \mathbb{R}, \quad f(\mathbf{x}) = \sin x_1 \sin x_2\)

f = makefunction(x -> exp(-hypot(x[1], x[2])^2 / 20) * cos(x[1]) * cos(x[2]), -2π .. 2π, -2π .. 2π);

By default the plot is a view down from positive \(z\) direction

fplot3d(f, mesh=9)

Create a 3D plot using Axis3 and switch of the mesh off since CairoMakie does not handle hidden lines.

fig = Figure()
Axis3(fig[1, 1])
fplot3d!(f, npoints=250, mesh=nothing)
fig

A good 3D plot is obtained using GLMakie:

GLMakie.activate!()
fig = Figure()
Axis3(fig[1, 1])
fplot3d!(f, mesh=19, npoints=250)
fig

Multivariate polynomial \(f(x, y) = x^2 y - y\)

g = MPolynomial([2 1; 0 1], [1.0, -1.0], QHat)
fig = Figure()
Axis3(fig[1, 1])
fplot3d!(g)
fig

Multivariate monomials

mmonomials(2, 3, QHat) |> components |> reverse |> fplot3d

Plot gradient of product function

CairoMakie.activate!()
g = ProductFunction(Sin(0 .. 2π), Sin(0 .. 2π))
p = fplot3d(g, mesh=nothing)
vplot!(gradient(g), npoints=9, lengthscale=0.3)
p