From 43cd7b7880b0e27986de73945fb94cb64ffeb105 Mon Sep 17 00:00:00 2001 From: Elvirience Date: Wed, 17 May 2023 22:07:13 +0300 Subject: [PATCH] try1 --- R1/TheParabolaMethod.py | 105 ++++++++++++++++++++++++++++++++++++++++ R1/ThePolylineMethod.py | 77 +++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 R1/TheParabolaMethod.py create mode 100644 R1/ThePolylineMethod.py diff --git a/R1/TheParabolaMethod.py b/R1/TheParabolaMethod.py new file mode 100644 index 0000000..6898646 --- /dev/null +++ b/R1/TheParabolaMethod.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python +# coding: utf-8 + +# In[25]: + + +from numpy import sign +from math import cos + + +# In[26]: + + +def f(x): + return cos(x)/x**2 + +def find_convex_triple(x0, a, b, eps, i=1): + h = (b-a)/10 + while (f(x0+h)>f(x0))&(f(x0-h)>f(x0)): + h /= 2 + if (f(x0+h)-f(x0)<=eps)|(f(x0-h)-f(x0)<=eps): + return x0 + if f(x0+h)<=f(x0): + x1 = x0 + h + x2 = x0 + h*2**(i) + f0 = f(x0) + while (f0 points[j+1][1]: + points[j], points[j+1] = points[j+1], points[j] + return points + + +# In[27]: + + +def method(x0, eps, a, b, count=1): + try: + x1, x2, x3 = find_convex_triple(x0, a, b, eps) + except: + x0 = find_convex_triple(x0, a, b, eps) + x4 = get_top(x1, x2, x3, f(x1), f(x2), f(x3)) + f1 = f(x1) + f2 = f(x2) + f3 = f(x3) + f4 = f(x4) + points = sort([(x1, f1), (x2, f2), (x3, f3), (x4, f4)]) + print(f"Номер итерации: {count} x1 = {points[0][0]:.4f} f1 = {points[0][1]:.4f}\n\t\t x2 = {points[1][0]:.4f} f2 = {points[1][1]:.4f}\n\t\t x3 = {points[2][0]:.4f} f3 = {points[2][1]:.4f}\n\t\t x4 = {points[3][0]:.4f} f4 = {points[3][1]:.4f}\n") + while points[1][1] - points[0][1] >= eps: + if sign(points[1][0] - points[0][0]) == sign(points[2][0] - points[0][0]) == -sign(points[3][0] - points[0][0]): + count += 1 + x3 = points[3][0] + x2 = points[1][0] + x1 = points[0][0] + x4 = get_top(x1, x2, x3, f(x1), f(x2), f(x3)) + f1 = f(x1) + f2 = f(x2) + f3 = f(x3) + f4 = f(x4) + points = sort([(x1, f1), (x2, f2), (x3, f3), (x4, f4)]) + print(f"Номер итерации: {count} x1 = {x1:.4f} f1 = {f1:.4f}\n\t\t x2 = {x2:.4f} f2 = {f2:.4f}\n\t\t x3 = {x3:.4f} f3 = {f3:.4f}\n\t\t x4 = {x4:.4f} f4 = {f4:.4f}\n") + else: + count += 1 + x3 = points[2][0] + x2 = points[1][0] + x1 = points[0][0] + x4 = get_top(x1, x2, x3, f(x1), f(x2), f(x3)) + f1 = f(x1) + f2 = f(x2) + f3 = f(x3) + f4 = f(x4) + points = sort([(x1, f1), (x2, f2), (x3, f3), (x4, f4)]) + print(f"Номер итерации: {count} x1 = {x1:.4f} f1 = {f1:.4f}\n\t\t x2 = {x2:.4f} f2 = {f2:.4f}\n\t\t x3 = {x3:.4f} f3 = {f3:.4f}\n\t\t x4 = {x4:.4f} f4 = {f4:.4f}\n") + print(f"argmin(f) = {points[0][0]:.4f}") + + +# In[28]: + + +method(9, 0.0001, 7, 11) + + +# In[ ]: + + + + diff --git a/R1/ThePolylineMethod.py b/R1/ThePolylineMethod.py new file mode 100644 index 0000000..6d1384c --- /dev/null +++ b/R1/ThePolylineMethod.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python +# coding: utf-8 + +# In[196]: + + +import numpy as np +from math import cos, sin + + +# In[248]: + + +def f(x): + return cos(x)/x**2 + +def df(x): + return -sin(x)/x**2 - 2*cos(x)/x**3 + +def get_R(a, b, h): + X = np.arange(a, b+h, h) + R = abs(df(X[0])) + for i in range(X.size-1): + if abs(df(X[i])) >= R: + R = df(X[i]) + return R + h + +def x(a, b, R): + return (a+b)/2 + (f(a)-f(b))/(2*R) + +def m(a, b, R): + return R*(a-b)/2 + (f(a)+f(b))/2 + + +# In[249]: + + +def Method(a, b, h, eps, get_R, x, m, count=1, q=0, k=0): + R = get_R(a, b, h) + num = 0 + digits = eps + while digits < 1: + digits *= 10 + num += 1 + X = np.array([]) + M = np.array([]) + x = x(a, b, R) + m = m(a, b, R) + while f(x) - m >= eps: + delta = (f(x)-m)/2/R + print(f"Номер итерации: {count} x = {x:.{num}f} f(x) = {f(x):.{num}f} m = {m:.{num}f} delta = {delta:.{num}f}") + X = np.append(X, [x-delta, x+delta]) + M = np.append(M, [(f(x)+m)/2, (f(x)+m)/2]) + q = M[0] + for i in range(M.size-1): + if M[i] <= q: + q = M[i] + k = i + M = np.delete(M, k) + x = X[k] + X = np.delete(X, k) + m = q + count += 1 + print(f"Номер итерации: {count} x = {x:.{num}f} f(x) = {f(x):.{num}f} R = {R:.{num}f}") + + +# In[252]: + + +Method(7, 11, 0.1, 0.0000001, get_R, x, m) + + +# In[ ]: + + + +