# Use line segments to approximate a function

I need to use line segments to approximate a function, f = 1/x. The range of x is from 1 to 2048 with an interval of 1. I will pick 10 locations for x and interpolate y between two adjacent x using line segment. The error between interpolated values and real values is expected to be minimum. I am using MATLAB for implementation.

The problem is that picked points are in order. If the picked points are modified, the points between two picked points will be changed accordingly. How can I write an objective function for such case?

I have little experience of optimization using MATLAB. How do I write an objective function that need to be optimized?

• What do you mean by "with an interval of 1"? May 6, 2015 at 13:03
• I am a little bit confused about what you are trying to do. Do you mean that you will only have 10 points (and thus 9 line segments) to approximate the function f between 1 and 2048? Or do you you mean that you will have an arbitrary number of points (presumable fairly large) evenly spaced between 1 and 2048 and that you then want to pick 10 x points (that probably don't coincide with your interpolations points) and thus use the line segment to interpolate the approximate y values? May 21, 2015 at 1:32

First, I would like to mention that what you define as 'error' is pretty important. You could obtain error estimates based on an assortment of different norms or error measures, each most likely resulting in a slightly different solution. Optimality will always be depending on the error/cost function.

Next, I personally formulated it as a Weighted Nonlinear Least Squares problem, where the location of each x point used for interpolation is a part of a vector seeking to be optimized using the standard Weighted Least Squares styled cost function.

I did a first attempt trying to use gradient descent with this formulation, trying out the results with a few different guesses. A guess I formulated before I even attempted this numerically has found to be roughly optimal when I plugged it into the numerical algorithm.

This sub-optimal analytical solution is:

$$x = e^{(-as)}$$

where $a = -ln(2048)$, and $s \in [0, 1]$. For this problem, choose 10 equally spaced apart values for $s$ starting at $s$ = 0 and ending at $s$ = 1. This generates a set of x values increasingly spreading apart as x increases.

Now, I got some pretty worthwhile results with the approach I did, but I am not sure if it's totally optimal or not with respect to the cost function I made. I would recommend tackling this problem using a more global optimization approach, such as Particle Swarm Optimization, Genetic Optimization, etc.

Basic $L^p$ Norm Info