Algorithm to position samples minimizing error

We have a 1D function f(x). We would like to approximate this function with a polyline of n points. How can we find the x position for these points so the polyline approximates f(x) with low error (not necessarily optimal but if it was, then even better)?

What algorithms are available to solve this problem? What are their trade-offs?

Example: the following two polylines have the same number of points but the second one approximates better the curve.

enter image description here