# function [ya] = piecewise_linear_interpolant_value (x, xi, yi] # Accompanying program for the text # # Classical and Modern Numerical Analysis: # Theory, Methods and Practice # by Azmy S. Ackleh, Edward J. Allen, # R. Baker Kearfott, and Padmanabhan Seshaiyer # # (Taylor and Francis / CRC Press, 2009) # # y = piecewise_linear_interpolant_value (x, xi, yi] returns the # vector of values of the piecewise linear interpolant at corresponding # coordinates of the vector x, corresponding to the data set whose # abscissas are in the vector xi and whose ordinates are in the vector yi. # The size of x and the size of xi should correspond exactly to the number # of points to plot and the number of abscissas, respectively. import numpy as np def piecewise_linear_interpolant_value (x, xi, yi): # this is not the best way to do this: should be vectorized m = len(x) n = len(xi) - 1 ya = np.empty_like(x) for i in range(m): for j in range(n): if xi[j] <= x[i] and x[i] <= xi[j+1]: hlp = xi[j+1] - xi[j] ya[i] = yi[j]*(xi[j+1]-x[i])/hlp + yi[j+1]*(x[i]-xi[j])/hlp return ya