Skip to content

Instantly share code, notes, and snippets.

@stevenRush
Created April 6, 2014 20:19
Show Gist options
  • Save stevenRush/10011050 to your computer and use it in GitHub Desktop.
Save stevenRush/10011050 to your computer and use it in GitHub Desktop.
Метод ломанных
def task1(a=1, b=2, eps=0.0001): # Метод ломаных
# Определяем коэфициенты. Коэфициенты взяты как четвертый знак курса евро\рубль за 28.03-05.04
coef = (5, 4, 6, 6, 1, 9, 4)
# Определяем функцию
f = lambda x: abs(abs(abs(coef[0]*x*x + coef[1]*x) - abs(coef[2]*x*x-coef[3])) - abs(coef[4]*x*x-coef[5]*x+coef[6]))
# Определяем L
L, M, i = 0, 0, a
while i < b:
M = f(i) / eps
L = max(M, L)
i += eps
calcer = lambda a, b: (a+b)/2 + (f(a) - f(b))/(2*L)
x0 = calcer(a, b)
x1 = 0
x2 = 0
while abs(a - b) > eps:
x1 = calcer(a, x0)
x2 = calcer(x0, b)
if f(x1) > f(x2):
a = x1
x0 = x2
else:
b = x2
x0 = x1
if f(x1) < f(x2):
return x1, f(x1)
else:
return x2, f(x2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment