• Remix
  • Share
  • New Entry

  • Karl

  • /
  • Galaxy rotation

on 24 Nov 2023
  • 18
  • 27
  • 0
  • 1
  • 1940
drawframe(1);
Write your drawframe function below
function drawframe(f)
% Draw an image frame in an animation of a rotating galaxy.
%
% This is a remix of Jenny Bosten's "Galaxy" (MATLAB Mini Hack 2022):
% https://uk.mathworks.com/matlabcentral/communitycontests/contests/5/entries/11703
% Rotation, and chaotic behaviour, added!
%
% The circular velocity, v(r), of a star moving in a cicular orbit, at
% a distance r from the centre of a galaxy, can be modelled as:
% v(r) = sqrt(G * M(r) / r)
% where G is the gravitational constant, and M(r) is the mass
% inside the orbit.
%
% In this animation, all stars have the same angular velocity,
% omega = v * r. This would imply a mass distribution M(r) = k * r^3,
% where k is a constant. This is inconsistent with observations,
% which indicate that angular velocity decreases with radius.
% The simplification of uniform angular velocity is used here to be able
% to show continuous rotation in an animation with two-second repeat!
% Define time and space.
nFrame = 48;
a = 508;
X = linspace(-1,1,a);
[y,x] = meshgrid(X);
% Define galaxy base image and its colormap.
% The base image is invariant under rotations about its centre.
r = (x.^2+y.^2).^0.5;
base = 255*(1-r);
baseColor = rescale(gray+0.1*(1-parula),0,1);
colormap(baseColor);
% Define galaxy's spiral structure.
% This is done by modulating transparency over the base image:
% magic formulae used for this from Jenny Boston.
% The random-number seed is ntermittently changed,
% to introduce some chaotic behaviour.
r2 = 0-rescale(r,-2,0.8);
z = rescale(sin(40*log(r2+2)+2*angle(x+y*1i)),0,1);
r2(r2<0) = 0;
seedInterval = 3;
rng(1+floor((f-1)/seedInterval))
modulation = rescale((1-r2.*z),0,0.6)...
.*rescale(abs(ifft2((r).^-1.25.*cos(7*rand(a)))),0,30);
% Rotate the modulating array.
theta = -360 * f / nFrame;
modulation = imrotate(modulation,theta,'nearest','crop');
% Display the rotated galaxy, zooming in on the spiral region.
idx1 = 104; idx2 = 404;
axes(Position=[0 0 1 1])
image(base(idx1:idx2,idx1:idx2),AlphaData=modulation(idx1:idx2,idx1:idx2));
end
Animation
Remix Tree