I am trying to find a generic way to get an approximation function for a given function. (I will be doing it programmatically eventually). What I want to obtain is a set of pairs, mapping the x-axis to the function's value, through which the approximation function can be interpolated as a piece-wise linear function later on.
My issue is that I don't know how frequent or sparse these points need to be on the x-axis. Obviously every function has its own behaviour, and what suits one does not suit the other. Also, it makes more sense for these intervals to vary dynamically to reflect the activity of the function at that region. I am thinking of maybe having some kind of error-tolerance value which defines how much the approximate function can deviate from the real one.
Is there some numeric or iterative method to obtain the values for a given function $f(x)$ that can build an approximation function $g(x)$?
This problem is known as the approximation with the free-knot splines, or optimal knot distribution. Maybe start by reading a few papers: Jupp, D. L. B, "Approximation to data by splines with free knots.", SIAM J. Numer. Anal., vol 15, No 2, 1978; or Lindstrom, Mary J., "Penalized Estimation of free-knot splines." Journal of Computational and Graphical Statistics, Vol . 8, No. 2, 1999.
If you have a discretized function, take a look at the methods in Matlab Curve Fitting Toolbox, e.g.,
OPTKNT Optimal knot distribution. OPTKNT(TAU,K) returns an `optimal' knot sequence for interpolation at data sites TAU(1), ..., TAU(n) by splines of order K.
Or a least squares fit with Free-knot spline approximation.