Skip to content

Instantly share code, notes, and snippets.

@ckunte
Last active May 4, 2023 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckunte/788269 to your computer and use it in GitHub Desktop.
Save ckunte/788269 to your computer and use it in GitHub Desktop.
Using bisect for linear interpolation
#!/usr/bin/env python
# encoding: utf-8
from bisect import bisect
# Roark's Table 26, Case 10, with three edges fixed
# a / b, and b1 are defined as lists below:
a_by_b = [0.25, 0.50, 0.75, 1.00, 1.50, 2.00, 3.00]
b1 = [0.020, 0.081, 0.173, 0.321, 0.727, 1.226, 2.105]
a, b = input("Enter a, b: ")
abyb = float(a) / b
try:
i = a_by_b.index(abyb)
beta1 = b1[i]
except ValueError:
# a/b did not match any value in a_by_b
i = bisect(a_by_b, abyb)
j = i - 1
P = b1[i] - b1[j]
q = (abyb - a_by_b[j])
Q = (a_by_b[i] - a_by_b[j])
beta1 = b1[j] + (P * q / Q)
finally:
print "a/b : ", round(abyb, 2)
print "Index of a/b ratio: ", i
print "beta1 : ", beta1
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment