Advertisement
AlexandrTalchuk

Untitled

Oct 24th, 2021
1,198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4.  
  5. def F(x):
  6.     return x**0.5-np.cos(x)
  7.  
  8. def F1(x):
  9.     return 0.5/(x**0.5)+np.sin(x)
  10.  
  11. def print_func(a,b,m):
  12.     fig, axs = plt.subplots()
  13.     x=np.linspace(a,b,m)
  14.     y = F(x)
  15.     l1, = axs.plot(x,y)
  16.     fig.legend((l1,),('F','upper right'))
  17.     plt.grid()
  18.     plt.tight_layout()
  19.     plt.show()
  20.  
  21. def print_values(a,b,m):
  22.     x=np.linspace(a,b,m)
  23.     y = F(x)
  24.     for i in range(len(x)):
  25.         print("x =",f"{x[i]:.3f}","\ty =",f"{y[i]:.3f}")
  26.  
  27. def MP3(x1,e,h,F,F1,dictionary):
  28.     zm=10
  29.     it=0
  30.     d1=F1(x1)
  31.     if(d1<0):#////////
  32.         h=-h
  33.     x2=x1+h
  34.     d2=F1(x2)
  35.     if((d2-d1)/h<=0):
  36.         print("Начальное приближение выбрано неправильно")
  37.         return dictionary
  38.     y1=F(x1)
  39.     y2=F(x2)
  40.     while (abs(zm)>e):
  41.         it+=1
  42.         z1=x1-x2
  43.         p=(d1-d2-2*(y1-y2-d2*z1)/z1)*z1*z1
  44.         q=(d2-d1+3*(y1-y2-d2*z1)/z1)/z1
  45.         r=d2
  46.         koren=q*q-3*p*r
  47.         if(koren<0):
  48.             print("под корнем меньше нуля")
  49.  
  50.         zm=(-q+(koren)**0.5)/(3*p)#////////////////
  51.         x1=x2
  52.         y1=y2
  53.         d1=d2
  54.         x2+=zm
  55.         if(x2<=0):
  56.             print("x<=0")
  57.         y2=F(x2)
  58.         d2=F1(x2)
  59.     x_answ=x2+zm
  60.     if(check(dictionary,x_answ)==True):
  61.         y=F(x_answ)
  62.         dictionary[x_answ]=y
  63.         print("Найденный минимум: x =",f"{x_answ:.3f}","y =",f"{y:.3f}","e =",e,"it =",it)
  64.     return dictionary
  65.  
  66. def check(dict,x):
  67.     keys = dict.keys()
  68.     for val in keys:
  69.         if(val==x):
  70.             return False
  71.     return True
  72.  
  73.  
  74.  
  75.  
  76. a=4
  77. b=20
  78. m=80
  79. h=(b-a)/m
  80. e=10**(-2)
  81. answ=dict()
  82. print_values(a,b,m)
  83.  
  84. while len(answ)!=3:
  85.      x=float(input("\nВведите x: "))
  86.      uer=float(input("\nВведите h: "))#//////////
  87.      answ=MP3(x,e,h,F,F1,answ)
  88.  
  89. print_func(a,b,m)
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement