Advertisement
Guest User

Vjezbe #3

a guest
Oct 22nd, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Tue Oct 22 08:18:14 2019
  5.  
  6. @author: student
  7. """
  8.  
  9. import numpy as np
  10. import matplotlib.pyplot as plt
  11.  
  12. #def f(x):
  13. # return 2*x**2 - 4
  14. #
  15. #xl = float(input("Unesi lijevu granicu: "))
  16. #xd = float(input("Unesi desnu granicu: "))
  17. #epsilon = float(input("Unesi dopustenu gresku: "))
  18. #
  19. #x = np.linspace(xl, xd, 100)
  20. #
  21. #plt.plot(x, f(x),'r-')
  22. #plt.axhline(color='black')
  23. #
  24. #maxiter = 100
  25. #it = 0
  26. #
  27. #while it < maxiter:
  28. # xs = (xd+xl) * 0.5
  29. #
  30. # error = np.abs(f(xs))
  31. #
  32. # if error < epsilon:
  33. # print("Dopustena greska zadovoljena")
  34. # break
  35. # elif f(xs) * f(xd) < 0:
  36. # xl = xs
  37. # else:
  38. # xd = xs
  39. # it = it + 1
  40. # print (it, xs)
  41. #
  42. #print("Bisekcija x: ", xs)
  43. #plt.plot(xs, 0, 'bo')
  44.  
  45. #def f(x):
  46. # return 2*x**2 - 4
  47. #
  48. #def f_der(x):
  49. # return 4*x
  50.  
  51. #x1 = float(input("Unesi pocetnu tocku: "))
  52. #epsilon = float(input("Unesi dopustenu gresku: "))
  53. #
  54. #X = np.array([x1])
  55. #maxit = 100
  56. #it = 1
  57. #
  58. #while it < maxit:
  59. # Xi = X[-1] - f(X[-1]) / f_der(X[-1])
  60. # X = np.append(X, Xi)
  61. #
  62. # error = np.abs(X[-1] - X[-2])
  63. # if error < epsilon:
  64. # break
  65. #
  66. # it += 1
  67. #
  68. #print(it, X[-1])
  69.  
  70. import scipy.optimize as opt
  71.  
  72. #x0 = opt.fsolve(f, 2)
  73. #print ("nultocka scipy: ", x0[0])
  74.  
  75.  
  76. #def f(x):
  77. # return np.cos((x/2.2) -5.5) *5
  78. #
  79. #x = np.linspace(0, 24, 100)
  80. #plt.plot(x, f(x), 'b-')
  81. #plt.axhline(color = 'black')
  82. #
  83. #x0 = opt.fsolve(f, [1,8,14,20])
  84. #print("nultocka: ", x0)
  85. #plt.plot(x0, [0, 0, 0, 0], 'k*', label ='0*C')
  86. #plt.legend()
  87.  
  88. def f(x):
  89. return 1250 * np.sin(x - 2) + 250 * x + 1100 - 1250
  90.  
  91. x = np.linspace(0,30,100)
  92. plt.plot(x, f(x), 'r--')
  93. plt.axhline(color='black')
  94.  
  95. vrijeme = opt.fsolve(f, 2)
  96. print (vrijeme[0])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement