+10 XP

Functions of Many Variables

So far every function had one input (usually time t). But real things depend on several inputs at once. A neuron's firing rate depends on both its excitatory input and its inhibitory input. How do you take a derivative when there's more than one knob?

The trick is wonderfully lazy: change one knob, freeze the rest.

A derivative that wiggles just one input while holding the others still is called a partial derivative. The only new symbol is a curly instead of the straight d — it's a little flag that says "there are other variables, but I'm ignoring them for now."

Worked example. Take a function of two variables:

f(x, y) = x² + 2xy + y²

A surface — its height depends on both x and y.

Partial with respect to x (treat y as a frozen constant):
• x² → 2x
• 2xy → 2y (y is just a number tagging along)
• y² → 0 (a constant doesn't change as x moves)

So ∂f/∂x = 2x + 2y.

Partial with respect to y (now freeze x):
• x² → 0
• 2xy → 2x
• y² → 2y

So ∂f/∂y = 2x + 2y.

Read the surprise in those answers. Both partials contain a 2x + 2y — they depend on both variables. That's entirely because of the cross term 2xy that mixes x and y. Kill the cross term (use just x² + y²) and the partials become a clean 2x and 2y — each one minds only its own variable. You'll see this in the next lesson's playground.

In Python you don't do this by hand — SymPy does the algebra for you. SymPy works with symbols (real math variables, not numbers), so it returns an exact formula, just like you'd get with pen and paper:

python
import sympy as sp                 # ← skip this and you get: NameError: name 'sp' is not defined

x, y = sp.symbols('x, y')          # make symbolic variables
f = x**2 + 2*x*y + y**2            # the function

df_dx = sp.diff(f, x)              # partial w.r.t. x
df_dy = sp.diff(f, y)              # partial w.r.t. y

print(df_dx)                       # 2*x + 2*y
print(df_dy)                       # 2*x + 2*y

sp.diff(f, x) takes the partial derivative with respect to x. The whole job is one function call per variable.

That error you may have hit — NameError: name 'sp' is not defined — is not a real bug. sp is just the nickname for SymPy, and it has to be imported first. NMA does it in a hidden setup cell, so if you run a demo cell on its own it errors. The fix is always the same: start with import sympy as sp.

Why neuroscience cares: a neuron driven by excitation and inhibition is a function of two inputs. The partial derivative w.r.t. excitation tells you how sensitive it is to a nudge of excitation; the partial w.r.t. inhibition tells you the same for inhibition. Stack those slopes together and you get the gradient — the single most important object in model training (it's the multi-knob version of the slope from the gradient-descent lesson).

🦌 Ilya says: you'll meet partial derivatives again as the Jacobian — a whole grid of them — when we study whether a circuit of neurons settles down or blows up. Same idea, just bookkeeping for many variables at once.