# Gradient Flow

Flow · (ẋ, ẏ, ż) = f(x, y, z, t)

`(−d/dx (sin(x) · cos(z) · cos(t/2)),  −0.5y,  −d/dz (sin(x) · cos(z) · cos(t/2)))`

[Open in the app](https://www.wavelace.com/app#p=126) · [This page](https://www.wavelace.com/presets/gradient-flow)

### The system

Every other field in this app is written out by hand. This one is worked out from a landscape. Take the height `V = sin(x) · cos(z) · cos(t/2)`, a lattice of hills and hollows, and let each seed move straight downhill. The velocity is then minus the slope, `−d/dx V` across and `−d/dz V` along. The third expression, `−0.5y`, only settles the seeds onto the floor, since the landscape uses two variables.

Written that way the field is never stated, only the surface it comes from. Change the landscape and the whole flow changes with it, which is the point of having a derivative in the language at all.

### Why it keeps moving

A seed rolling downhill stops at the bottom, so a still landscape would be over quickly. The basins of `sin(x) · cos(z)` sit at `(−1.57, 0)` and `(1.57, ±3.14)`, one every `2π ≈ 6.28`. Every seed reaches one and stays there.

The `cos(t/2)` is what prevents that. It changes sign every `6.28` seconds at `Speed` 1, which turns the landscape inside out. Each hollow becomes a hill, every seed finds itself on a summit, and the cloud streams off to the hollows that were hills a moment ago. The pattern returns to its start every `12.57` seconds.

### Try

- Take the clock out, with `−d/dx (sin(x) · cos(z))` in the first expression and the matching change in the third. The seeds run to the nearest basin and then nothing happens at all.
- Turn the whole thing round by dropping both minus signs. Downhill becomes uphill, so the seeds climb to the summits and balance there, which is the same picture read backwards.
- Tilt the landscape by adding `+ 0.3x` inside both derivatives. Every basin is now on a slope, and the seeds drift steadily in one direction instead of sitting still.
- Raise `Seed spread` to its top. The cloud starts wide enough to reach more than one basin at a time, and the split between them is easier to see.

### Read more

- [Gradient descent](https://en.wikipedia.org/wiki/Gradient_descent)
- [Vector field](https://en.wikipedia.org/wiki/Vector_field)
- [Conservative vector field](https://en.wikipedia.org/wiki/Conservative_vector_field)
- [Numerical differentiation](https://en.wikipedia.org/wiki/Numerical_differentiation)
