# Finding the two closest values of a given value

1. Aug 22, 2015

### BubblesAreUs

1. The problem statement, all variables and given/known data

Say, I was given a list, i.e. [1, 3, 7, 9, 12, 15, 24]

Now I'd like to find two values closest to an input ( say, x) so that I can produce an interval ( f(a), f(b)).

This interval would be used in an IVT problem where the intervals will be be part of [a,b] and will be used to compute the output of the initial input.

2. Relevant equations

given x

a < x < b

Find a and b within the list.

3. The attempt at a solution

I'm trying to tackle this problem via hand, but I'm also using python as well. I've thought of using a list, calculating the absolute difference between each element and then find the lowest difference.

List = [.......]
x ( given number) = input("Enter given number")

Output = numpy.substract(List, x)

The lowest absolute difference should represent the closest value to the given number. I suppose I could take the second lowest difference as well, but I haven't really thought about a control loop for that problem.

On MATLAB, it's possible to take an array and given number, compute its absolute value and then use that to find its min, i.e. [,idx]= min(abs(List - x)). On Python, it doesn't seem like min() can be used for arrays/ lists.

Let's see how this goes...

2. Aug 22, 2015

### Staff: Mentor

Do you want to find the closest values or one value above and one below? In your example, what happens with x=16? Do you want to find 12 and 15 (the closest values) or 15 and 24 (to have an interval that contains 16)?

Is the input always sorted like in your example? That makes it easier.

You can keep track of the lowest two differences then you don't have to store all the differences, especially if the list is long.

3. Aug 22, 2015

### BubblesAreUs

If x = 16, I'd want it to fall within the interval: 15 < x < 24. It should intermediate to two points.

If I store the difference, I will have to fetch the position in the original position as well.

Is there an easier way to just retrieve the lowest absolute difference and then produce the original number? Right now, I'm thinking of utilising a while Output != min(output) with i being tied a counter. That would tell me the index of the list where min is located. Then I could just add the index by 1 since the list will be in ascending order and intermediate to that value and the index where the abs is the lowest.

4. Aug 22, 2015

### Staff: Mentor

Then taking absolute differences does not help. You need the sign.
Keep track of the position, too, of course.

Again: Is your original input list sorted by size? Your example would suggest this.

5. Aug 22, 2015

### BubblesAreUs

Yes, I could use the index() feature. Now, if I were to cease using the abs() function, I would no longer be able to determine the lowest distance between the given value and the an integer on the list. Since it is a continuous function, either the value to its left-side or right-side would represent its corresponding bond to x.

I was thinking about the squeeze theorem and how the two lower/ upper bound functions and the introduced target function lie in between the two bounds.

Again: Is your original input list sorted by size? Your example would suggest this.[/QUOTE] Yes it'll be sorted by size in ascending order.

6. Aug 22, 2015

### SammyS

Staff Emeritus
Yes it'll be sorted by size in ascending order.[/QUOTE]
As mfb said, it's pretty simple if the list is sorted. Just go through the list in order until the first item larger than x.That's b, the item in the list just before that is a.

Only small problems to deal with:
x is equal to an item in the list.
x is outside of the range of items in the list.​

7. Aug 23, 2015

### BubblesAreUs

[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
As mfb said, it's pretty simple if the list is sorted. Just go through the list in order until the first item larger than x.That's b, the item in the list just before that is a.

Only small problems to deal with:
x is equal to an item in the list.
x is outside of the range of items in the list.​
[/QUOTE] Got it. Seems to work.

Thanks.