def interpolate(xarray, yarray, x, rigid_limits=True):
    """

    returns the y value of the linear interpolated function
    y(x). Assumes increasing xarray!

    rigid_limits=False means when x is outside xarray's range,
    use the endpoint as the y-value.

    """
    if not len(xarray) == len(yarray):
        print("lengths don't match.", len(xarray), len(yarray))
        return None
    if x < xarray[0] or x > xarray[-1]:
        if rigid_limits:
            print("x=" + str(x) + " is not in " + str(min(xarray)) + " to " + str(max(xarray)))
            return None
        else:
            if x < xarray[0]: return yarray[0]
            else:             return yarray[-1]

    # find the index of the first value in xarray higher than x
    for n2 in range(1, len(xarray)):
        if x >= min(xarray[n2], xarray[n2-1]) and x <= max(xarray[n2], xarray[n2-1]):
            break
        if n2 == len(xarray):
            print("couldn't find x anywhere.")
            return None
    n1 = n2-1

    # now we have the indices surrounding the x value
    # interpolate!

    return yarray[n1] + (x-xarray[n1])*(yarray[n2]-yarray[n1])/(xarray[n2]-xarray[n1])
def interpolate ( xarray , yarray , x , rigid_limits = True ) : if not len ( xarray ) == len ( yarray ) : print ( "lengths don't match." , len ( xarray ) , len ( yarray ) ) return None if x < xarray [ 0 ] or x > xarray [ - 1 ] : if rigid_limits : print ( "x=" + str ( x ) + " is not in " + str ( min ( xarray ) ) + " to " + str ( max ( xarray ) ) ) return None else : if x < xarray [ 0 ] : return yarray [ 0 ] else : return yarray [ - 1 ] for n2 in range ( 1 , len ( xarray ) ) : if x >= min ( xarray [ n2 ] , xarray [ n2 - 1 ] ) and x <= max ( xarray [ n2 ] , xarray [ n2 - 1 ] ) : break if n2 == len ( xarray ) : print ( "couldn't find x anywhere." ) return None n1 = n2 - 1 return yarray [ n1 ] + ( x - xarray [ n1 ] ) * ( yarray [ n2 ] - yarray [ n1 ] ) / ( xarray [ n2 ] - xarray [ n1 ] )
returns the y value of the linear interpolated function
    y(x). Assumes increasing xarray!

    rigid_limits=False means when x is outside xarray's range,
    use the endpoint as the y-value.