scipy - Trying to interpolate linearly in python -
i have 3 arrays: a, b, c length 15.
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]
i trying find value of @ index b=c. t
the problem there no place b=c need linearly interpolate between values in array find value of b=c. make sense?
i thinking using scipy.interpolate interpolation.
i having hard time wrappying mind around how solve problem. ideas on great!
here's simpler variation of function another answer of mine:
from __future__ import division import numpy np def find_roots(t, y): """ given input signal `y` samples @ times `t`, find times `y` 0. `t` , `y` must 1-d numpy arrays. linear interpolation used estimate time `t` between samples @ sign changes in `y` occur. """ # find y crosses 0. transition_indices = np.where(np.sign(y[1:]) != np.sign(y[:-1]))[0] # linearly interpolate time values transition occurs. t0 = t[transition_indices] t1 = t[transition_indices + 1] y0 = y[transition_indices] y1 = y[transition_indices + 1] slope = (y1 - y0) / (t1 - t0) transition_times = t0 - y0/slope return transition_times
that function can used t = a
, y = b - c
. example, here data, entered numpy arrays:
in [354]: = np.array([950, 850, 750, 675, 600, 525, 460, 400, 350, 300, 250, 225, 200, 175, 150]) in [355]: b = np.array([16, 12, 9, -35, -40, -40, -40, -45, -50, -55, -60, -65, -70, -75, -80]) in [356]: c = np.array([32.0, 22.2, 12.399999999999999, 2.599999999999998, -7.200000000000003, -17.0, -26.800000000000004, -3 ...: 6.60000000000001, -46.400000000000006, -56.2, -66.0, -75.80000000000001, -85.60000000000001, -95.4, -105.2000000000 ...: 0002])
the place "b = c" place "b - c = 0", pass b - c
y
:
in [357]: find_roots(a, b - c) out[357]: array([ 312.5])
so linearly interpolated value of a
312.5.
with following matplotlib commands:
in [391]: plot(a, b, label="b") out[391]: [<matplotlib.lines.line2d @ 0x11eac8780>] in [392]: plot(a, c, label="c") out[392]: [<matplotlib.lines.line2d @ 0x11f23aef0>] in [393]: roots = find_roots(a, b - c) in [394]: [axvline(root, color='k', alpha=0.2) root in roots] out[394]: [<matplotlib.lines.line2d @ 0x11f258208>] in [395]: grid() in [396]: legend(loc="best") out[396]: <matplotlib.legend.legend @ 0x11f260ba8> in [397]: xlabel("a") out[397]: <matplotlib.text.text @ 0x11e71c470>
i plot
Comments
Post a Comment