Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions R1/TheParabolaMethod.py
Original file line number Diff line number Diff line change
@@ -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<f(x1))|(f(x2)<f(x1))|(f0+f(x2)-2*f(x1)<=0):
i+=1
x1 = x0 + h*2**(i-1)
x2 = x0 + h*2**(i)
else:
x1 = x0 - h
x2 = x0 - h*2**(i)
f0 = f0
while (f0<f(x1))|(f(x2)<f(x1))|(f0+f(x2)-2*f(x1)<=0):
i+=1
x1 = x0 - h*2**(i-1)
x2 = x0 - h*2**(i)
return (x0, x1, x2)

def get_top(x1, x2, x3, f1, f2, f3):
return (x1+x2)/2 + (f2-f1)*(x3-x2)*(x3-x1)/(2*(f1*(x2-x3)+f2*(x3-x1)+f3*(x1-x2)))

def sort(points):
for i in range(3,0,-1):
for j in range(i):
if points[j][1] > 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[ ]:




77 changes: 77 additions & 0 deletions R1/ThePolylineMethod.py
Original file line number Diff line number Diff line change
@@ -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[ ]: