home *** CD-ROM | disk | FTP | other *** search
/ Transactor / Transactor_18_1987_Transactor_Publishing.d64 / bisect (.txt) < prev    next >
Commodore BASIC  |  2023-02-26  |  800b  |  36 lines

  1. 100 rem sample program to find the
  2. 110 rem roots of the function
  3. 120 rem
  4. 130 rem y=(x-3)(x-1)(x+4)
  5. 140 rem
  6. 150 rem which we know are x=1,3, and -4.
  7. 160 rem
  8. 170 rem try the intervals (0,2), (2,0)
  9. 180 rem and (-5,-3)
  10. 190 :
  11. 200 deffnf(x) = (x-3)*(x-1)*(x+4)
  12. 210 print
  13. 220 input"left endpoint";a
  14. 230 input"right endpoint";b
  15. 240 input"tolerance";tl
  16. 250 print
  17. 260 gosub 380
  18. 270 if er=1 then print "no zero was found between";a;"and";b: goto 210
  19. 280 print "root is at:";mid: goto 210
  20. 290 :
  21. 300 rem **************************
  22. 310 :
  23. 320 rem bisect subroutine, used to
  24. 330 rem find the zero of a function
  25. 340 rem between points a and b, with
  26. 350 rem tolerance tl.  if no zero
  27. 360 rem exists, er=1.
  28. 370 rem
  29. 380 fl=fnf(a): fr=fnf(b): er=0
  30. 390 if fl*fr > 0 then er=1: return
  31. 400 mid = (a+b)/2
  32. 410 if abs(a-mid) <= tl then return
  33. 420 fm = fnf(mid)
  34. 430 if fl*fm <= 0 then b = mid: goto 400
  35. 440 a = mid: fl = fm: goto 400
  36.