Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Define funtion interpolate
- def interpolate(x,y,x_test):
- # Define variables needed for equation
- # Search for the lowest closest matching value in the x array against the inputted x_test
- lower_x = min(x, key=lambda z:abs(z-x_test))
- # Test if the closest value is higher than the given value to test
- if lower_x > x_test:
- # Make sure array wont go out of range
- if (x[x.index(lower_x)] > 0):
- # Redefine value with shifted index down by one
- lower_x = x[x.index(lower_x)-1]
- # Get value 1 index higher than that
- if x[x.index(lower_x)] < (len(x)-1):
- higher_x = x[x.index(lower_x) + 1]
- # Else
- else:
- # Higher x is maximum
- higher_x = x[x.index(lower_x)]
- # Lower x is one below
- lower_x = x[x.index(higher_x)-1]
- # Get corresponding lower and higher values from y array
- lower_y = y[x.index(lower_x)]
- higher_y = y[x.index(higher_x)]
- # Perform given equation a*x'+b
- a = (higher_y - lower_y) / (higher_x - lower_x)
- b = lower_y - a * lower_x
- yPrime = a * x_test + b
- # Return
- return yPrime
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement