At the following link I found a pendulum simulation that is an several
places on the web. It simulates a single 9 meter long pendulum by
repeatedly sending the turtle/pendulum fd by the amount 25*9, and adjusting
the heading angle before each such forward amount by a function of the
previous heading angle (sine of the angle, actually) and its new speed,
which itself is a function  of the previous speed.

http://code.activestate.com/recipes/577553-pendulum-simulation-with-turtle/

I have translated the code at that link to J, which you can run by typing
in simply `main''` after entering the code in a J session.

GA =: 9.80665 NB. Gravitational Acceleration (meters per second squared)
NB. FORM = 'Time={:6.3f}, Angle={:6.3f}, Speed={:6.3f}'


sin =: 1&o.
pi =: 1p1
main =: 3 : 0
    length =: 9.0            NB. Of pendulum (meters)
    ngol =: - GA % length    NB. Negative G over L
    total_time =: 0.0        NB. Seconds
    angle =: 1.0             NB. Initial angle of pendulum (radians)
    speed =: 0.0             NB. Initial angular velocity (radians/second)
    time_step =: 0.05        NB. Seconds
    while. total_time < 30.0 do.
        total_time =: total_time + time_step
        speed =: speed + ngol * (sin angle) * time_step
        angle =: angle + speed * time_step
    smoutput 'setheading :',":angle + pi
    smoutput 'forward,dot :',": 25 10 * length
   end.
)

I have also translated it so that it will work with tgsjhs.ijs which is on
the jwiki, but I am having problems with this version because the turtles
are making big jumps, instead of small ones, and there is no oscillation in
their patterns. I am particularly suspicious of my scaling of the forward
steps and that I am not familiar with the dot() function used in the python
version. Does anyone know what the dot() function does?


The version below has to be loaded after a fresh version of tgsjhs.ijs from
jwiki, and can be run then with: main 4.



http://www.jsoftware.com/jwiki/BrianSchott/code/turtleGraphics/tgsjhs

NB.
http://code.activestate.com/recipes/577553-pendulum-simulation-with-turtle/

NB. to execute load this after loading tgsjhs.ijs; enter something like:
main 4

coclass'tgsj'
coinsert'jhs'

GA =: 9.80665 NB. Gravitational Acceleration (meters per second squared)

main =: 3 : 0
NTurtles =: y
PENDING=: ''
NUMBER=: 0
init ''
cs''
initTurtles y
pu ''

setlength =. <.@-: + 3 +i.
length =. setlength y
xdist =. * i:@-:@<:
ngol =: - GA % length    NB. Negative G over L
total_time =: 0.0        NB. Seconds
angle =: 1.0             NB. Initial angle of pendulum (radians)
sinAngle =. sin angle
goto 25 * (xdist,.-@setlength,.sinAngle * -@setlength) y
pd''
speed =: 0.0             NB. Initial angular velocity (radians/second)
time_step =: 0.05        NB. Seconds
NB. while. total_time < 10.0 do.
while. total_time < 0.15 do. NB. this is for testing only
    total_time =: total_time + time_step
    speed =: speed + ngol * (sin angle) * time_step
    angle =: angle + speed * time_step
    pt dfr angle + pi -rfd }:1{"1 Heading
    fd 25 * length
    smoutput ({:"1 Heading),.Position NB. this is for testing only

end.
)

Thanks,
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to