# Why Is The Output Of Linspace And Interp1d Always The Same?

So I was doing my assignment and we are required to use interpolation (linear interpolation) for the same. We have been asked to use the `interp1d`

package from `scipy.interpolate`

and use it to generate new `y`

values given new `x`

values and old coordinates `(x1,y1)`

and `(x2,y2)`

.

To get new `x`

coordinates (lets call this `x_new`

) I used `np.linspace`

between `(x1,x2)`

and the new `y`

coordinates (lets call this `y_new`

) I found out using `interp1d`

function on `x_new`

.

However, I also noticed that applying `np.linspace`

on `(y1,y2)`

generates the exact same values of `y_new`

which we got from `interp1d`

on `x_new`

.

Can anyone please explain to me why this is so? And if this is true, is it always true?

And if this is always true why do we at all need to use the `interp1d`

function when we can use the `np.linspace`

in it's place?

Here is the code I wrote:

```
import scipy.interpolate as ip
import numpy as np
x = [-1.5, 2.23]
y = [0.1, -11]
x_new = np.linspace(start=x[0], stop=x[-1], num=10)
print(x_new)
y_new = np.linspace(start=y[0], stop=y[-1], num=10)
print(y_new)
f = ip.interp1d(x, y)
y_new2 = f(x_new)
print(y_new2) # y_new2 values always the same as y_new
```

## Answer

The reason why you stumbled upon this is that you only use two points for an interpolation of a linear function. You have as an input two different `x`

values with corresponding `y`

values. You then ask `interp1d`

to find a linear function `f(x)=m*x +b`

that fits best your input data. As you only have two points as input data, there is an exact solution, because a linear function is exactly defined by two points. To see this: take piece of paper, draw two dots an then think about how many straight lines you can draw to connect these dots.

The linear function that you get from two input points is defined by the parameters `m=(y1-y2)/(x1-x2)`

and `b=y1-m*x1`

, where `(x1,y1),(x2,y2)`

are your two inputs points (or elements in your `x`

and `y`

arrays in your code snippet.

So, now what does `np.linspace(start, stop, num,...)`

do? It gives you `num`

evenly spaced points between `start`

and `stop`

. These points are `start`

, `start + delta`

, ..., `end`

. The step width `delta`

is given by `delta=(end-start)/(num - 1)`

. The -1 comes from the fact that you want to include your endpoint. So the `n`

th point in your interval will lie at `xn=x1+n*(x2-x1)/(num-1)`

. At what `y`

values will these points end up after we apply our linear function from `interp1d`

? Lets plug it in:

`f(xn)=m*xn+b=(y1-y2)/(x1-x2)*(x1+n/(num-1)*(x2-x1)) + y1-(y1-y1)/(x1-x2)*x1`

. Simplifying this results in `f(xn)=(y2-y1)*n/(num - 1) + y1`

. And this is exactly what you get from `np.linspace(y1,y2,num)`

, i.e. `f(xn)=yn`

!

Now, does this always work? No! We made use of the fact that our linear function is defined by the two endpoints of the intervals we use in `np.linspace`

. So this will not work in general. Try to add one more `x`

value and one more `y`

value in your input list and then compare the results.

## Related Questions

- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module