Functions
The language every scientific model is written in.
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.
- what a function is, with the standard definition;
- the families of functions that appear again and again in science;
- how functions are chained together (composition) — the idea behind neural networks;
- functions of several variables, like temperature that depends on place and time.
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$ | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| output $f(x) = 2x$ | 0 | 2 | 4 | 6 | 8 |
Everything else in this chapter — exponentials, waves, neural networks — is this same idea with a more interesting rule.
1. What is a function?

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.
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$.
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).
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.
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
| Family | Formula | Shape | Where 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$ bends | projectile paths, approximations (Taylor series) |
| Exponential | $a\,e^{kx}$ | grows ($k>0$) or decays ($k<0$) at a rate proportional to itself | population, cooling, radioactive decay, interest |
| Trigonometric | $A\sin(\omega x + \varphi)$ | repeating waves | vibrations, sound, AC electricity, seasons |
| Sigmoid / tanh | $\tanh x$, $\dfrac{1}{1+e^{-x}}$ | smooth S-shaped step | logistic 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.
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.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.
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$.
- A function gives exactly one output for each input.
- Variables change; parameters set the shape. Training a network means choosing parameters.
- Exponentials, waves and S-curves appear throughout science.
- Composition builds complex functions from simple ones — exactly how neural networks work.
- Fields such as $u(x,t)$ are functions of several variables — the unknowns of PDEs.
Further reading
- J. Stewart, Calculus: Early Transcendentals, 8th ed., Cengage, 2016 — chapter "Functions and Models".
- G. Strang, Calculus, Wellesley-Cambridge Press — free online through MIT OpenCourseWare.
- OpenStax, Calculus Volume 1 — free online textbook, chapter "Functions and Graphs".