Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # z = (x1 + 1)^2 + x1x2 + (x1–3*x2)^2
- import random
- precision = 0.0000000001
- def func(x1, x2):
- return (x1 + 1) ** 2 + x1 * x2 + (x1 - 3 * x2) ** 2
- def derivative1(x1, x2):
- # производная по 1 переменной
- return 4 * x1 + 2 - 5 * x2
- # 4 -5
- def derivative2(x1, x2):
- # производная по 2 переменной
- return 18 * x2 - 5 * x1
- # -5 18
- def test(x1, x2):
- # уравнение из примера
- return (2*x1-5*x2)**2+(3*x1-15)**2
- def task1():
- h = 0.01
- x1 = -0.8
- x2 = -0.3
- n = 1000
- f = func(x1, x2)
- for i in range(1, n + 1):
- while True:
- f0 = f
- x1 += h
- f = func(x1, x2)
- if f - f0 > 0:
- break
- while True:
- f0 = f
- x2 += h
- f = func(x1, x2)
- if f - f0 > 0:
- break
- if abs(h) < precision / 2:
- break
- else:
- h -= h / 2
- print("\nx1 = %s\nx2 = %s\nF = %s\niter: %d" % (x1, x2, f, i))
- # task1()
- def task2():
- f = 1000
- x1, x2, n, h = -1, -1, 1000, 0.01
- for i in range(1, n):
- x10, x20 = x1, x2
- f0, f1, f2 = func(x1, x2), derivative1(x1, x2), derivative2(x1, x2)
- while True:
- f = func(x1, x2)
- x1 -= h * f1
- x2 -= h * f2
- if func(x1, x2) > f:
- break
- if ((x1 - x10) ** 2 + (x2 - x20) ** 2) ** 0.5 < precision * 10:
- print(x1, x2, f, i)
- break
- if abs(func(x1, x2) - f0) < precision * 0.0000001:
- print(x1, x2, f, i)
- break
- # task2()
- def task3():
- x1, x2, n = 1, 1, 1000
- for i in range(1, n + 1):
- x10, x20 = x1, x2
- f0, f1, f2 = func(x1, x2), derivative1(x1, x2), derivative2(x1, x2)
- while True:
- h = 0.00005
- x11, x21, a = x1, x2, 0
- while True:
- a += h
- fa = func(x11, x21)
- x11 -= a * f1
- x21 -= a * f2
- fb = func(x11, x21)
- if fb >= fa:
- break
- f = func(x1, x2)
- x1 -= a * f1
- x2 -= a * f2
- if func(x1, x2) > f:
- break
- if ((x1 - x10) ** 2 + (x2 - x20) ** 2) ** 0.5 < precision * 10:
- print(x1, x2, f)
- break
- if abs(func(x1, x2) - f0) < precision * 0.000001:
- print(x1, x2, f)
- break
- # task3()
- def task4():
- x1, x2, n, alpha, beta = 1, 1, 1000, 0.1, 0.5
- for i in range(1, n + 1):
- x10, x20 = x1, x2
- f0, f1, f2 = func(x1, x2), derivative1(x1, x2), derivative2(x1, x2)
- lmd = alpha
- while True:
- lmd *= beta
- if func(x10 - lmd * f1, x20 - lmd * f2) < f0:
- break
- while True:
- f = func(x1, x2)
- x1 -= lmd * f1
- x2 -= lmd * f2
- if func(x1, x2) < f:
- break
- if ((x1 - x10) ** 2 + (x2 - x20) ** 2) ** 0.5 < precision * 10:
- print(x1, x2, f)
- break
- if abs(func(x1, x2) - f0) < precision * 0.000001:
- print(x1, x2, f)
- break
- # task4()
- def task5():
- x10, x20, alp, x1, x2 = 1, 1, 1, 1, 1
- while True:
- x1k = x1
- x2k = x2
- f, f1, f2 = func(x1, x2), derivative1(x1, x2), derivative2(x1, x2)
- f11, f12, f21, f22 = 4, -5, -5, 18
- delta = f11 * f22 - f12 * f21
- delta1 = f1 * f22 - f2 * f12
- delta2 = f11 * f2 - f21 * f1
- dx1 = -delta1 / delta
- dx2 = -delta2 / delta
- x1 += alp * dx1
- x2 += alp * dx2
- dev = ((x1 - x1k)**2 + (x2 - x2k)**2)**0.5
- if dev < precision:
- break
- print(x1, x2, f, f1, f2)
- # task5()
- def task6():
- x1, x2, h, n = 10, 10, 0.01, 100
- for i in range(1, n):
- while True:
- fa = func(x1, x2)
- y = x1
- y1 = y - h
- y2 = y + h
- f0, f1, f2 = func(y, x2), func(y1, x2), func(y2, x2)
- x1 -= (h / 2) * (f2 - f1) / (f2 - 2 * f0 + f1)
- if abs(x1 - y) < precision * 10000:
- break
- while True:
- y = x2
- y1 = y - h
- y2 = y + h
- f0, f1, f2 = func(x1, y), func(x1, y1), func(x1, y2)
- x2 -= (h / 2) * (f2 - f1) / (f2 - 2 * f0 + f1)
- if abs(x2 - y) < precision * 10000:
- break
- fb = func(x1, x2)
- if abs(fa - fb) < precision*0.0000001:
- break
- print(x1, x2, func(x1, x2), i)
- # task6()
- def task7():
- x1, y1 = 0, 0
- x, y = 0, 0
- f = func(x, y)
- f0 = f
- # здесь я спецом беру такой цикл, желательно подобрать range под свой варик
- # на 1000 делю чтобы закидывать дробные значения в функцию
- for x in range(-1000, 0):
- f = func(x/1000, y/1000)
- if f < f0:
- x1, y1, f0 = x, y, f
- for y in range(-1000, 0):
- f = func(x/1000, y/1000)
- if f < f0:
- x1, y1, f0 = x, y, f
- print(x1/1000, y1/1000, func(x1/1000, y1/1000))
- # -0.766 -0.213 0.234043
- # task7()
- def task8():
- n, xk, yk = 1000, 100, 100
- x, y = 0, 0
- x1, y1 = 0, 0
- f = func(x, y)
- f0 = f
- for i in range(1, n):
- rnd = random.randint(-8000, -2000) / 10000
- print(rnd)
- x = rnd
- f = func(x, y)
- if f < f0:
- x1, y1, f0 = x, y, f
- for j in range(1, n):
- rnd = random.randint(-8000, -2000) / 10000
- y = rnd
- f = func(x, y)
- if f < f0:
- x1, y1, f0 = x, y, f
- if f0 < 0.0001:
- break
- print(x1, y1, f0)
- # task8()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement