Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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*")

like image 380
Athul R T Avatar asked Jan 29 '26 15:01

Athul R T


2 Answers

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.

like image 184
Pikalek Avatar answered Jan 31 '26 05:01

Pikalek


To rotate the image, I would calculate the new positions of the stars using a rotation matrix, which you have to do for each star like

R = [ [ np.cos(phi), -np.sin(phi) ], [ np.sin(phi), np.cos(phi) ] ]
[x_new, y_new] = np.dot( [x_old, y_old], R )

What exactly do you mean with "radially distributed"? Could you draw an example image?

like image 33
RobertAlpha Avatar answered Jan 31 '26 03:01

RobertAlpha