'============================================================================ ' G.H. George SLIDE2.BAS 1999 02 04 ' Print values of speed and distance for a block sliding against friction. '============================================================================ ' Procedures: DECLARE SUB Welcome () ' Print welcome message DECLARE SUB GetValues (mass#, slope#, mu#, k#) ' Get parameter values DECLARE SUB PrintTable (mass#, slope#, mu#, k#) ' Solve & print s(t), v(t) DECLARE SUB GetTimes (start#, NumRows#, dt#) DECLARE FUNCTION s# (t AS DOUBLE, a#, b#) DECLARE FUNCTION v# (t AS DOUBLE, a#, b#) ' Global constants: ' False: logical false (= 0) ' True: logical true (= -1) ' Pi: pi ' g: acceleration due to gravity DEFINT F, T DEFDBL A-E, G-S, U-Z CONST False = 0, True = NOT False CONST Pi = 3.141592653589794# CONST g = 9.8065 ' Variables: ' mass: mass of sliding block ' slope: angle (in radians) of the slope ' mu: coefficient of friction ' k: air drag coefficient CALL Welcome CALL GetValues(mass, slope, mu, k) CALL PrintTable(mass, slope, mu, k) END DEFSNG F, T SUB GetTimes (start, NumRows, dt) '---------------------------------------------------------------------------- ' Get start time, number of times and interval between times for the ' printed table. '---------------------------------------------------------------------------- ' Parameters: ' start: first value of time at which to evaluate s and v. ' NumRows: number of rows in the printed table ' dt: interval between consecutive values of time in the table ' No procedures. ' Local variables: ' StopTime: the last time to appear in the table. INPUT "Enter the desired start time: ", start DO WHILE start < 0 COLOR 12, 0 PRINT "Input error: the start time must be non-negative. Try again." COLOR 7, 0 INPUT "Enter the start time: ", start LOOP INPUT "Enter the number of values required (from 1 to 20): ", NumRows DO WHILE NumRows <> INT(NumRows) OR NumRows < 1 OR NumRows > 20 COLOR 12, 0 PRINT "Input error: "; NumRows; " is an invalid choice. Try again." COLOR 7, 0 INPUT "Enter the number of values required (from 1 to 20): ", NumRows LOOP INPUT "Enter the desired stop time: ", StopTime DO WHILE StopTime <= start COLOR 12, 0 PRINT "Input error: the time interval must be positive. Try again." COLOR 7, 0 INPUT "Enter the desired stop time: ", StopTime LOOP LET dt = (StopTime - start) / NumRows END SUB DEFINT F, T SUB GetValues (mass, slope, mu, k) '---------------------------------------------------------------------------- ' Obtain and validate parameter values from the user. '---------------------------------------------------------------------------- ' Parameters: ' mass: mass of sliding block ' slope: angle (in radians) of the slope ' mu: coefficient of friction ' k: air drag coefficient ' No procedures or local variables. DIM t AS DOUBLE INPUT "Enter the mass m (kg) of the block: ", mass DO WHILE mass <= 0 COLOR 12, 0 PRINT "Input error: the mass must be positive. Try again." COLOR 7, 0 INPUT "Enter the mass m (kg) of the block: ", mass LOOP PRINT DO INPUT "Enter the angle of the slope (in degrees): ", slope DO WHILE slope <= 0 OR slope >= 90 COLOR 12, 0 PRINT "Input error: the angle must be in the first quadrant. "; PRINT "Try again." COLOR 7, 0 INPUT "Enter the angle of the slope (in degrees): ", slope LOOP LET slope = slope * Pi / 180 ' Convert degrees to radians. PRINT PRINT "Enter the coefficient of (surface) friction "; CHR$(230); INPUT ": ", mu DO WHILE mu < 0 COLOR 12, 0 PRINT "Input error: "; CHR$(230); " must be non-negative. "; PRINT "Try again." COLOR 7, 0 PRINT "Enter the coefficient of (surface) friction "; CHR$(230); INPUT ": ", mu LOOP IF TAN(slope) <= mu THEN COLOR 12, 0 PRINT "Error: the slope is too small compared to the static "; PRINT "friction" PRINT " to allow the block to begin to move." PRINT "Please try new values of angle and "; CHR$(230); "." PRINT COLOR 7, 0 END IF LOOP UNTIL TAN(slope) > mu PRINT INPUT "Enter the coefficient of air drag k: ", k DO WHILE k < 0 COLOR 12, 0 PRINT "Input error: k must be non-negative. Try again." COLOR 7, 0 INPUT "Enter the coefficient of air drag k: ", k LOOP PRINT END SUB SUB PrintTable (mass, slope, mu, k) '---------------------------------------------------------------------------- ' Print a table of values of time t , distance s and speed v . '---------------------------------------------------------------------------- ' Parameters: ' mass: mass of sliding block ' slope: angle (in radians) of the slope ' mu: coefficient of friction ' k: air drag coefficient ' Procedures: ' GetTimes: get values of start, NumRows & dt (as below) ' s: distance (as a function of time) ' v: speed (as a function of time) ' Local variables: ' a, b: combination of parameters in (bv - a)dt + dv = 0 ' (see LET statements below) ' t: elapsed time since the start of the slide ' start: first value of time at which to evaluate s and v. ' NumRows: number of rows in the printed table ' dt: interval between consecutive values of time in the table ' row%: loop counter = current row in the table DIM t AS DOUBLE LET a = g * (SIN(slope) - mu * COS(slope)) LET b = k / mass PRINT #1, CHR$(12) ' Page break PRINT #1, TAB(31); "Sliding block program" PRINT #1, TAB(31); "Dr. G.H. George, 1999" PRINT #1, PRINT #1, "Parameters: " PRINT #1, " g = "; g; "m/s^2" PRINT #1, " Mass = "; mass; "kg" PRINT #1, USING " Slope angle = ##.##### "; slope; PRINT #1, USING "rad = ###.# deg"; slope * 180 / Pi PRINT #1, "Coefficient of friction = mu = "; mu PRINT #1, "Coefficient of air drag = k = "; k PRINT #1, "a ="; a; TAB(28); "b ="; b; IF b > 0 THEN PRINT #1, USING " Terminal speed =###.##### m/s"; a / b ELSE PRINT #1, " Speed increases without bound." END IF PRINT #1, PRINT #1, TAB(10); "Time (s)"; TAB(30); "s(t)"; TAB(45); "v(t)" PRINT #1, CLS PRINT TAB(31); "Sliding block program" PRINT TAB(31); "Dr. G.H. George, 1999" PRINT COLOR 10, 0 ' Bright green on black PRINT "Parameters: " PRINT " g = "; g; "m/s^2" PRINT " Mass = "; mass; "kg" PRINT USING " Slope angle = ##.##### "; slope; PRINT USING "rad = ###.# deg"; slope * 180 / Pi PRINT "Coefficient of friction = mu = "; mu PRINT "Coefficient of air drag = k = "; k PRINT "a ="; a; TAB(28); "b ="; b; IF b > 0 THEN PRINT USING " Terminal speed =###.##### m/s"; a / b ELSE PRINT " Speed increases without bound." END IF COLOR 7, 0 PRINT PRINT "The distance s travelled and speed v at a range of times "; PRINT "will be" PRINT "printed." PRINT CALL GetTimes(start, NumRows, dt) COLOR 15, 0 ' Bright white on black PRINT TAB(10); "Time (s)"; TAB(30); "s(t)"; TAB(45); "v(t)" PRINT FOR row% = 1 TO CINT(NumRows) + 1 t = start + (row% - 1) * dt PRINT #1, TAB(8); USING "#####.####"; t; PRINT #1, TAB(24); USING "#######.####"; s(t, a, b); PRINT #1, TAB(40); USING "######.####"; v(t, a, b) PRINT TAB(8); USING "#####.####"; t; PRINT TAB(24); USING "#######.####"; s(t, a, b); PRINT TAB(40); USING "######.####"; v(t, a, b) NEXT row% CLOSE END SUB FUNCTION s (t AS DOUBLE, a, b) '---------------------------------------------------------------------------- ' Returns distance travelled at time t , with parameters a, b '---------------------------------------------------------------------------- ' Parameters: (as above) ' No procedures or local variables. IF b > 0 THEN LET s = a * (b * t + EXP(-b * t) - 1) / b ^ 2 ELSE LET s = a * t * t / 2 ' Case of no air drag. END IF END FUNCTION FUNCTION v (t AS DOUBLE, a, b) '---------------------------------------------------------------------------- ' Returns speed at time t , with parameters a, b. '---------------------------------------------------------------------------- ' Parameters: (as above) ' No procedures or local variables. IF b > 0 THEN LET v = a * (1 - EXP(-b * t)) / b ELSE LET v = a * t ' Case of no air drag. END IF END FUNCTION DEFSNG A-Z SUB Welcome '---------------------------------------------------------------------------- ' Open output file & print welcome message to screen and file. '---------------------------------------------------------------------------- ' No parameters or procedures. ' Local variables: ' FileName$: name of output file CLS PRINT TAB(31); "Sliding block program" PRINT TAB(31); "Dr. G.H. George, 1999" PRINT PRINT "A block of mass m is sliding down an inclined slope of angle alpha" PRINT "from rest under the influence of gravity, friction with the surface" PRINT "(with a coefficient of friction "; CHR$(230); ") and air resistance"; PRINT " kv, (where" PRINT " k is the drag coefficient and v is the speed)." PRINT PRINT "Unless you specify otherwise, the output file will be 'c:\t.txt'." PRINT "Just press the 'Enter' key at this prompt to accept this default." PRINT COLOR 12, 0 ' Bright red on black INPUT "Enter a valid DOS file name for the output file: ", FileName$ ' *** NOTE lack of data validation here. COLOR 7, 0 PRINT IF FileName$ = "" THEN LET FileName$ = "c:\t.txt" ' Default name. OPEN FileName$ FOR OUTPUT AS #1 PRINT #1, TAB(31); "Sliding block program" PRINT #1, TAB(31); "Dr. G.H. George, 1999" PRINT #1, PRINT #1, "A block of mass m is sliding down an inclined slope of "; PRINT #1, "angle alpha" PRINT #1, "from rest under the influence of gravity, friction with the "; PRINT #1, "surface " PRINT #1, "(with a coefficient of friction "; CHR$(230); ") and air "; PRINT #1, "resistance kv, (where" PRINT #1, " k is the drag coefficient and v is the speed)." PRINT #1, END SUB