Search code examples
pythonnumpyrandomgalaxyspiral

Simulating a logarithmic spiral galaxy in python


I am simulating a logarithmic spiral galaxy using python. Using the parametric equations,

x= a*exp(b*theta)*cos(theta) and y= a*exp(b*theta)*sin(theta)

I used numpy.random for getting the random distribution of stars. The sample code is given below.

import random
from math import *
from pylab import *
import numpy as np

n=100000
a= 1
b=0.6
th =np.random.randn(n)
x= a*exp(b*th)*cos(th)
y=a*exp(b*th)*sin(th)
x1 = a*exp(b*(th))*cos(th+ pi)
y1=a*exp(b*(th))*sin(th + pi)
plot(x,y,"*")
plot(x1, y1,"*")
show()

The resulting image is shown below spiral galaxy with two arms

What I need: 1) stars should be radially distributed in the spiral galaxy. I got the distribution only along the arms. 2) Both arms should be blue. Here I have one arm with blue color and other with green.

After simulating this, I need to rotate the galaxy. Any help regarding this would be appreciable.

**edit: I got both the arms in blue color using plot(x1, y1,"b*")


Solution

  • If an approximation is good enough, try adding some noise the points before plotting them. For starters I would start with a normal (Gaussian) distribution. For example, this tweaked version:

    import random
    from math import *
    from pylab import *
    import numpy as np
    
    n=1000
    a=0.5
    b=0.6
    th=np.random.randn(n)
    x=a*exp(b*th)*cos(th)
    y=a*exp(b*th)*sin(th)
    x1=a*exp(b*(th))*cos(th+pi)
    y1=a*exp(b*(th))*sin(th+pi)
    
    sx=np.random.normal(0, a*0.25, n)
    sy=np.random.normal(0, a*0.25, n)
    plot(x+sy,y+sx,"*")
    plot(x1+sx, y1+sy,"*")
    
    show()
    

    Gives this output:enter image description here You might need to play around with the variables a bit to adjust the output to your needs. Also, as mentioned in the comments, this isn't true radial noise.