You are on page 1of 3

Spiral Galaxy Formation Simulation Using Embedded MATLAB Functions file:///E:/32bits/Matlab2007a/toolbox/simulink/simdemos/simgeneral/html...

Open sldemo_eml_galaxy_script.m in the Editor Run in the Command Window

Spiral Galaxy Formation Simulation Using Embedded MATLAB


Functions
This model was inspired by the classic paper "Galactic Bridges and Tails" (Toomre & Toomre 1972).
The original paper explained how disc shaped galaxies could develop spiral arms. Two disc shape
galaxies originally are far apart. They then fly by each other and almost collide. Once the galaxies are
close enough, mutual gravitational forces cause spiral arms to form.

Except for the "PlotAll" visualization block's use of plotting routines, all Embedded MATLAB function
blocks in this model support code generation with Real-Time Workshop and Real-Time Workshop
Embedded Coder.

Demo Requirements: During this demonstration, Simulink generates code for the simulation in a
Simulink project directory created in the current working directory (pwd). If you do not want to
affect the current directory (or if you cannot generate files in this directory), you should change
your working directory.

Contents

Opening the Model and Running the Simulation


Model Description
Closing the Model
Comments About the Model
References

Opening the Model and Running the Simulation

Open the model and run the simulation by executing the code below. If you are using MATLAB Help,
select the code you want to execute and press F9 (or select code, right click on selection and select
"Evaluate Selection"). The Simulink model will load, compile, and run.

model = 'sldemo_eml_galaxy';
open_system(model);
sim(model);

1 of 3 08.07.2008 07:21
Spiral Galaxy Formation Simulation Using Embedded MATLAB Functions file:///E:/32bits/Matlab2007a/toolbox/simulink/simdemos/simgeneral/html...

Model Description

This section describes the model in detail and explains the role of each block in the model. The green
blocks initialize the simulation, the orange blocks are the core of the simulation, and the yellow block
makes the galaxy animation view.

Initial Conditions

The model requires initial conditions for each galaxy. The initial conditions are: galaxy radius in parsecs
(rp), galaxy mass in solar mass units (cm), galaxy position in parsecs (pos), and galaxy velocity in m/s
(vel).

In the model, constant blocks specify the initial conditions. The initial conditions have been chosen such
that the galaxies will nearly collide at some point in time.

"ConstructGalaxy" Blocks

The initial conditions are passed to the Embedded MATLAB function blocks Construct Galaxy 1 and
Construct Galaxy 2. These Embedded MATLAB function blocks contain M-code that builds the galaxy
models.

In a typical galaxy, most of the mass is concentrated in its center as a super-massive black hole and/or
star agglomeration. We model the galaxy as a disc with radius r with most of its mass concentrated in
the inner circle of radius r/3. In addition to this super-massive nucleus, the "ConstructGalaxy" Embedded
MATLAB function block creates 349 random stars with masses ranging from 4 to 24 solar masses.
These stars are randomly positioned within distance r/3 and r from the center of the galaxy. The stars
initially move in circular orbits around the galaxy core. Every object (star or galaxy core) has mass,
position (x, y, z), and velocity (Vx, Vy, Vz).

"Matrix Concatenation" Block

This block joins information about both galaxies. At this point the model has 750 objects: 1 core for each
galaxy and 349 stars around each core. These 750 objects interact according to Newtonian mechanics.

"Partition" Block

This Embedded MATLAB function block separates all 750 objects into two groups: heavy bodies and

2 of 3 08.07.2008 07:21
Spiral Galaxy Formation Simulation Using Embedded MATLAB Functions file:///E:/32bits/Matlab2007a/toolbox/simulink/simdemos/simgeneral/html...

light bodies. The heavy bodies are the galaxy cores. The light bodies are the stars. Because the galaxy
cores are much heavier than individual stars, the model will consider only the heavy-heavy and
heavy-light interactions. We can ignore the light-light body interactions. This will save a lot of time since
748 out of 750 bodies in the model are light.

"ApplyGravity" Block

This Embedded MATLAB function block uses Newtonian mechanics to compute the velocities and
positions of the bodies at each step. The "combine" block is also an embedded MATLAB function block.
It merges the data about heavy and light objects together.

"PlotAll" Block

This Embedded MATLAB function block plots the bodies in a figure and updates the position of each
star at every step in the simulation.

Closing the Model

Close the model without saving any changes. Clear data generated by simulation/demo.

close_system(model,0);
clear model sldemo_eml_galaxy_output;

Note:

In this model signal logging is turned on (see the 'GalaxyBodies' signal and the little blue arrow
next to it). The model saves output data in a Simulink.ModelDataLogs structure. Any other
data is saved/modified in the model workspace to avoid cluttering MATLAB workspace.

To modify signal logging settings right-click on the signal line and select "Signal Properties". In
this demo the signal name is 'GalaxyBodies' and "Log signal data" checkbox is checked.

The logged signal is saved to MATLAB workspace as 'sldemo_eml_galaxy_output'. This is a


Simulink.ModelDataLogs data structure. 'GalaxyBodies' signal can be retrieved from this
structure by typing sldemo_eml_galaxy_output.GalaxyBodies. See more about working
with data structures in MATLAB Help "Accessing Data in Structure Arrays".

Comments About the Model

Despite its simplicity, this model provides insight into how our own galaxy evolved. The demo allows the
user to maximally simplify the model and run a fast simulation. The user can easily modify the demo by
adding more galaxies.

References

Toomre, Alar; Toomre, Juri; "Galactic Bridges and Tails"; Astrophysical Journal, Vol. 178, pp. 623-666
(1972); 12/1972;
Copyright 2006 The MathWorks, Inc.
Published with MATLAB® 7.4

3 of 3 08.07.2008 07:21

You might also like