Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. # Define funtion interpolate
  2. def interpolate(x,y,x_test):
  3.    
  4.     # Define variables needed for equation
  5.     # Search for the lowest closest matching value in the x array against the inputted x_test
  6.     lower_x = min(x, key=lambda z:abs(z-x_test))
  7.  
  8.     # Test if the closest value is higher than the given value to test
  9.     if lower_x > x_test:
  10.  
  11.         # Make sure array wont go out of range
  12.         if (x[x.index(lower_x)] > 0):
  13.  
  14.             # Redefine value with shifted index down by one
  15.             lower_x = x[x.index(lower_x)-1]
  16.  
  17.     # Get value 1 index higher than that
  18.     if x[x.index(lower_x)] < (len(x)-1):
  19.         higher_x = x[x.index(lower_x) + 1]
  20.  
  21.     # Else
  22.     else:
  23.         # Higher x is maximum
  24.         higher_x = x[x.index(lower_x)]
  25.  
  26.         # Lower x is one below
  27.         lower_x = x[x.index(higher_x)-1]
  28.        
  29.     # Get corresponding lower and higher values from y array
  30.     lower_y = y[x.index(lower_x)]
  31.     higher_y = y[x.index(higher_x)]
  32.  
  33.     # Perform given equation a*x'+b
  34.     a = (higher_y - lower_y) / (higher_x - lower_x)
  35.     b = lower_y - a * lower_x
  36.  
  37.     yPrime = a * x_test + b
  38.  
  39.  
  40.     # Return
  41.     return yPrime
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement