Facebook
From Chocolate Peafowl, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 553
  1. # program liczący miejsce zerowe funkcji metodą bisekcji
  2.  
  3.  
  4. epi=0.00005  #dokładność
  5. x1=0       #początek przedziału
  6. x2=10         #koniec przedziału
  7. x=(x1+x2)/2  #wartość średnia
  8.  
  9. def funk (x):
  10.     y=2*x*x+3*x-5 #szukamy miejsca zerowego dla tej funkcji
  11.     return y
  12.  
  13. y=funk (x)
  14. print ('epi=',epi)
  15. print ('x1=',x1)
  16. print ('x2=',x2)
  17. print ('x=',x)
  18. print ('')
  19.  
  20.  
  21. while abs(y)>epi:
  22.     x=(x1+x2)/2
  23.     y=funk (x)
  24.     y1=funk (x1)
  25.     y2=funk (x2)
  26.     print('y1=',y)
  27.     print('y=',y1)
  28.     print('y2=',y2)
  29.     print ('')
  30.  
  31.     if y>0:
  32.         z='+'
  33.     elif y<0:
  34.         z='-'
  35.  
  36.     if y1>0:
  37.         z1='+'
  38.     elif y1<0:
  39.         z1='-'
  40.  
  41.     if z1!=z:
  42.         x2=x
  43.     else:
  44.         x1=x
  45.  
  46.    
  47.  
  48. print ('')
  49. print ('y=',y)
  50. print ('')
  51. print ('f(',x,')=0')
  52.    
  53.  
  54.    
  55.    
  56.    
  57.  

Replies to Untitled rss

Title Name Language When
f(x)=0 bisekcja Mike C. python 5 Years ago.