10 ' 20 ' ELECTRIC POTENTIAL FOR A CHARGED WIRE 30 ' 40 ' ----------------------- 4/14/93 -------------------------- 50 ' 60 ' -- This program calculates the electric potential for a charged 70 ' wire which is assumed to be a continuous finite line of charge. 80 ' The program finds the exact solution and approximate solutions 90 ' by Finite Sum, Trapezoidal Rule, and Simpson's Rule. 100 ' 110 ' -- Thus four potential values are calculated: 120 ' 130 ' V1 = Exact potential for the finite line of continuous charge 140 ' V2 = Approximation: Finite Sum from N point charges 150 ' V3 = Approximation: Trapezoidal Rule (another summing) 160 ' V4 = Approximation: Simpson's Rule: (another summing) 170 ' 180 ' --> LIST OF PROCEDURES <-- 190 ' 200 GOSUB 310 'INITIALIZATIONS AND INPUT 210 GOSUB 500 'CALCULATE V1 220 GOSUB 580 'CALCULATE V2, V3, V4 230 GOSUB 1100 'OUTPUT 240 PRINT 250 PRINT 260 INPUT "WANT TO RUN ANOTHER (Y/N)? ", AGAIN$ 270 IF AGAIN$ = "Y" OR AGAIN$ = "y" THEN 200 280 END 290 ' 300 '------------------------------------------------------ 310 'INITIALIZATIONS AND INPUT 320 ' 330 'PRINT "INPUT VALUES FOR THE FOLLOWING:" 340 'PRINT " NUMBER OF TERMS: N" 350 'PRINT " HALF-LENGTH OF LINE: A" 360 'PRINT " X CO-ORD OF POINT: X0" 370 'PRINT " Y CO-ORD OF POINT: Y0" N = 101 A = 1! X0 = 0! Y0 = 1! PRINT " WAVE NUMBER: K" 380 PRINT 390 PRINT "SEPARATE THE VALUES WITH COMMAS AND PRESS RETURN" 400 PRINT 410 'INPUT N, A, X0, Y0, K INPUT K 420 ' 430 ' -- We redefine N, making it the nearest larger odd integer. 440 ' -- This is really only necessary for Simpson's Rule, 450 ' 460 N = 2 * INT(N / 2) + 1 470 ' 480 RETURN 490 '------------------------------------------------------ 500 'CALCULATE V1 510 ' 520 V1N = SQR((X0 - A) ^ 2 + Y0 ^ 2) - (X0 - A) 530 V1D = SQR((X0 + A) ^ 2 + Y0 ^ 2) - (X0 + A) 540 V1 = LOG(V1N / V1D) 550 ' 560 'RETURN 570 '------------------------------------------------------ 580 'CALCULATE V2, V3, V4 590 ' 600 ' -- V2, V3, and V4 are computed as sums over the number of 610 ' charges making up the line. Set them initially to zero. 620 ' 630 V2 = 0 640 V3 = 0 650 V4 = 0 660 ' 670 ' -- DX is the spacing between adjacent point charges 680 ' used in the approximations 690 ' 700 DX = 2 * A / (N - 1) 710 ' 720 ' -- Start at one end of the line 730 ' 740 X = -A 750 ' 760 ' -- Loop to add the potential from each of N charges 770 ' 780 FOR J = 1 TO N 790 ' 800 ' -- L (for lambda) is the charge density (here assumed 1) 810 ' 820 L = 1 830 ' 840 ' -- F is the function we wish to integrate 850 ' 860 F = L * COS(K * 3.14159 * X) / SQR((X - X0) ^ 2 + Y0 ^ 2) 870 ' 880 ' -- Add contribution for Finite Sum 890 ' 900 C = 1 910 V2 = V2 + C * F * DX 920 ' 930 ' -- Add contribution for Trapezoidal Rule 940 ' 950 IF J = 1 OR J = N THEN C = .5 960 IF J <> 1 AND J <> N THEN C = 1 970 V3 = V3 + C * F * DX 980 ' 990 ' -- Add contribution for Simpson's Rule 1000 ' 1010 IF J = 1 OR J = N THEN C = 1 / 3 1020 IF J = 2 * INT(J / 2) THEN C = 4 / 3 1030 IF J <> 1 AND J <> N AND J <> 2 * INT(J / 2) THEN C = 2 / 3 1040 V4 = V4 + C * F * DX 1050 X = X + DX 1060 NEXT J 1070 ' 1080 RETURN 1090 '------------------------------------------------------ 1100 'OUTPUT 1110 ' 1120 PRINT 1130 INPUT "PRINTER (P) OR SCREEN (S)? ", ANS$ 1140 IF ANS$ = "P" OR ANS$ = "p" THEN OPEN "LPT1:" FOR OUTPUT AS #1 1145 IF ANS$ = "S" OR ANS$ = "s" THEN OPEN "SCRN:" FOR OUTPUT AS #1 1150 IF ANS$ <> "P" AND ANS$ <> "p" AND ANS$ <> "S" AND ANS$ <> "s" THEN GOTO 1120 1160 ' 1170 PRINT #1, 1180 PRINT #1, "N="; N, " A="; A, " X0="; X0, " Y0="; Y0 1190 PRINT #1, 1200 PRINT #1, USING "V1 = #.##### "; V1 1210 PRINT #1, USING "V2 = #.##### "; V2 1220 PRINT #1, USING "V3 = #.##### "; V3 1230 PRINT #1, USING "V4 = #.##### "; V4 PRINT #1, USING "V2-V4 = #.##### "; V2 - V4 PRINT #1, USING "V3-V4 = #.##### "; V3 - V4 1240 ' 1250 CLOSE #1 1260 ' 1270 RETURN 1280 '---------------- END OF PROGRAM LINES ----------------