KgCro

bisect, newton,...

Jan 19th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Tue Oct 22 11:17:19 2019
  5.  
  6. @author: student
  7. """
  8.  
  9. import numpy as np
  10. import matplotlib.pyplot as plt
  11. import scipy.optimize as opt
  12.  
  13. def f1(t):
  14.     return t**3 - 6*t**2 + 11*t +np.cos(t/10) - 50
  15.  
  16. def f2(t, a, b):
  17.     return t**3 - 6*t**2 + 11*t +np.cos(t/a) - b
  18.  
  19. def der_f1(t):
  20.     return 3*t**2 - 12*t + 11 - 0.1*np.sin(t/10)
  21.  
  22. x1 =  np.linspace(-5,8,200)
  23. y1 = f1(x1)
  24.  
  25. #xl = float(input('odaberi xlijevo: '))
  26. #print(xl, type(xl))
  27.  
  28.  
  29. def bisekcija():
  30.     xl =-4
  31.     xd = 7
  32.     if f1(xl)*f1(xd)>0:
  33.         print(' nije dobro!')
  34.    
  35.     sol = 1
  36.  
  37.     yl =f1(xl)
  38.     yd = f1(xd)
  39.  
  40.     brojac = 0
  41.     while sol > 1.e-04:
  42.    
  43.     #plt.plot(xl, yl, 'o')
  44.     #plt.plot(xd, yd, 'o')
  45.  
  46.         xs = (xl+xd)/2
  47.         ys = f1(xs)
  48.     #plt.plot(xs, ys, 'D')
  49.  
  50.         sol = np.abs(ys)
  51.    
  52.    
  53.         if yl *ys <0:
  54.             xd = xs
  55.         else:
  56.             xl = xs
  57.         if brojac > 3:
  58.             break
  59.    
  60.         brojac  += 1
  61.    
  62.    
  63.     print('xs :', xs)
  64.     xb = opt.bisect(f1, -5, 8)  
  65.     print("xb: ", xb)
  66.  
  67.     plt.plot(x1, y1)
  68.  
  69. #plt.plot([xl,xd], [yl,yd], #linewidth = 4,
  70. #         linestyle ='--',
  71. #         color = 'salmon',
  72. #         marker = 'D',
  73. #         markersize = 10)
  74.     plt.axhline(0, color='r')     #traženje nultoče
  75.     plt.show()
  76.  
  77.  
  78. def newton():
  79.     x = np.linspace(-5, 8, 200)
  80.     y = f1(x)
  81.    
  82.     x0 = 8
  83.     y0 = f1(x0)
  84.     sol = np.abs(y0)
  85.     brojac = 0
  86.     while sol  > 1.e-04:
  87.        
  88.         x1 = x0 - f1(x0)/ der_f1(x0)
  89.         y1 = f1(x1)
  90.         plt.plot([x0, x1, x1], [y0, 0, y1], "-D")
  91.         x0 = x1
  92.         y0 = f1(x0)
  93.         sol = np.abs(y0)
  94.         brojac += 1
  95.         if brojac > 100:
  96.             break
  97.     print("x1: ", x1)
  98.     nx = opt.newton(f1, 8)
  99.     print("nx: ", nx)
  100.     fx = opt.fsolve(f1, 8)
  101.     print("fx: ", fx, type(fx))
  102.     plt.plot(x, y)
  103.     plt.axhline(0, c = "r")
  104.     plt.show()
  105.  
  106. #newton()
  107.  
  108.  
  109. def nova():
  110.     x = np.linspace(-5, 8, 200)
  111.     y = f2(x, a = 10, b = 50)
  112.     fx = opt.fsolve(f1, 8)
  113.     print("fx: ", fx, type(fx))
  114.     fx = opt.fsolve(f2, 8, args = (10, 50))     # args = argumenti
  115.     print("fx: ", fx, type(fx))
  116.     plt.plot(x, y)
  117.     plt.axhline(0, c = "r")
  118.     plt.plot(plt.xlim(), [100, 100], "g")
  119.     plt.show()
  120.  
  121.  
  122. if __name__ == "__main__":    
  123.     nova()
Add Comment
Please, Sign In to add comment