Advertisement
Guest User

trapSimp

a guest
Jan 23rd, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1.  
  2. from numpy import *
  3. from pylab import *
  4. from scipy import integrate
  5.  
  6. def f(x):
  7. return x**2-x+1
  8.  
  9. a=1.0
  10. b=3.0
  11. c=(a+b)/2.
  12.  
  13. x1=array([a,b])
  14. x2=array([a,b,c])
  15. y1=f(x1)
  16. y2=f(x2)
  17.  
  18. iTrap=trapz(y1,x1)
  19. print ('Rjesenje pomocu trapezne:'), iTrap
  20.  
  21. iSimp=integrate.simps(y2,x2)
  22. print ('Rjesenje pomocu Simps.:'), iSimp
  23.  
  24. from numpy import *
  25. from pylab import *
  26. from scipy import *
  27. from scipy import interpolate
  28.  
  29. def f(x):
  30. return x**2-x+1
  31.  
  32. a=1.0
  33. b=3.0
  34. c=(a+b)/2.
  35.  
  36. x1=array([a,b])
  37. x2=array([a,b,c])
  38. y1=f(x1)
  39. y2=f(x2)
  40.  
  41. iTrap=trapz(y1,x1)
  42. print ('Rjesenje pomocu trapezne:'), iTrap
  43.  
  44. iSimp=integrate.simps(y2,x2)
  45. print ('Rjesenje pomocu Simps.:'), iSimp
  46.  
  47. integral=3**3/3.+3**2/2.
  48. print ('Tocno rjesenje:'), integral
  49.  
  50. print ('Prava greska trap:'), (abs(integral-iTrap))
  51. print ('Prava greska Simp:'), (abs(integral-iSimp))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement