Trying to interpolate linearly in python
<p>I have 3 arrays: a, b, c all with length 15. </p>

<pre><code>a=[950, 850, 750, 675, 600, 525, 460, 400, 350, 300, 250, 225, 200, 175, 150] 

b = [16, 12, 9, -35, -40, -40, -40, -45, -50, -55, -60, -65, -70, -75, -80]

c=[32.0, 22.2, 12.399999999999999, 2.599999999999998, -7.200000000000003, -17.0, -26.800000000000004, -36.60000000000001, -46.400000000000006, -56.2, -66.0, -75.80000000000001, -85.60000000000001, -95.4, -105.20000000000002] 
</code></pre>

<p>I am trying to find the value of a  at the index where b=c. T</p>

<p>The problem is that there is no place where b=c exactly so I need to linearly interpolate between values in the array to find the value of a where b=c. Does that make sense? </p>

<p>I was thinking about using <strong>scipy.interpolate</strong> to do the interpolation. </p>

<p>I am having a hard time wrappying my mind around how to solve this problem. Any ideas on this would be great!</p>

<p>This is not necessarily a solution to your problem, since your data does not appear to be linear, but it might give you some ideas. If you assume that your lines a, b, and c are linear, then the following idea works:</p>

<p>Perform a linear regression of lines a, b and c to get their respective slopes (m_a, m_b, m_c) and y-intercepts (b_a, b_b, b_c). Then solve the equation 'y_b = y_c' for x, and find y = m_a * x + b_a to get your result.</p>

<p>Since the linear regression approximately solves y = m * x + b, equation y_b = y_c can be solved by hand giving: x = (b_b-b_c) / (m_c-m_b).</p>

<p>Using python, you get:</p>

<pre><code>&gt;&gt; m_a, b_a, r_a, p_a, err_a = stats.linregress(range(15), a)
&gt;&gt; m_b, b_b, r_b, p_b, err_b = stats.linregress(range(15), b)
&gt;&gt; m_c, b_c, r_c, p_c, err_c = stats.linregress(range(15), c)
&gt;&gt; x = (b_b-b_c) / (m_c-m_b)
&gt;&gt; m_a * x + b_a
379.55151515151516
</code></pre>

<p>Since your data is not linear, you probably need to go through your vectors one by one and search for overlapping y intervals. Then you can apply the above method but using only the endpoints of your two intervals to construct your b and c inputs to the linear regression. In this case, you should get an exact result, since the least-squares method will interpolate perfectly with only two points (although there are more efficient ways to do this since the intersection can be solved exactly in this simple case where there are two straight lines).</p>

<p>Cheers.</p>

python scipy interpolation