home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Transactor
/
Transactor_18_1987_Transactor_Publishing.d64
/
bisect
(
.txt
)
< prev
next >
Wrap
Commodore BASIC
|
2023-02-26
|
800b
|
36 lines
100 rem sample program to find the
110 rem roots of the function
120 rem
130 rem y=(x-3)(x-1)(x+4)
140 rem
150 rem which we know are x=1,3, and -4.
160 rem
170 rem try the intervals (0,2), (2,0)
180 rem and (-5,-3)
190 :
200 deffnf(x) = (x-3)*(x-1)*(x+4)
210 print
220 input"left endpoint";a
230 input"right endpoint";b
240 input"tolerance";tl
250 print
260 gosub 380
270 if er=1 then print "no zero was found between";a;"and";b: goto 210
280 print "root is at:";mid: goto 210
290 :
300 rem **************************
310 :
320 rem bisect subroutine, used to
330 rem find the zero of a function
340 rem between points a and b, with
350 rem tolerance tl. if no zero
360 rem exists, er=1.
370 rem
380 fl=fnf(a): fr=fnf(b): er=0
390 if fl*fr > 0 then er=1: return
400 mid = (a+b)/2
410 if abs(a-mid) <= tl then return
420 fm = fnf(mid)
430 if fl*fm <= 0 then b = mid: goto 400
440 a = mid: fl = fm: goto 400