Function that connects points with straight lines

Oliver Kovacs
3 min readMar 31, 2021

How to find a function that connects a list of points with straight line segments?

Problem

Given a list of n Points P = (P₁, P₂, …, Pₙ) where P₁ = (P₁ₓ, P₁ᵧ), … , ordered along the x axis, what function f(x), when graphed, connects the points in the domain [P₁ₓ; Pₙₓ] with straight lines?

Example: f(x) connects the points (1, 1), (2, 3) and (4, 2) in [1; 4]

Note: A Subscirpt always refers to the x component of a point, and never the xth index, same for y.

Solution

Note: I came up with this on my own so there are probably better ways to do this.

where

Explanation

Consider the function g(x)=a|x-b|: it is like a linear function, a determines the steepness, but it’s mirrored at b along the y axis.

If you sum multiple of these functions with different a and b values together, you get a zigzagged line, something that could work as the function we are searching for.

Let’s assume we can use the x-values of the given points as values for b. Then we get:

which is the same as

Now we need to find the values for a = (a₁, a₂, …, aₙ).

We know the values of f(x) at the given points:

which in simpler terms just means

From this we can construct a linear system of equations:

All values for P are known, so at this point, we could simply use an equation solver to get a.
However, it can also be rewritten as a matrix multiplication:

Solve for a:

And that’s it.

Example

Let’s find f(x) for the points (1, 1), (2, 3) and (4, 2).
First, we need to find the values for a.

Using the above formula, we get:

Simplify:

Solving with WolframAlpha:

a is (3/2, -5/4, 3/4)

Now we can use the very first formula to get f(x):

If we graph the solution with GeoGebra, we can see that it indeed meets all criteria.

--

--