SHARE
TWEET

Untitled

a guest Feb 27th, 2020 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from matplotlib import pyplot as plt
  2. import tkinter as tk
  3. import math
  4. import numpy as np
  5. def f(x):
  6.     return np.cos(x)
  7. def df(x):
  8.     return np.cos(x)
  9. def d2f(x):
  10.     return -np.sin(x)
  11.  
  12. def graph(): #График
  13.     global a,b
  14.     x1 = np.linspace(a.get(), b.get(), 10 ** 5, dtype = float)
  15.     y1 = [f(el) for el in x1]
  16.     x2, y2 = [], []
  17.     for el in x1:
  18.         if np.fabs(d2f(el)) <= 1e-3:
  19.             x2.append(el)
  20.             y2.append(f(el))
  21.     plt.plot(x1, y1, x1, np.zeros(10 ** 5, dtype = float), 'r', x2, y2, 'gs')
  22.     plt.legend(('f(x)', 'Ось абсцисс', 'Точки перегиба'))
  23.     plt.grid(b = True)
  24.     plt.show()
  25.    
  26. def table():#таблица
  27.     global af,hf,bf,ef
  28.     af=a.get()
  29.     bf=b.get()
  30.     hf=h.get()
  31.     ef=e.get()
  32.    
  33.     korni()
  34.     win=tk.Tk()
  35.     win.title('Результат')
  36.  
  37.     win.resizable(True,True)
  38.     tk.Label(win,text="№",font=('Colibri',15),fg='red').grid(row=0, column=0,padx=20)
  39.     tk.Label(win,text="Начало интервала",font=('Colibri',15),fg='red').grid(row=0, column=1,padx=20)
  40.     tk.Label(win,text="Конец интервала",font=('Colibri',15),fg='red').grid(row=0, column=2,padx=20)
  41.     tk.Label(win,text="Значение корня",font=('Colibri',15),fg='red').grid(row=0, column=3,padx=20)
  42.     tk.Label(win,text="Значение функции",font=('Colibri',15),fg='red').grid(row=0, column=4,padx=20)
  43.     tk.Label(win,text="Кол-во итераций",font=('Colibri',15),fg='red').grid(row=0, column=5,padx=20)
  44.     for i in range(len(kon)):
  45.         for j in range(len(kon[i])):
  46.             tk.Label(win, text = kon[i][j], font=('Colibri',15)).grid(row=i+1, column=j)
  47.    
  48.     win.mainloop()
  49. def korni():
  50.    
  51.     global af,bf,hf,ef
  52.     i=0;
  53.     while True:
  54.         xmin=af+hf*i
  55.         i+=1
  56.         xmax=af+hf*i
  57.         if xmin>bf:
  58.             break
  59.         if xmax>bf:
  60.             xmax=bf
  61.         fmin=f(xmin)
  62.         fmax=f(xmax)
  63.         if fmin*fmax>0:
  64.             continue
  65.         t=0
  66.         x1=xmin
  67.         x2=xmax
  68.         while abs(x1-x2)>ef:
  69.             x=(x1+x2)/2
  70.             fx=f(x)
  71.             if fx*fmin>0:
  72.                 x1=x
  73.             else:
  74.                 x2=x
  75.             t+=1
  76.         pred=[i,xmin, xmax, x, fx, t]
  77.         kon.append(pred)
  78.         print(kon)
  79.  
  80. kon=[]
  81. pred=[]
  82. root=tk.Tk()
  83. root.title('Уточнение корней')
  84. root.geometry('360x200')
  85. root.resizable(False,False)
  86. a=tk.DoubleVar()
  87. b=tk.DoubleVar()
  88. h=tk.DoubleVar()
  89. e=tk.DoubleVar()
  90. aentry=tk.Label(root,text="a :",font=('Colibri',15),fg='red').grid(row=1, column=0)
  91. bentry=tk.Label(root,text="b :",font=('Colibri',15),fg='red').grid(row=2, column=0)
  92. hentry=tk.Label(root,text="step :",font=('Colibri',15),fg='red').grid(row=3, column=0)
  93. eentry=tk.Label(root,text="eps :",font=('Colibri',15),fg='red').grid(row=4, column=0)
  94. a_entry=tk.Entry(root,font=('Colibri',15), textvariable = a)
  95. a_entry.grid(row=1, column=1, pady=5, padx=5)
  96. b_entry=tk.Entry(root,font=('Colibri',15), textvariable = b)
  97. b_entry.grid(row=2, column=1, pady=5, padx=5)
  98. h_entry=tk.Entry(root,font=('Colibri',15), textvariable = h)
  99. h_entry.grid(row=3, column=1, pady=5, padx=5)
  100. e_entry=tk.Entry(root,font=('Colibri',15), textvariable = e)
  101. e_entry.grid(row=4, column=1, pady=5, padx=5)
  102. b1_entry=tk.Button(root,command=table,text='Таблица',width=10,height=1,font=('Colibri',15))
  103. b1_entry.grid(row=7, column=0, pady=5, padx=5)
  104. b2_entry=tk.Button(root,command=graph,text='График',width=10,height=1,font=('Colibri',15))
  105. b2_entry.grid(row=7, column=1, pady=5, padx=5)
  106.  
  107. root.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top