Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- def f(x):
- return x + (1 / x**2)
- def interp_parabolic(_a, _b, eps):
- a = _a
- b = _b
- c = a + ((b - a) * 0.5)
- d_top = 0.0
- d_bottom = 0.0
- d = 0.0
- l = b - a
- while True:
- if l < eps:
- print('a:', a)
- print('b:', b)
- print('c:', c)
- print('d:', d)
- return True
- else:
- f_c = f(c)
- f_b = f(b)
- f_a = f(a)
- a_2 = a**2
- b_2 = b**2
- c_2 = c**2
- d_top = (f_a * (c_2 - b_2)) + \
- (f_c * (b_2 - a_2)) + (f_b * (a_2 - c_2))
- d_bottom = (f_a * (c - b)) + (f_c * (b - a)) + (f_b * (a - c))
- d = 0.5 * (d_top / d_bottom)
- f_d = f(d)
- if d < c:
- if f_d < f_c:
- b = c
- c = d
- else:
- a = d
- else:
- if f_d < f_c:
- a = c
- c = d
- else:
- b = d
- l = b - a
- interp_parabolic(1.0, 2.0, 0.0001)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement