mathplotdosastronomygw-basic# How to print output of .bas file to text

I am trying to print coordinate outputs of a program to a text file in order to use it in another program but I don't really know anything about GWBASIC and its my first time using MS-DOS. I need it to open a text file named plot.txt and print output there and save it without actually plotting on GWBASIC. Here is the program which I found in an old magazine.

```
810 REM MAKE A GLOBULAR
12 REM
14 R0=20: R2=R0*R0: R3=R2*R0
16 P1=3.14159265#
18 C0=P1*P1*R3/4
20 R1=R0/SQR(2)
22 XM=512: YM=512
24 X2=XM/2: Y2=YM/2: S=5
26 INPUT "HOW MANY STARS ";T
27 RANDOMIZE TIMER
28 CLS: REM CLEAR SCREEN
30 FOR I=1 TO T
32 C=C0*RND: R=R1
34 REM
36 REM NOW FIND R
38 FOR K=1 TO 5
40 GOSUB 100
42 R=R+(C-C1)/D
44 NEXT K
46 REM 3-DIMENSIONAL PLACE
48 X=RND-.5
50 Y=RND-.5
52 Z=RND-.5
54 S1=SQR(X*X+Y*Y+Z*Z)
56 IF S1>.5 THEN GOTO 48
58 REM POINT IS NOW IN SPHERE
60 R=R*S1: X=X*R: Y=Y*R: Z=Z*R
62 GOSUB 200
64 NEXT I
66 END
68 REM
100 REM NEWTON-RAPHSON ITERATION
105 A=R/R0
110 C1=ATN(A)*.5*R3
115 A=1+A*A
120 C1=C1+R*.5*R2/A
125 C1=P1*(C1-R*R2/(A*A))
130 D=4*P1*R*R/(A*A*A)
135 RETURN
140 REM
200 REM 2-DIMENSIONAL PLOT
203 SCREEN 9
205 X=X*S+X2: Y=Y*S+Y2
210 IF X<0 OR Y<0 THEN 225
215 IF X>=XM OR Y>=YM THEN 225
220 PSET(X,Y)
225 RETURN
230 REM ------------------------
240 REM APPEARED IN ASTRONOMICAL
250 REM COMPUTING, SKY & TELE-
260 REM SCOPE, APRIL, 1986
270 REM ------------------------
```

Solution

Here is a Python 3 paraphrase:

```
#globular.py
#Python paraphrase of model.bas from
#http://www.skyandtelescope.com/wp-content/uploads/model.bas
from math import pi, sqrt, atan
from random import uniform, random
#Global variables:
r0 = 20.0
r2 = r0**2
r3 = r0**3
c0 = pi**2*r3/4
r1 = r0/sqrt(2)
def NRI(c,r):
#Newton-Raphson Iteration
a = r/r0
c1 = atan(a)*0.5*r3
a = 1+a**2
c1 += r*0.5*r2/a
c1 = pi*(c1-r*r2/a**2)
d = 4*pi*r**2/a**3
return (c1,d)
def makeStars(t):
stars = []
for i in range(t):
c = c0*random()
r = r1
for k in range(5):
c1,d = NRI(c,r)
r += (c-c1)/d
while True:
x = uniform(-0.5,0.5)
y = uniform(-0.5,0.5)
z = uniform(-0.5,0.5)
s1 = sqrt(x**2 + y**2 + z**2)
if s1 <= 0.5: break
r *= s1
x *= r
y *= r
z *= r
stars.append((x,y,z))
return stars
def starsToFile(t,fname):
stars = makeStars(t)
f = open(fname,'w')
for star in stars:
print(*star, sep = ', ',file = f)
f.close()
```

I skipped the part about printing `x`

and `y`

and instead wrote a function `makeStars`

to return a list of `(x,y,z)`

tuples, as well as a related function which takes such an output and sends it to a text file. This last function is the only thing that used Python 3 instead of Python 2. If you are using Python 2 you can import Python 3's print function from the future.

Typing `starsToFile(100,'stars.txt')`

in the Python shell gave me a text file which begins:

```
-0.32838465248713156, -0.3294895266926551, -1.2963580524762535
14.20224408569865, 1.4434961933043464, 6.450969593697097
1.6525937589658193, -0.24447292610082685, 1.0543647986350608
1.5707528567123823, 5.190972598268825, -2.0054790217091134
```

I don't have good 3-d scatter-plot graphing at my finger tips, but here is a screen shot of 50 points generated by the function and plotted using a computer algebra system called Derive:

Final remark: I wonder if there is a typo in the source code. The line

```
C0=P1*P1*R3/4
```

strikes me as suspicious since it is fairly rare in mathematics for `pi`

to appear squared -- though it *does* happen. Maybe there should be only 1 factor of `pi`

there (which would then have the effect of setting `C0`

proportional to the volume of the sphere of radius `R0`

). On the other hand, I don't know exactly what is happening here, so I left it in. If the results seem problematic, you could maybe experiment with that line.

- Evaluating integral using Riemann sums
- How to use `scipy.integrate.quad` to compute integral of a function which depends on the integral of another function
- Polynomial which satisfies integral and two points
- "Array indices must be positive integers or logical values"
- Integration of sin x dx problem in python programming implementation
- Integration in Python Midpoint Calculation
- How to find arc that connects two segments?
- How to calculate percentage in Kotlin
- Unsupported operand types
- User input based circle drawing, then printing to paper with fixed sizes
- Float number division with 3 decimal places
- Inverting a real-valued index grid
- How to calculate with complex numbers in JavaScript?
- Solve matrix and vector multiplication with parameters instead of values (preferably in python)
- Determine new GPS position using roll, pitch, yaw and length
- Algorithm to locate local maxima
- unsigned long long int pow
- Simple statistics - Java packages for calculating mean, standard deviation, etc
- Arbitrary-precision arithmetic Explanation
- How do I calculate r-squared using Python and Numpy?
- Print all ways to sum n integers so that they total a given sum.
- Prove that (p → q) → ((r ∨ p) → (r ∨ q)) is a tautology without using truth table
- How to implement this Desmos roll function in Unity
- How to compute the centroid of a mesh with triangular faces?
- Different results for XIRR between Excel and ExcelFinancialFunctions 3.2.0
- Efficiently getting all divisors of a given number
- In JavaScript, why does zero divided by zero return NaN, but any other divided by zero return Infinity?
- Python fit line to high dimensional points and sample between them
- How can I compute a dual norm?
- How to pick between 2 numbers