Anonymous user
N-body problem: Difference between revisions
→{{header|K}}
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>
<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>▼
<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
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>
<lang K>s::n#3 / size of sprite bitmap
sc:1.25;P::{w*0.5*1+(1%sc)*x} / display units▼
wh::(w;h);C::wh%2 / viewbox
▲sc:1.25;P::{w*0.5*1+(1%sc)*x}
RGB:("#f00";"#0f0";"#00f") / color palette
BW:("#000";"#fff") / monochrome palette
msg::,":" / initial position marker
tick::{N[dt]} / time increment callback
draw::{ / display callback
r:(,:'P[p]-s%2),'`RGB,',:'(2#'s)#'!n / plot the bodies▼
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>
<p>Although the animation is limited to two dimensions,
we can verify that the implementation really does support three,
by changing the axis of rotation in the initialization function:</p>
<lang J>cs:{(0;cos 2*pi*x%y;sin 2*pi*x%y)} / x-axis
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}}==
|