N-body problem: Difference between revisions
Content added Content deleted
Line 1,268: | Line 1,268: | ||
=={{header|K}}== |
=={{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> |
<h4>configuration generator</h4> |
||
<p>The configurations shown here are based on those in the J task.</p> |
<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 |
<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)} |
cs:{(cos 2*pi*x%y;sin 2*pi*x%y)} / positions |
||
sc:{(sin 2*pi*x%y;-cos 2*pi*x%y)}</lang> |
sc:{(sin 2*pi*x%y;-cos 2*pi*x%y)} / velocities</lang> |
||
<h4>static equilibrium configuration</h4> |
<h4>static equilibrium configuration</h4> |
||
<p>The system is stable for an initial velocity for which centrifugal and gravitational forces are in balance</p> |
<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] |
<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 |
g::1;vs::pow[1%3;1%4];v*::vs / gravitational constant, initial velocity |
||
t::0;dt::0.01;rot:2</lang> |
t::0;dt::0.01;rot:2 / time, time increment, rotational period</lang> |
||
<h4>dynamic equilibrium</h4> |
<h4>dynamic equilibrium</h4> |
||
<p>Within a narrow range of initial velocities the system oscillates between more and less kinetic and potential energy.</p> |
<p>Within a narrow range of initial velocities the system oscillates between more and less kinetic and potential energy.</p> |
||
Line 1,288: | Line 1,287: | ||
<lang K>/ as for static case, plus: |
<lang K>/ as for static case, plus: |
||
/ v+::(0 0;0 0;0 0.0001);rot:4 / wait for it ... wait for it ...</lang> |
/ v+::(0 0;0 0;0 0.0001);rot:4 / wait for it ... wait for it ...</lang> |
||
⚫ | |||
<p>To do: generate and run static-equilibrium cases for n=4, 5, 6, ...</p> |
|||
<h4>physics</h4> |
<h4>physics</h4> |
||
<lang K>DV:{p[x]-p[y]};DS:{sqrt[+/sq'DV[x;y]]} |
<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]]} |
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} |
A:{+/{A1/:[x;!n]}'!n} / total centripetal acceleration |
||
tmax::2*pi*rot%vs |
tmax::2*pi*rot%vs / duration of simulation |
||
N:{[dt] / increment |
|||
N:{[dt] |
|||
v0:v;v+::dt*A[];p+::dt*0.5*v+v0 |
v0:v;v+::dt*A[];p+::dt*0.5*v+v0 / update velocities and positions |
||
t+::dt;$[t>tmax;do[msg::,"[";tick::{}];0] |
t+::dt;$[t>tmax;do[msg::,"[";tick::{}];0] / update time, detect end of time |
||
}</lang> |
}</lang> |
||
<h4> |
<h4>events 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> |
|||
⚫ | |||
<lang K>s::n#3 |
|||
wh::(w;h);C::wh%2 / viewbox |
|||
⚫ | |||
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]} |
|||
⚫ | |||
draw::{ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
}</lang> |
}</lang> |
||
⚫ | |||
<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}}== |
=={{header|Octave}}== |