Functions

The language every scientific model is written in.

⏱ 15 minLevel: starting point3 simulations
In plain words

A function is a machine. You put something in, and it gives you exactly one thing out. Put a distance into an autorickshaw's fare meter and out comes one fare. Put a time of day into a thermometer log and out comes one temperature. Every equation you will meet in this workshop — and every neural network — is a function.

You will learn
Level 1 · basicWhat a function is; domain and range; everyday families Level 2 · standardComposition and functions of several variables Level 3 · advancedFields $u(x,t)$ and the link to networks
Simplest example first · the doubling machine

Take the rule "double the number": $f(x) = 2x$. Put in $3$, get out $6$. Put in $0$, get out $0$. Every input gives exactly one output — that is all a function is. Writing the pairs in a table and plotting them as points $(x, f(x))$ gives the graph: here a straight line through the origin.

input $x$01234
output $f(x) = 2x$02468

Everything else in this chapter — exponentials, waves, neural networks — is this same idea with a more interesting rule.

1. What is a function?

Animation: an input slides along the x-axis, goes through a machine f, and the output x squared is shown on the parabola.
Watch: A function is a machine: slide the input $x$ and watch the single output $f(x) = x^2$ follow along the graph.

We write $f(x)$ (read "f of x") for the output of the function $f$ when the input is $x$. The input is called the variable; the rule is what $f$ does to it.

Definition · Function

A function $f$ from a set $A$ to a set $B$, written $f : A \to B$, is a rule that assigns to each element $x \in A$ exactly one element $f(x) \in B$. The set $A$ is the domain (allowed inputs); the set of outputs actually produced is the range.

Stewart, Calculus: Early Transcendentals, 8th ed., Cengage (2016), chapter "Functions and Models".

The words "exactly one" matter. A fare meter that showed two different fares for the same distance would be broken — and so would a "function" that gave two outputs for one input. Different inputs, however, may share an output: $x^2$ gives $9$ for both $x=3$ and $x=-3$.

Example

Rickshaw fare: ₹30 to start plus ₹12 per km. As a function of distance $d$ (km): $F(d) = 30 + 12d$. So $F(5) = 30 + 60 = 90$, i.e. ₹90. Domain: $d \ge 0$ (no negative distances).

The function machinedrag the input; change the rule
The input $x$ goes in along the horizontal axis; the output $f(x)$ comes out on the vertical axis. Change $a$ and $b$ — these are parameters: they change the shape of the rule, not the input.

2. Variables, parameters, domain and range

In $f(x) = a\,e^{bx}$, the letter $x$ is the variable (it changes), while $a$ and $b$ are parameters (fixed for one situation, but we may choose different values for different situations). This distinction is everywhere in science: in a cooling coffee cup, time is the variable and the cooling rate is a parameter. In a neural network, the millions of adjustable numbers are parameters, collected into a symbol $\theta$ — and "training" simply means choosing them well.

Real world

A reservoir engineer writes the pressure in a well as a function of time, $p(t)$. Its parameters — rock permeability, fluid viscosity — are not known exactly. Estimating parameters from measured data is called an inverse problem, and it is one of the main uses of scientific machine learning.

3. Families of functions you will meet

FamilyFormulaShapeWhere it appears
Linear$a x + b$straight line, constant slope $a$uniform motion, Ohm's law, simple pricing
Polynomial$a_0 + a_1x + \dots + a_nx^n$curves with up to $n-1$ bendsprojectile paths, approximations (Taylor series)
Exponential$a\,e^{kx}$grows ($k>0$) or decays ($k<0$) at a rate proportional to itselfpopulation, cooling, radioactive decay, interest
Trigonometric$A\sin(\omega x + \varphi)$repeating wavesvibrations, sound, AC electricity, seasons
Sigmoid / tanh$\tanh x$, $\dfrac{1}{1+e^{-x}}$smooth S-shaped steplogistic growth; activation functions in neural networks

4. Chaining functions: composition

We can feed the output of one function into another. If $g$ acts first and then $f$, we write $(f\circ g)(x) = f(g(x))$. Order matters: putting on socks and then shoes is not the same as shoes and then socks.

Definition · Composition

For $g: A\to B$ and $f: B\to C$, the composition $f\circ g: A\to C$ is defined by $(f\circ g)(x) = f(g(x))$.

Stewart (2016), chapter "Functions and Models", section on new functions from old functions.
Order matterscompare f(g(x)) with g(f(x))
f(g(x))g(f(x))
With $g = \sin$ and $f(x)=x^2$: $f(g(x))=\sin^2 x$ stays between 0 and 1, while $g(f(x))=\sin(x^2)$ oscillates faster and faster. Same two machines, different order, different result.
In SciML

A deep neural network is nothing more than a long composition of simple functions: $u_\theta(x) = f_L(\cdots f_2(f_1(x)))$. Each layer $f_\ell(z) = \tanh(W_\ell z + b_\ell)$ is a linear map followed by a bend. Composition is what lets a few simple pieces build very complicated shapes — you will see this live in Chapter 12.

5. Functions of several variables

Many quantities depend on more than one thing. The temperature in a metal rod depends on where you measure ($x$) and when ($t$): $u(x,t)$. Such a function is called a field. Its graph is a surface, which we can show as a colour map.

A rod cooling downu(x, t) = e−π²αt sin(πx)
Left: colour shows temperature at each position (across) and time (down); bright is hot. Right: the temperature profile along the rod at the chosen time. This formula is the exact solution of the heat equation that you will meet in Chapter 7.

6. Try it in Python

Press Run. Python runs inside your browser — nothing to install. The first run downloads Python once (about 10–20 MB).

import numpy as np
import matplotlib.pyplot as plt

def fare(d):                      # a function: distance (km) -> fare (Rs)
    return 30 + 12 * d

print("Fare for 5 km:", fare(5))

x = np.linspace(-3, 3, 200)       # 200 inputs between -3 and 3
plt.plot(x, x**2, label="x^2")
plt.plot(x, np.exp(-x), label="exp(-x)")
plt.plot(x, np.tanh(2 * x), label="tanh(2x)")
plt.ylim(-2, 6); plt.legend(); plt.grid(alpha=.3)
plt.title("Three families of functions")
plt.show()

Which of these is not a function of $x$?

Right. For $x = 4$, both $y = 2$ and $y = -2$ satisfy $y^2 = x$ — two outputs for one input. Restricting to $y \ge 0$ gives the function $y = \sqrt{x}$.

If $g(x) = x + 1$ and $f(x) = 2x$, what is $f(g(3))$?

Right: $g(3) = 4$, then $f(4) = 8$. In the other order, $g(f(3)) = g(6) = 7$.

Key takeaways

Further reading