N-body problem: Difference between revisions

Line 1,268:
 
=={{header|K}}==
<p>The simulation is meant to be viewed in-browser, using a Javascript implementation of (a nice subset of) K, plus graphics primitives, here: [http://johnearnest.github.io/ok/ike/ike.html[http://johnearnest.github.io/ok/ike/ike.html]]; just paste the code into the code window and push the go button. Playing with the parameters in this interactive and immediate environment gives a feel for the system beyond what you are likely to get from tables or graphs.</p>
<b><em>... under construction ...</em></b>
<p>To do: make it 3D</p>
<h4>configuration generator</h4>
<p>The configurations shown here are based on those in the J task.</p>
<lang K>sq:{pow[x;2]};sqrt:{pow[x;0.5]};pi:3.14159265 / math stuff
cs:{(cos 2*pi*x%y;sin 2*pi*x%y)} / positions
sc:{(sin 2*pi*x%y;-cos 2*pi*x%y)} / velocities</lang>
<h4>static equilibrium configuration</h4>
<p>The system is stable for an initial velocity for which centrifugal and gravitational forces are in balance</p>
<lang K>n::3;m::n#1;p::cs\:[!n;n];v::sc\:[!n;n] / count, masses, positions, velocities
g::1;vs::pow[1%3;1%4];v*::vs / gravitational constant, initial velocity
t::0;dt::0.01;rot:2 / time, time increment, rotational period</lang>
<h4>dynamic equilibrium</h4>
<p>Within a narrow range of initial velocities the system oscillates between more and less kinetic and potential energy.</p>
Line 1,288 ⟶ 1,287:
<lang K>/ as for static case, plus:
/ v+::(0 0;0 0;0 0.0001);rot:4 / wait for it ... wait for it ...</lang>
<h4>n>3</h4>
<p>To do: generate and run static-equilibrium cases for n=4, 5, 6, ...</p>
<h4>physics</h4>
<lang K>DV:{p[x]-p[y]};DS:{sqrt[+/sq'DV[x;y]]} / nth body position vector and magnitude
A1:{$[x=y;0;g*m[y]*DV[x;y]%pow[DS[x;y];3]]} / nth body centripetal acceleration component
A:{+/{A1/:[x;!n]}'!n} / total centripetal acceleration
tmax::2*pi*rot%vs / duration of simulation
N:{[dt] / increment
N:{[dt]
v0:v;v+::dt*A[];p+::dt*0.5*v+v0 / update velocities and positions
t+::dt;$[t>tmax;do[msg::,"[";tick::{}];0] / update time, detect end of time
}</lang>
<h4>animationevents and graphics</h4>
<lang K>s::n#3 / size of sprite bitmap
<p>Paste the code into <em>iKe</em>, an in-browser Javascript implementation of the K language!</p>
sc:1.25;P::{w*0.5*1+(1%sc)*x} / display units
<lang K>s::n#3
wh::(w;h);C::wh%2 / viewbox
sc:1.25;P::{w*0.5*1+(1%sc)*x}
RGB:("#f00";"#0f0";"#00f") / color palette
wh::(w;h);C::wh%2
BW:("#000";"#fff") / monochrome palette
RGB:("#f00";"#0f0";"#00f")
msg::,":" / initial position marker
BW:("#000";"#fff")
tick::{N[dt]} / time increment callback
msg::,":"
draw::{ / display callback
tick::{N[dt]}
r:(,:'P[p]-s%2),'`RGB,',:'(2#'s)#'!n / plot the bodies
draw::{
r,:,(P[1 0];BW;~,/'+text@`i$msg) / plot the initial position marker
r:(,:'P[p]-s%2),'`RGB,',:'(2#'s)#'!n
r,:,(P[1 0];BW;~,/'+text@`i$msg)
}</lang>
<h4>n>33D</h4>
<ul>
<p>Although the animation is limited to two dimensions,
<li>[Vector.UK Vol.26 No.4[http://archive.vector.org.uk/#ref6]]</li>
we can verify that the implementation really does support three,
<li>['A graphical sandbox for K'[http://archive.vector.org.uk/art10501610]</li>
by changing the axis of rotation in the initialization function:</p>
<li>IDE - run this code in the browser![http://johnearnest.github.io/ok/ike/ike.html]]</li>
<lang J>cs:{(0;cos 2*pi*x%y;sin 2*pi*x%y)} / x-axis
</ul>
sc:{(0;sin 2*pi*x%y;-cos 2*pi*x%y)} / objects on screen move up and down
 
cs:{(cos 2*pi*x%y;0;sin 2*pi*x%y)} / y-axis
sc:{(sin 2*pi*x%y;0;-cos 2*pi*x%y)} / objects on screen move left and right
 
cs:{(cos 2*pi*x%y;sin 2*pi*x%y;0)} / z-axis
sc:{(sin 2*pi*x%y;-cos 2*pi*x%y;0)} / objects on screen go round and round</lang>
 
=={{header|Octave}}==