'============================================================================ ' G.H. George Created: 1992 SEP 30 ' TAYLOR.BAS Modified: 1998 MAY 01 ' to illustrate how Taylor polynomials approximate y = cos x . '============================================================================ ' Procedures: ' IntroScreen: print the welcome message ' Choose: ask the user to choose the order n of the Taylor fn. ' DrawGraph: draw the cosine and Taylor polynomial functions. DECLARE SUB Axes (n AS INTEGER) DECLARE SUB Choose (n AS INTEGER) DECLARE SUB Cosine () DECLARE SUB DrawGraph (n AS INTEGER) DECLARE SUB IntroScreen () DECLARE SUB PressAnyKey () DECLARE SUB Taylor (n AS INTEGER) DECLARE SUB WaitForKey (KeyPressed AS STRING) DECLARE FUNCTION T! (n AS INTEGER, x!) ' Global constants: ' False: logical false (= 0) ' True: logical true (= -1) ' Pi: pi DEFINT F, T CONST False = 0, True = NOT False CONST Pi = 3.14159265# ' Variables: ' n: the order of the Taylor polynomial approximating cos x . DIM n AS INTEGER CALL IntroScreen LET n = 2 DO CALL DrawGraph(n) CALL Choose(n) LOOP UNTIL n < 0 END DEFSNG F, T SUB Axes (n AS INTEGER) '============================================================================ ' Enter graphics mode, draw axes and graph title. '============================================================================ ' No procedures. ' Parameter: ' n: the order of the Taylor polynomial approximating cos x ' Local variable: ' i: loop counter = a pixel coordinate DIM i AS INTEGER SCREEN 7: COLOR 3, 0 ' Graphics screen, cyan on black. CLS LINE (0, 100)-(319, 100), 3 ' x-axis, cyan. LOCATE 12, 39 PRINT "x" LINE (160, 180)-(160, 20), 3 ' y-axis. LOCATE 2, 19 PRINT "y" FOR i = 20 TO 180 STEP 20 LINE (158, i)-(162, i), 3 ' tick marks every one unit, y-axis. NEXT i LOCATE 3, 22 PRINT "4" FOR i = 20 TO 620 STEP 75 LINE (i / 2, 102)-(i / 2, 98), 3 ' tick marks every pi units, x-axis. NEXT i LOCATE 14, 29 PRINT "2"; CHR$(227); ' CHR$(227) = pi PRINT TAB(38); "4"; CHR$(227) LOCATE 1, 1 ' Title for graphs. COLOR 10, 0 ' bright green on black PRINT "y = cos x ;"; COLOR 12, 0 ' bright red on black PRINT " y = Taylor_"; LTRIM$(STR$(n)); "(x)" COLOR 7, 0 ' white on black (default) END SUB SUB Choose (n AS INTEGER) '============================================================================ ' Ask the user for the order of Taylor polynomial to plot next. '============================================================================ ' Procedures: ' WaitForKey: pause until any key is pressed ' Parameter: ' n: the order of the Taylor polynomial approximating cos x ' Local variables: ' KeyPressed: the key pressed by the user (not needed here) DIM KeyPressed AS STRING COLOR 7, 0 LOCATE 22, 1 PRINT " = next value of n" PRINT " "; CHR$(34); "-"; CHR$(34); " = previous value of n " PRINT " "; CHR$(34); "Q"; CHR$(34); " = exit from the program "; LOCATE 1, 1 CALL WaitForKey(KeyPressed) SELECT CASE KeyPressed CASE "Y", "F", "N" ' Includes and space IF n < 36 THEN LET n = n + 2 ' Maximum allowed n is 36 CASE "-", "B", "P" IF n > 0 THEN LET n = n - 2 ' Minimum allowed n is 0 CASE "Q", "X", "E" LET n = -1 ' n < 0 is the exit flag CASE ELSE ' Do nothing: repeat present graph END SELECT END SUB SUB Cosine '============================================================================ ' Plot the curve y = cos x . '============================================================================ ' No procedures or parameters. ' Local variables: ' pixel: loop counter = horizontal pixel coordinate ' x: x coordinate correspoding to pixel ' y: value of cos x ; then corresponding pixel coordinate DIM pixel AS INTEGER PSET (10, 80), 10 ' Plot first point (-4Pi, 1) FOR pixel = 10 TO 310 STEP 2 LET x = (pixel - 160) * 2 * Pi / 75 ' -4 pi < x < +4 pi LET y = COS(x) LET y = 100 - CINT(y * 20) ' convert y to pixel coordinates. LINE -(pixel, y), 10 ' colour = bright green NEXT pixel END SUB SUB DrawGraph (n AS INTEGER) '============================================================================ ' Having chosen n , plot y = cos x and y = T_n (x) . '============================================================================ ' Procedures: ' Axes: Ask the user for the order of Taylor polynomial to plot next ' Cosine: Plot the curve y = cos x ' Taylor: Draw the graph of the Taylor polynomial y = Tn (x) ' Parameter: ' n: the order of the Taylor polynomial approximating cos x ' Local variables: ' CALL Axes(n) CALL Taylor(n) CALL Cosine END SUB SUB IntroScreen '============================================================================ ' Preliminary screen of information to the user. '============================================================================ ' SCREEN 0: WIDTH 80 CLS PRINT " Illustration of Taylor polynomial approximations to y = cos x ." PRINT PRINT " You will be able to cycle through values for n " PRINT " from 0 to 36." PRINT " The graph of the nth order Taylor polynomial will be" PRINT " plotted on top of the graph of y = cos x" CALL PressAnyKey SCREEN 7 END SUB SUB PressAnyKey '============================================================================ ' Prompts the user to press any key -- program pauses until user complies. '============================================================================ ' Procedures: ' WaitForKey: pause until any key is pressed ' Parameters: ' ' Local variables: ' KeyPressed: the key pressed by the user (not needed here) DIM KeyPressed AS STRING LOCATE 23, 1 COLOR 15, 0 ' bright text. PRINT "Press any key to continue: "; COLOR 7, 0 ' normal text. CALL WaitForKey(KeyPressed) END SUB FUNCTION T (n AS INTEGER, x) '============================================================================ ' Evaluate the nth Taylor polynomial at the current value of x . '============================================================================ ' general term = (-1)^n x^(2n) / (2n)! ' No procedures. ' Parameters: ' n: the order of the Taylor polynomial approximating cos x ' x: the current x coordinate at which the function value is needed ' Local variables: ' Term: loop counter = (power of x in current term) / 2 ' ThisTerm: value of current term in the Taylor series ' PartialSum: partial sum of terms in the Taylor series DIM Term AS INTEGER LET Term = 1 LET ThisTerm = 1 LET PartialSum = 1 ' Terminate sum early if terms become very small. DO WHILE Term <= n / 2 AND ABS(ThisTerm) > .0000001 LET ThisTerm = ThisTerm * (-x) * x / (2 * Term * (2 * Term - 1)) LET PartialSum = PartialSum + ThisTerm LET Term = Term + 1 LOOP LET T = PartialSum END FUNCTION SUB Taylor (n AS INTEGER) '============================================================================ ' Draw the graph of the n'th order Taylor polynomial y = Tn (x) . '============================================================================ ' Procedures: ' T: value of the n'th order Taylor polynomial at the current x ' Parameters: ' n: the order of the Taylor polynomial approximating cos x ' Local variables: ' y: y coordinate or vertical pixel coordinate ' pixel: loop counter = horizontal pixel coordinate ' x: x coordinate DIM pixel AS INTEGER LET y = T(n, (-4 * Pi)) ' Find Tn at the starting point. IF ABS(y) < 5 THEN ' Guard against points off screen LET y = 100 - CINT(y * 20) ' convert to pixel coordinates. ELSEIF y < 0 THEN LET y = 300 ' Off the bottom of the screen ELSE LET y = -100 ' Off the top of the screen END IF PSET (10, y), 12 ' set cursor, colour = bright red. FOR pixel = 10 TO 310 STEP 4 LET x = (pixel - 160) * 2 * Pi / 75 ' -4 pi < x < +4 pi LET y = T(n, x) IF ABS(y) < 5 THEN ' Guard against points off the screen LET y = 100 - CINT(y * 20) ELSEIF y < 0 THEN LET y = 300 ELSE LET y = -100 END IF LINE -(pixel, y), 12 NEXT pixel END SUB SUB WaitForKey (KeyPressed AS STRING) '============================================================================ ' - cause program to pause until a key is pressed. '============================================================================ ' No procedures or local variables. ' Parameters: ' KeyPressed: the key pressed by the user DO LET KeyPressed = UCASE$(INKEY$) LOOP UNTIL KeyPressed <> "" IF ASC(KeyPressed) = 13 OR ASC(KeyPressed) = 32 THEN LET KeyPressed = "Y" ' ^-- treat and as a "YES" response. END SUB