Some simple physics utility programs in BASIC. by Donald E. Simanek

BASIC is a neat language, a handy, free (with DOS), calculational tool,
which is always readily available on your computer. Many times I'll pop
into basic and do a one-liner or few-liner calculation rather than get
out my hand calculator.

These programs are in vanilla BASIC, with few frills. Feel free to
improve/modify them, and be kind enough to send your improvements back to me.

Some of these were written for my Tandy 100 portable, which I take
to lab with me. All of these programs should be exportable to any computer
which runs basic, since they use no fancy screen graphics.

These programs resulted from necessity, to avoid drudgery of things done
often.

Titles, all caps, precede each, and they have a short DOC preceding them.
Use a text editor to extract only the numbered lines to a file with a .BAS
extension, then run it under BASIC. These may be compiled, but most are
so short that that's not necessary.

HELMERT's EQUATION FOR g.

Helmert's equation may be found in the Handbook of Chemistry and Physics.
It gives the value of the acceleration due to gravity as a function of
latitude and elevation above sea level. This program carries out the
calculation for you. It allows you to enter latitude in degrees and
minutes. Or the degree entry may have a decimal form, in which case
you simply hit ENTER at the 'minutes' prompt.

You are prompted for elevation in meters. If you enter zero, or just
hit ENTER, you'll be prompted for elevation in feet.

Exercises: See how much difference in g between equator, 45 degrees,
90 degrees (at the poles). See how much difference between sea level
and the top of a mountain.

How much is g at the height of an orbiting space station (a couple of
hundred miles).

At line 100 you may wish to supply data for your location.

10 'HELMERT.BAS, calculates acceleration due to gravity
20 'at any latitude and elevation.  Written, 6/2/91.
25 PI = 3.1415927
30 SCREEN 2:CLS:LOCATE 1,1,1:'visible cursor
40 PRINT "HELMERT'S EQUATION, program by Donald Simanek.
50 PRINT:PRINT "Helmert's equation for the acceleration due to gravity is:"
60 PRINT "                                       2               -6
70 PRINT "g = 980.616 - 2.5928cos(2í) + 0.0069cos (2í) - 3.086x10  h
80 PRINT
90 PRINT "where í is the latitude and h is the elevation in centimeters.
100 PRINT:PRINT "Lock Haven U: latitude 41ø8', elevation 580 ft.
110 PRINT:PRINT "Enter degrees and minutes separately, as prompted.
120 PRINT:PRINT "If you enter degrees in decimal form, just hit 'enter' at minutes prompt.
130 PRINT:PRINT "Latitude, í, in degrees"; :INPUT PHI
160 PRINT "Minutes"; :INPUT PHIM
180 PHI = PHI + PHIM/60 :PRINT "í = ";PHI;"ø" :PHI = PHI*2*PI/360
181 PRINT:PRINT "Elevation, h, in meters"; :INPUT H :H=H*100
182 IF H=0 THEN 190 ELSE 215
190 LOCATE 19,1:PRINT "  Elevation, h, in feet"; :INPUT H
210 H=H*30.48 :'Elevation above sea level, convert feet to centimeters
215 PRINT "h = ";H/100;" meters"
220 G = 980.616 - 2.5928*COS(2*PHI) + .0069*(COS(2*PHI))^2 - 3.086E-06*H
230 PRINT:PRINT "g =";G;"cm/s^2 =";G/100;"m/s^2"
240 PRINT:PRINT "Another calculation? [Y/n]";
242 R\$=INKEY\$:IF R\$="" THEN 242
245 IF R\$="N" OR R\$="n" THEN SYSTEM ELSE GOTO 30
250 'Last line of program.

GAUSSIAN PLOT

This program generates a pseudo-random number with a particular mean.
It makes a histogram as it calculates. Observe the raggedness of the plot,
even after many trials have been logged. The distrubution approaches a
Gaussian, of course, but the raggedness persists. Note also that the
mean, re-calculated each time a new number is generated, doesn't get
very close to the pre-determined mean of the number generator until very
many values are logged, and it continually fluctuates.

10 'GAUSPLOT, Gaussian distribution plot for t2k
15 SCREEN 2:LOCATE 1,1,0
30 DEFINT I,N
32 defdbl S
35 DIM S(101)
36 SUM=0 :SS=0
40 'initialize rand no on seconds
41 SEC=VAL(RIGHT\$(TIME\$,2))
42 FOR I=1 TO SEC
43 DUMMY=RND(1)
44 NEXT I
45 D1=50 :D2=15
46 FOR I=1 TO 100 :S(I)=0 : NEXT
47 CLS
50 FOR I=1 TO 10000
60 GOSUB 9500
70 N=G1*63/100
79 LOCATE 15,1 :PRINT USING "N=    #####";I;
80 S(N)=S(N)+1 :NN=S(N)
81 LOCATE 16,1 :PRINT USING "Value= ####";G1;
82 SUM=SUM+G1 :MEAN = SUM/I
84 LOCATE 17,1 :PRINT USING "Mean=  ##.#####";MEAN;
85 DS=(G1-MEAN)^2 :SS=SS+DS
86 IF I=1 OR I>50 THEN ID=I ELSE ID=I-1
87 SA=SS/ID :SD=SQR(SA)
89 LOCATE 18,1 :PRINT USING "SD=    ##.#####";SD;
90 IF N>63 OR N<0 GOTO 100
95 PSET (NN,N)
96 A\$=INKEY\$ :IF A\$="" THEN 100
97 LOCATE 20,1 :PRINT "Quit";:INPUT R\$
98 IF R\$="y" OR R\$="Y" THEN 200
99 LOCATE 20,1 :PRINT "         ";
100 NEXT I
200 CLS :SYSTEM :'End or program
9500 'generate Gaussian rand. no.
9510 'D1, D2 are desired mean and s.d.
9511 'number returned in G1
9530 S5=0
9540 FOR I2=1 TO 12
9550  S5=S5+RND(1)
9560 NEXT I2
9570 G1=(S5-6)*D2+D1
9580 RETURN :'from Gaussian rand no gen
9999 END 'last line

ORBITING EARTH SATELLITE

This program calculates the speed and period of a satellite in circular
orbit around the earth at a chosen altitude. If you enter zero or just
press the 'enter' key at the 'altitude in kilometers' prompt, you'll
get an 'altitude in miles' prompt.

The program also calculates the value of g, the acceleration due to
gravity, at that altitude, helping to convince students that astronauts
are *not* weightless, but have a weight not much less than their weight

10 'ORBITER.BAS Satellite orbits, By Donald Simanek, Dec 5, 1995.
11 PRINT "Period of an earth satellite in circular orbit."
12 PRINT "by Donald E. Simanek, Lock Haven University."
13 PRINT
20 PI=3.1416
30 EM = 5.98E+24 :'Mass of Earth, in kg.
40 ER = 6.38E+06 : 'Radius of Earth, in m.
50 G = 6.6726E-11 :'Universal gravitational constant.
60 MM = 1.99E+30 : 'Mass of moon, in kg.
70 MR = 1.72E+06 : 'Radius of Moon, in m. (In case anyone wants it)
80 INPUT "Enter satellite altitude in kilometers";SA
81 IF SA > 0 GOTO 84
82 INPUT "Enter satellite altitude in miles";SA
83 SA = SA*1.609 :'Miles to kilometers.
84 PRINT "Earth radius is ";ER/1000;"km, or ";ER*6.214E-04;"miles."
85 PRINT "Satellite altitude is ";SA;"km, or ";SA*.6214;"miles."
90 SR = ER/1000 + SA : 'Satellite orbit radius in km.
95 PRINT "Satellite orbit radius is ";SR;"km, or ";SR*.6214;"miles."
100 V = SQR(G*EM/(1000*SR)) : PRINT "Satellite speed is ";V;"m/s"
105 AG = G*EM/((1000*SR)^2 : PRINT "Acceleration due to gravity is ";AG"m/s^2"
110 T = 2*PI*SR*1000/V : TM = T/60
120 PRINT "Satellite period is ";T; "sec, or ";TM;"minutes."
150 END

BALLISTIC PENDULUM

Here's a little program for doing ballistic pendulum calculations.
It follows my philosophy of providing for input of reasonable
mixed units. If you don't like the units asked for, just enter
zero or hit the enter key and you'll be prompted for the data
in different units. You are asked for projectile mass in grams,
but if you enter zero, you'll get the prompt for mass in pounds.

10 'BALLIST2.BAS, Program for ballistic pendulum calculations, cgs & English
45 CLS:SCREEN 2:LOCATE 1,1,1:'visible cursor
50 PRINT "BALLISTIC PENDULUM CALCULATION, by Donald Simanek, December 31, 1994.
51 PRINT
52 PRINT "                  1/2
53 PRINT "v = (1 + M/m)(2gh)
55 PRINT
60 PRINT "Metric cgs entries assumed.
70 PRINT "If you enter zero, you'll be prompted for English units.
80 PRINT
81 PRINT "Enter projectile mass in grams";
90 INPUT MP
95 IF MP>0 GOTO 100
96 PRINT "Enter projectile weight in pounds"; ELSE GOTO 100
97 INPUT MP
98 IF MP=0 GOTO 81 ELSE MP = MP*1000/2.2
100 PRINT "Projectile mass is"; MP; "gram"
101 PRINT "Enter catcher mass in grams";
102 INPUT MC
103 IF MC>0 GOTO 109
104 PRINT "Enter catcher weight in pounds";
105 INPUT MC
106 IF MC=0 GOTO 101 ELSE MC = MC*1000/2.2 : PRINT "Catcher mass is"; MC; "gm"
109 PRINT "Catcher mass is "; MC ;"gm"
110 PRINT "Enter suspension length in centimeters";
111 INPUT R
112 IF R>0 GOTO 116
113 PRINT "Enter suspension length in inches";
114 INPUT R
115 IF R=0 GOTO 110 ELSE R = R*2.54 : PRINT "Suspension length is";R;"cm"
116 INPUT "Did you measure the horizontal displacement of catcher [Y/n]"; A\$
117 IF A\$="n" or A\$="N" GOTO 130
120 PRINT "Enter horizontal displacement of catcher in centimeters";
121 INPUT X
122 IF X>0 GOTO 128
123 PRINT "Enter horizontal displacement of catcher in inches";
124 INPUT X
125 IF X=0 GOTO 120 ELSE X = X*2.54
128 Y = R - SQR(R^2 - X^2) : GOTO 139 :'Quadratic for sagitta
130 PRINT "Enter vertical displacement of catcher in centimeters";
131 INPUT Y
132 IF Y>0 GOTO 138
133 PRINT "Enter vertical displacement of catcher in inches";
134 INPUT Y
135 IF Y=0 GOTO 130 ELSE X = X*2.54
138 PRINT "Horizontal displacement of catcher is ";X;"cm"
139 PRINT "Vertical rise of catcher is "; Y ;"cm"
140 'Finally, do the velocity calculation
141 V = (1 + MC/MP)*SQR(2*980*Y)
142 PRINT "The projectile velocity is ";V;"cm/s"
145 PRINT "Enter projectile firing height in centimeters";
146 INPUT H
147 IF H>0 GOTO 150
148 PRINT "Enter projectile firing height in inches";
149 INPUT HI: IF HI=0 GOTO 145 ELSE H = HI*2.54
150 RC = V*SQR(2*H/980) : RI = RC/2.54
151 PRINT "Firing horizontally from a height of ";H;"cm,"
152 PRINT "the predicted range is ";RC;"cm = ";RI;"inches"
200 INPUT "Quit [Y,n]"; A\$
201 IF A\$="n" OR A\$ = "N" THEN CLS: GOTO 50
300 SYSTEM

COLLLISIONS, ONE DIMENSION

Here's a really simple one, for one dimensional collisions,
for example, collisions on an air track.

10 'Collide.bas. Calculation of elastic collisions.
20 '
30 CLS: SCREEN 2
35 PRINT "One-dimensional elastic collision of two bodies.
36 PRINT :INPUT "Mass of first body"; M1
50 INPUT "Velocity of first body"; V1
55 PRINT :INPUT "Mass of second body"; M2
70 INPUT "Velocity of second body"; V2
80 VP2=(M1*(2*V1-V2)+M2*V2)/(M1+M2)
81 VP1=(M1*V1+M2*(2*V2-V1))/(M1+M2)
90 PRINT:PRINT "Final velocity of first body = "; VP1
91 PRINT "Final velocity of second body = "; VP2
92 PRINT :PRINT "Another calculation? [Y/n]
95 Q\$=INKEY\$ :IF Q\$="" THEN 95
99 IF Q\$="n" OR Q\$="N" GOTO 100 ELSE 30
100 SYSTEM

COMPTON SCATTERING

10 'COMPTON.BAS :`Compton scattering calculations.
35 SCREEN 2:CLS:LOCATE 1,1,1:'visible cursor
36 MPA = 1E-10 :'Meter/angstrom.
37 C = 3E8 :'Speed of light, MKS.
38 CW = 2.46E-12 :'Compton wavelength of electron, h/mc.
40 EVPJ = 1.602E-19 :'Electron volts/Joule.
49 H = 6.6257E-34 :'Planck's constant, Joule-second.
50 PRINT "COMPTON SCATTERING CALCULATIONS, by Donald Simanek
51 PRINT
52 PRINT "1   1    h
53 PRINT "- - - = --- (1 - cos à)
54 PRINT "f'  f   m c
55 PRINT "         o
56 PRINT
60 PRINT "Enter scatter angle à in degrees and minutes separately, as prompted.
70 PRINT "If you enter degrees in decimal form, just hit 'enter' at minutes prompt.
80 PRINT:PRINT "Scattering angle, à: degrees [default: 90]";
90 INPUT A
95 IF A=0 THEN A=90 :'Sets default, and traps for division by zero.
100 PRINT "Minutes";
110 INPUT AM
120 A = A + AM/60
130 IF A=0 THEN A=60
135 PRINT: PRINT "For other options, wavelength, ev or Angstrom, hit 'enter'."
140 PRINT:PRINT "Frequency of incident photon [in Hz]:";
150 INPUT F : IF F<>0 goto 194
170 PRINT "Wavelength of incident photon [in meters]:";
180 INPUT WL : if WL=0 goto 183
182 F = C/WL: goto 194
183 PRINT "Wavelength of incident photon [in Angstrom];";
184 INPUT WLA : IF WLA=0 goto 187
186 F = WLA*MPA : GOTO 194
187 PRINT "Energy of incident photon [in electron volts]:";
188 INPUT EV : F = EV/(H*EVPJ)
194 RF = 1/F :'Reciprocal of incident frequency.
196 RFP = RF + CW*(1-cos(A)) : FP = 1/RFP :'Compton calculation.
197 WP = C/FP : EVP = H*FP*EVPJ
198 WPA = WP/MPA
200 PRINT:PRINT "The scattered photon has frequency: "; FP;" Hz"
201 PRINT       "                         wavelength:"; WP;" meter"
202 PRINT       "                         wavelength:"; WPA;" Angstrom"
203 PRINT       "                         energy:    "; EVP;" electron volt"
220 PRINT:INPUT "Another calculation [Y/n]";R\$
230 IF R\$="N" OR R\$="n" THEN 300 ELSE 45
300 SYSTEM

THIN LENS CALCULATION

During lab, it's nice to be able to check student data quickly.
This program asks for: object position, screen position, and lens position
with reference to the bench scale.

Some student always comes up with data in which the object is at a high
number and the screen is at a low number on the scale. This is the opposite
of what is usually recommended, which I call an 'inverted' situation.
If that's the case, respond to the prompt 'Invert bench scale' with 'yes'
and the calculation is done correctly.

10 'Thin lens calculations, By Dr. Donald Simanek, Sept 2001
12 'Vanilla Basic, will work on Tandy 100.
13 Screen 2 :'Remove from T100 version
15 FV = 1
20 CLS : INPUT "Invert bench scale [y/N]"; F\$
30 IF F\$ = "Y" OR F\$ = "y" THEN FV=-1
40 INPUT "Object position"; O
50 INPUT "Lens position"; L
60 INPUT "Image position"; I
70 P=FV*(L-O) : Q=FV*(I-L) : F=P*Q/(P+Q)
80 PRINT "P ="; P;", Q ="; Q
90 PRINT "F ="; F
100 INPUT "Another calculation [Y/n]"; F\$
110 IF F\$ = "N" OR F\$ = "n" THEN 120 ELSE 15
120 SYSTEM:'Last line

THICK LENS

This was written in response to a question on an internet discussion
group about the focal length of a perfect sphere, for paraxial rays.
It solves the case of a two-surface thick lens, with possibly different
refractive indices on either side.

1 'THICKLEN.BAS Thick Lens, by Donald E. Simanek, April 2, 1995.
2 PRINT
3 PRINT "    N2   N1   N2-N1
4 PRINT "    -- = -- + -----      (Cartesian sign convention)
5 PRINT "    Q    P      R
6 PRINT
7 PRINT "This program solves a two-surface thick lens, using equation above."
8 PRINT "Use cartesian coordinates for positions of vertices and objects."
9 PRINT
10 INPUT "Index of refraction of first medium"; N1
11 INPUT "Index of refraction of second medium"; N2
12 INPUT "Index of refraction of third medium"; N3
13 PRINT
20 INPUT "Position of first surface vertex"; V1
21 INPUT "Position of second surface vertex"; V2
22 PRINT
23 INPUT "Radius of first surface"; R1
24 INPUT "Radius of second surface"; R2
25 PRINT
26 PRINT "Enter zero at the next prompt for an object at infinity."
27 INPUT "Position of object"; OBJ
28 IF OBJ=0 THEN PRINT "Object distance is infinite." : CP1=0 : GOTO 40
30 P1 = OBJ-V1 : CP1 = N1/P1
40 CQ1 = CP1 + (N2-N1)/R1
41 Q1 = N2/CQ1
45 T = V2-V1
46 PRINT :PRINT "RESULTS:
47 PRINT
49 PRINT "Image from first surface is "; Q1 " from the first surface."
50 P2 = Q1-T
51 PRINT "Image from first surface is "; P2 " from the second surface."
52 CP2 = N2/P2
55 CQ2 = CP2 + (N3-N2)/R2
60 Q2 = N3/CQ2
70 PRINT "The final image is at "; V2+Q2
71 PRINT "It is "; Q2+T; " from the first surface."
72 PRINT "It is "; Q2; " from the second surface."
80 END

PRISM SPECTROMETER

We do the classic prism spectrometer, with American Optical spectrometers,
or Ealing spectrometers. This helps check what goes wrong with student
data.

10 'SPECTROM.BAS, by Donald Simanek.  Calculates n for prism.
40 PI = 3.1415927
45 SCREEN 2:CLS:LOCATE 1,1,1:'visible cursor
50 PRINT "INDEX OF REFRACTION OF A PRISM, by Donald Simanek
51 PRINT
52 PRINT "    sin [(A+D)/2]
53 PRINT "n = -------------
54 PRINT "      sin [A/2]
55 PRINT
60 PRINT "Enter degrees and minutes separately, as prompted.
70 PRINT "If you enter degrees in decimal form, just hit 'enter' at minutes prompt.
80 PRINT:PRINT "Prism angle, A: degrees [default: 60]";
90 INPUT A
95 IF A=0 THEN A=60 :'Sets default, and traps for division by zero.
100 PRINT "Minutes";
110 INPUT AM
120 A = A + AM/60
130 IF A=0 THEN A=60
140 PRINT:PRINT "Minimum deviation angle, D: degrees";
150 INPUT D
160 PRINT "Minutes";
170 INPUT DM
180 D = D + DM/60
190 N = SIN(PI*(A+D)/360)/SIN(PI*A/360)
200 PRINT:PRINT "A = ";A;", D = ";D
210 PRINT "The index of refraction, n = ";N
220 PRINT:INPUT "Another calculation [Y/n]";R\$
230 IF R\$="N" OR R\$="n" THEN 300 ELSE 45
300 SYSTEM

TRAJECTORIES

This is a project unfinished. There are far better trajectory programs out
there, which take air friction into account. This one doesn't. It does have
one interesting feature I'd like to see in other programs. It shows the
vectors vo*t and (1/2)at^2 superimposed on the trajectory. One can also
toggle on a target and wall, the idea being to hit the target without touching
the wall. Each time you invoke the target, the position of target and height
of the wall are different. It's kinda fun, and crying out for improvement.
Press t to toggle the target on/off. Instructions are in the code at the
bottom. It runs on Sanyo, Xerox, and IBM computers up to EGA.

10 'TRAJECT#.BAS, Trajectory simulation, Dr. Donald Simanek, 9/19/87
15 'Lock Haven Univ., Lock Haven, PA. 17745.  Phone 717-893-2079 or 2048
17 RIGHT=77:LEFT=75:UP=72:DOWN=80 :'Cursor keys, all except Sanyo
18 CLS :GOSUB 659 :'Determine computer type
19 CLS :SCREEN SCRN
20 LOCATE 12,5:PRINT "THANK YOU. Would you like instructions? [y/N]"
21 Q\$=INKEY\$: IF Q\$="" THEN 21
22 IF Q\$="y"OR Q\$="Y" THEN GOSUB 830
40 V0=80:VTY0=V:HF=10:TINT=1:ACC=10:VL0=V0
60 RANDOMIZE VAL(RIGHT\$(TIME\$,2))
70 ' **** Return point for rerun, start with same initial position and angle.
80 PXL=1:VL=V0:VTX=0:VTY=VTY0:'vl=vl0
90 ' **** Return point for parameter changes
100 IF ABS(ASET) < 3.1416/2 THEN 130
110 LOCATE 5,1:PRINT "This cannon can't be aimed backward."
120 GOTO 270
130 IF VL0 <160 THEN 160
140 LOCATE 4,1:PRINT "This cannon hasn't that much firepower."
150 VL0 = VL0-10:GOTO 240
160 CLS:LOCATE 1,1:PRINT"Cursor keys move and aim the gun, + & - change velocity, f to fire."
170 LINE (0,V-1)-(640,V-1)
180 IF IFLAG<0 GOTO 210:'IFLAG positive draws target and wall
190 LINE (XTARG,YTARG)-(XTARG+20,V),1,B
200 LINE (XWALL,YWALL)-(XWALL,V),1
210 VTY=VTY0:HEIGHT=V-VTY0:VL=VL0
220 GOSUB 610:LOCATE 4,1,0:'Parameter display
230 A=ASET:GOSUB 1180:'Vector subroutine needs VTX,VTY,VL,HF,A,PXL
240 Q\$=INKEY\$:IF Q\$="" THEN 240
250 IF Q\$="f" OR Q\$="F" THEN 390
260 IF Q\$="a" OR Q\$="A" THEN 270 ELSE 280
270 LOCATE 4,1:INPUT "Angle (in radians)";AR:ASET=-AR:GOTO 100
280 IF Q\$="+" OR Q\$="=" THEN VL0=VL0+10:GOTO 100
290 IF Q\$="-" THEN VL0=VL0-10:GOTO 100
300 IF Q\$="t" OR Q\$="T" THEN IFLAG=-IFLAG:GOSUB 1120:GOTO 100
305 IF Q\$="`" THEN LFLAG=-LFLAG :'Flag for interrupted display.
310 B=ASC(RIGHT\$(Q\$,1))
320 IF B=UP THEN VTY0=VTY0-20:GOTO 100
330 IF B=DOWN THEN VTY0=VTY0+20:GOTO 100
340 IF B=LEFT THEN ASET=ASET-.03:GOTO 100
350 IF B=RIGHT THEN ASET=ASET+.03:GOTO 100
360 IF B=27 THEN 550:'Escape key
370 GOTO 240
380 'Main loop, plot points and draw vector polygon
390 FOR TIME=1 TO 100 STEP TINT
400 VL=TINT*VL0
410 FOR I=1 TO 10
420 T = TIME-1+.1*I
430 XP=VL0*T*COS(ASET)
440 YP=V-HEIGHT +VCOMP*(VL0*T*SIN(ASET) +.5*ACC*(T^2))
450 IF XP>640 GOTO 550
460 IF YP>V GOTO 550
470 IF YP<1 GOTO 490
480 PSET (XP,YP)
490 NEXT I
491 IF LFLAG>0 GOTO 500
492 R\$=INKEY\$:IF R\$="" THEN 492
493 IF R\$= " " GOTO 500
500 A=ASET:GOSUB 1180:'V0T Vector, needs VTX,VTY,VL,HF,A,PXL
510 VTX=VHX:VTY=VHY
520 VL=.5*ACC*(TIME^2)
521 IF LFLAG>0 GOTO 530
522 R\$=INKEY\$:IF R\$="" THEN 522
523 IF R\$= " " GOTO 530
530 A=ADROP:GOSUB 1180:'.5AT^2 Vector, needs VTX,VTY,VL,HF,A,PXL
540 NEXT TIME
550 LOCATE 1,1:PRINT "                                             [Q]uit, or [R]un another?"
560 Q\$=INKEY\$:IF Q\$="" GOTO 560
570 IF Q\$="q" OR Q\$="Q" GOTO 580 ELSE 80
580 CLS:screen 2 :SYSTEM
590 '
600 ' **** Parameter display subroutine
630 LOCATE 3,1:PRINT "Height,";HEIGHT
640 RETURN
650 '
658 ' **** Subroutine to set parameters for computers, 9/19/87
659 screen 2
660 PRINT "Select computer and display adaptor from this list:
661 PRINT :PRINT "1  IBM PC, XT, AT and compatibles with CGA.
662 PRINT "2  Same with EGA color graphics card
663 PRINT "3  Xerox 6064 or ATT 6300 monochrome high resolution
664 PRINT "4  Tandy 2000, color high resolution
665 PRINT "5  Sanyo 555
668 PRINT:PRINT:
669 RIGHT=77:LEFT=75:UP=72:DOWN=80 :'Cursor keys, all except Sanyo 555
670 Q\$=INKEY\$ :IF Q\$="" THEN 670
671 ICHOOSE=ASC(Q\$)-48
672 ON ICHOOSE GOTO 681, 682, 683, 684, 685
681 V=200 :SCRN=2 :VCOMP=5/12 :RETURN
682 V=350 :SCRN=9 :VCOMP=5/12 :RETURN
683 V=400 :SCRN=3 :VCOMP=.9   :RETURN
684 V=400 :SCRN=4 :VCOMP=.9   :RETURN
685 V=200:SCRN=2:VCOMP=.5:RIGHT=29:LEFT=28:UP=30:DOWN=31:RETURN:'Sanyo 555
789 '
790 ' **** Instructions for the trajectory simulation
800 LOCATE 12,5:PRINT "THANK YOU. Would you like instructions? [y/N]"
810 Q\$=INKEY\$: IF Q\$="" THEN 810
820 IF Q\$="y"OR Q\$="Y" THEN 830 ELSE 1090
825 ' **** Instructions
830 CLS: LOCATE 1,1:PRINT "Trajectory simulation program by Dr. Donald E. Simanek."
840 PRINT:PRINT "You will control a cannon at the left of the screen.
850 PRINT"A vector (arrow) shows its position, initial velocity and elevation angle.
860 PRINT:PRINT"The cannon may be moved up or down with the cursor up and down keys.
870 PRINT:PRINT"The cursor left and right keys change its elevation angle,
880 PRINT"in increments of about 2 degrees.
890 PRINT:PRINT"If you press 'a' you may directly enter the value of the angle--in radians!
900 PRINT:PRINT"Keys + and - change the initial velocity, in coarse increments,
910 PRINT"but you are limited to values less than 150. '=' is the same as '+';
915 PRINT"you needn't shift.
920 PRINT:PRINT"For those who must make a game of everything, t toggles on a target;
930 PRINT"a rectangular 'tower' and a 'wall' protecting it.
940 PRINT:PRINT"Since the velocity and height adjustments are crude, you'll have
950 PRINT"to use direct angle entry for finer elevation angle control.
960 PRINT:PRINT"     PRESS ANY KEY TO SEE MORE INSTRUCTIONS
970 IF INKEY\$="" GOTO 970
980 CLS: LOCATE 1,1
990 PRINT:PRINT"Use the 'a' direct entry option to make large changes in the angle.
1000 PRINT:PRINT"After firing, press 'q' to quit, or any other key
1010 PRINT"to have another try at the same target.
1020 PRINT:PRINT"Press 't' to erase target, 't' again to display a new target.
1030 PRINT:PRINT"Pedagogical note:  The trajectory is plotted with dots at
1040 PRINT"EQUAL time intervals.  Vectors V0*T and .5*T^2 are drawn only for
1050 PRINT"every 10th interval.
1060 PRINT:PRINT"During the simulation, press 'escape' to exit.
1070 PRINT:PRINT"End of instructions, press any key to begin the simulation.
1080 IF INKEY\$="" GOTO 1080
1090 CLS:RETURN
1100 '
1110 ' **** Target and wall creation subroutine
1120 XTARG=RND*400 + 200 :YTARG=V-RND*V/3-V/10
1140 XWALL=XTARG-RND*100-H/10 :YWALL=V-RND*V/3 -V/8
1160 RETURN
1170 '
1180 'Vector drawing subroutine 1/24/87
1200 VLX=VL*COS(A):VLY=VL*SIN(A):VLYC=VCOMP*VLY:VLXC=VCOMP*VLX
1210 VHX=VTX+VLX :VHY=VTY+VLYC
1220 IF VHX>640 THEN RETURN
1230 IF VTY>V OR VTY<0 THEN RETURN
1240 IF VHY>V OR VHY<0 THEN RETURN
1250 LINE (VTX,VTY)-(VHX,VHY),PXL
1260 H1X=VHX-HF*VLX/VL+HF*VLY/(VL*2)
1270 H1Y=VHY-HF*VLYC/VL-HF*VLXC/(VL*2)
1280 H2X=VHX-HF*VLX/VL-HF*VLY/(VL*2)
1290 H2Y=VHY-HF*VLYC/VL+HF*VLXC/(VL*2)
1300 LINE (VHX,VHY)-(H1X,H1Y),PXL
1310 LINE (VHX,VHY)-(H2X,H2Y),PXL
1320 LINE (H1X,H1Y)-(H2X,H2Y),PXL
1330 RETURN:'Last line of program TRAJECT.BAS

WHEATSTONE BRIDGE

This is written in "generic" basic so that it may be used with,
or adapted to, nearly any computer or basic compiler. It can
handle either balanced or unbalanced bridge problems.

Units are not a problem. If resistor values are in ohms and
potential in volts, the currents are in amperes.

If resistor values are assumed to be kilohms, the currents are
in milliamperes. This a more convenient, practical system of
units for real circuit problems.

The circuit is shown on the screen, with the cursor on R1,
waiting for you to enter a value for R1.  After each value is
entered, the cursor moves to the next resistor.  You may skip any
entry by pressing the `enter' key.  You need not enter the
galvanometer resistance unless you are particularly interested
in how it affects the currents.  The program defaults to zero
galvanometer resistance if you do not enter a value.  The last
entry is the working battery voltage.  If you press return
without entering a value, the program assumes a value of 2 volts,
which is typical for such circuits.

If you enter values for the resistances in all four bridge arms,
the program assumes an unbalanced bridge, and calculates the
currents.  The currents of the unbalanced bridge depend on the
galvanometer resistance.  If you did not enter a value for the
galvanometer, the program assumes it has zero resistance.

If you omit entry of the resistance in one arm, the program
assumes you want to calculate a value for it which will balance
the bridge. The program does so, calculating the missing value
and displaying that value at the lower left of the screen.  The
currents of the balanced bridge are independent of the
galvanometer resistance.

This program uses the ASCII screen graphic characters, which can
be looked up in any table of ASCII codes. In Europe, resistors
are shown in circuit diagrams as simple rectangles, sometimes
with the resistor's value within the rectangle.  The "zigzag"
form you may be used to is _not_ a world-wide standard. Another
nice European style is to write resistor (and other component)
values in this manner:  6K8, which translates to 6.8 k, that is,
6.8 kilohm.  This method neatly avoids using the decimal point,
which can often get "lost" in reproduction.

When you clip out the program below for use, you may have to
remove the hard returns in the long lines.

The picture of the circuit on the screen (lines 403-432) contain
ASCII graphics characters with codes above 127. These may cause
problems when you download this. On request I can e-mail you a
unix-encoded version which may translate better. If you want to
see how these characters look on your browser, click on the link
at the bottom of this document.

As a last resort you may have to import the text into a good
wordprocessor, set the line lengh long enough so there's no
word wrap, and reconstruct the picture to look good. My program
used block characters to make solid 'bar' resistors.
The wires were single lines, as was the cell. In older versions
of BASIC be sure you have GRAPHTABL loaded.

10 'WHEATS, Wheatstone bridge calculation, version 5.1
11 'By Dr. Donald Simanek, Lock Haven University 9/14/89, 2/1/90, 1/23/92
12 'With direct calculation of balanced bridge, input of V.
14 SCREEN 2 :KEY OFF :CLS
15 DEFINT I-N :DEFDBL A-H,O-Z
30 A\$="###.####"
60 DIM R(5),C(3),S(3),B(3,3)
65 'Begin the program
70 GOSUB 400 :'Draw the screen
75 LOCATE 22,40:PRINT "Wheatstone Bridge Circuit";
80 LOCATE 23,40:PRINT "By Donald Simanek, ver 5.1";
82 GOSUB 500:'Input R subroutine
85 'Check, is the bridge unbalanced?
86 'If no resistance values are zero, use unbalanced bridge routine.
87 NZ=0:'Initialize count of zero values
91 FOR I=1 TO 4
92   IF R(I)=0! THEN NZ=NZ+1:IX=I
93 NEXT I
94 IF NZ=0 GOTO 180:'No zero values, do unbalanced bridge.
95 IF NZ>1 THEN GOTO 96 ELSE 105:'Must balance the bridge if one zero value.
96 LOCATE 24,1:PRINT "You must enter at least 3 resistance values.  Press any key to restart.";
97 K\$=INKEY\$:IF K\$="" THEN 97
100 CLS:GOTO 65
105 GOSUB 800 :'Direct calculation to balance the bridge.
110 LOCATE 24,1 :PRINT "R";IX;"=";:PRINT USING A\$;R(IX);:PRINT " balances the bridge."
170 '
180 'Begin calculation of currents in balanced or unbalanced bridge.
183 B(1,1)=R(1)+R(3)+R(5)
184 B(2,2)=R(2)+R(4)+R(5)
185 B(3,3)=R(3)+R(4)
186 B(1,2)=-R(5) :B(2,1)=-R(5)
187 B(2,3)=-R(4) :B(3,2)=-R(4)
188 B(1,3)=-R(3) :B(3,1)=-R(3)
190 GOSUB 5280 :'Cramer's rule subroutine
191 D1=D
192 FOR I=1 TO 3
193  FOR J=1 TO 3
194   S(J)=B(I,J) :B(I,J)=0
195  NEXT J
196   B(I,3)= V
197   GOSUB 5280 :'Cramer's rule
198   C(I)=D/D1
199  FOR K=1 TO 3 :B(I,K)=S(K) :NEXT K
200 NEXT I
305 C1=C(1) :C2=C(2) :C3=C(3)-C(1) :C4=C(3)-C(2) :C5=C(1)-C(2)
306 'LINE 5,10,17, COL 20,40
310 LOCATE 5,20 :PRINT USING A\$; C1
311 LOCATE 5,44 :PRINT USING A\$; C2
312 LOCATE 10,44:PRINT USING A\$; C5
313 LOCATE 17,20:PRINT USING A\$; C3
314 LOCATE 17,44:PRINT USING A\$; C4
390 'End of calculation of currents
392 LOCATE 23,40 :INPUT "Another calculation [Y/n]";R\$
393 CLS :IF R\$="N" OR R\$="n" THEN SYSTEM ELSE 65
399 '
400 'Subroutine to draw the screen graphics
401 '
403 PRINT"                R1=                     R2=
404 PRINT"
412 PRINT"         ÚÄÄÄÄÄÄÛÛÛÛÛÛÛÛÛÛÛÄÄÄÄÄÄÂÄÄÄÄÄÄÛÛÛÛÛÛÛÛÛÛÛÄÄÄÄÄÄ¿
413 PRINT"         ³                       ³                       ³
414 PRINT"         ³      I1=              ³      I2=              ³
415 PRINT"         ³                       ³                       ³
416 PRINT"         ³                   ÚÄÄÄÅÄÄÄ¿                   ³
417 PRINT"         ³      GALVANOMETER ³  ÛÛÛ  ³  R5=              ³
418 PRINT"    ÚÄÄÄÄ´        (Null      ³  ÛÛÛ  ³                   ÃÄÄÄÄÄ¿
419 PRINT"    ³    ³        detector)  ³  ÛÛÛ  ³  I5=              ³     ³
420 PRINT"    ³    ³                   ÀÄÄÄÅÄÄÄÙ                   ³     ³
421 PRINT"    ³    ³                       ³                       ³     ³
422 PRINT"    ³    ³      R3=              ³      R4=              ³     ³
423 PRINT"    ³    ³                       ³                       ³     ³
424 PRINT"    ³    ÀÄÄÄÄÄÄÛÛÛÛÛÛÛÛÛÛÛÄÄÄÄÄÄÁÄÄÄÄÄÄÛÛÛÛÛÛÛÛÛÛÛÄÄÄÄÄÄÙ     ³
425 PRINT"    ³                                                          ³
426 PRINT"    ³           I3=                     I4=                    ³
427 PRINT"    ³                                                          ³
429 PRINT"    ³                      +  ³   -                            ³
430 PRINT"    ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´ ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
431 PRINT"                              ³
432 PRINT"                V =
440 RETURN :'from screen drawing subroutine
500 '
510 'Data input subroutine, LINE 1,13,18, COL 20,44
520 '
540 LOCATE 1,20 :INPUT R(1):IF R(1)<>0 THEN LOCATE 1,20 :PRINT USING A\$; R(1);
550 LOCATE 1,44 :INPUT R(2):IF R(2)<>0 THEN LOCATE 1,44 :PRINT USING A\$; R(2);
555 LOCATE 8,44 :INPUT R(5):LOCATE 8,44 :PRINT USING A\$; R(5);
560 LOCATE 13,20:INPUT R(3):IF R(3)<>0 THEN LOCATE 13,20:PRINT USING A\$; R(3);
570 LOCATE 13,44:INPUT R(4):IF R(4)<>0 THEN LOCATE 13,44:PRINT USING A\$; R(4);
580 LOCATE 22,20:INPUT V   :IF V=0 THEN V=2:LOCATE 22,20:PRINT USING A\$; V;
610 RETURN :'From data input subroutine
800 '
801 'Subroutine to balanced the bridge.
802 'Needs IX, the number of the missing resistor.
803 '
820 ON IX GOTO 830,840,850,860
830 R(1)=R(3)*R(2)/R(4):GOTO 870
840 R(2)=R(1)*R(4)/R(3):GOTO 870
850 R(3)=R(1)*R(4)/R(2):GOTO 870
860 R(4)=R(2)*R(3)/R(1):GOTO 870
870 RETURN :'From direct calculation subroutine
5270 '
5280 'Find determinant of a 3x3 matrix by Cramers' rule, for unbalanced bridge
5290 '
5300 D=B(1,1)*(B(2,2)*B(3,3)-B(3,2)*B(2,3))
5310 D=D-B(1,2)*(B(2,1)*B(3,3)-B(3,1)*B(2,3))
5320 D=D+B(1,3)*(B(2,1)*B(3,2)-B(3,1)*B(2,2))
5330 RETURN :'From determinant subroutine
9999 'Last line

Enjoy these programs. If you like and use any of them, remember us here
at Lock Haven University, Lock Haven, PA. Post some of your own programs
to PHYSHARE.

-- Donald E. Simanek

To see how characters translate on your browser, look at these: