Simulation Source Code


    1 REM >Magnetic Pendulum
    2 REM Simulation of Chaotic Magnetic Pendulum
    3 REM By Mathew Tizard
	
   10 MODE19:OFF
   20 ORIGIN 640,512
   30 COLOUR3,4
   40 COLOUR2,2
   50 *mount everest
   60 *dir progs.chaos.pictures
   70 *screenload magnetic
   80 MOVE 0,1000

   85 REM Set simulation constants
   90 mass=50
  100 SC=250
  110 I=0.97
  120 M=1
  130 C=.1
  140 d=0.25

  150 DIM Xm(3),Ym(3),D(3)
  160 FOR N=1 TO 3:READ A$,B$:Xm(N)=EVAL(A$):Ym(N)=EVAL(B$):NEXT
  170 DATA 1,0,-0.5,SQR(3)/2,-0.5,-SQR(3)/2
  180 FOR N=1 TO 3:GCOLN:CIRCLE FILL Xm(N)*SC,Ym(N)*SC,14:NEXT

  190 FOR WY=1000-2 TO 0 STEP -2
  200  FOR EX=0 TO 1280-2 STEP 2
  210   ORIGIN 640,500
  220   GCOL 4,0
  230   GCOL FNwin(EX,WY)
  240   ORIGIN 0,0
  250   RECTANGLE FILL EX,WY,16,16
  260  NEXT
  270 NEXT

  280 DEF FNwin(x,y)
  290 X=(x-640)/SC:Y=(y-500)/SC
  300 Xv=0:Yv=0
  310 Z=0:CL=1
  320
  330 REPEAT
  340 REM Force components initially zero for this time step
  350 Xf=0:Yf=0
  360
  370 REM Gravitational Force (proportional to distance from centre)
  380 r=SQR(X*X+Y*Y)
  390 Xf-=C*X/r
  400 Yf-=C*Y/r
  410
  420 REM Magnetic Forces (Coulomb's 2nd Law- Inverse Square)
  430 FOR J=1 TO 3
  440  dx=Xm(J)-X:dy=Ym(J)-Y
  450  r3=(dx*dx+dy*dy+d*d)^1.5
  460  Xf+=M*dx/r3
  470  Yf+=M*dy/r3
  480 NEXT
  490
  500 REM Friction (Proportional to velocity of pendulum)
  510 Xf-=Xv*I:Yf-=Yv*I
  520
  530 REM Apply a=F/m
  540 Xv+=Xf/mass:Yv+=Yf/mass
  550 X+=Xv:Y+=Yv
  560
  570 CIRCLE FILL X*SC,Y*SC,20
  580 WAIT
  590 CIRCLE FILL X*SC,Y*SC,20
  600 DRAW X*SC,Y*SC
  610 a=FNclosest
  620 IF a=CL Z+=1:PRINTTAB(0,0);a;":";Z;"    " ELSE Z=0
  630 IF Z>250 =a
  640 CL=a
  650 MOUSE e,w,q
  660 UNTIL q<>0
  
  670 CLS:END

  690 DEF FNclosest
  700 FOR Q=1 TO 3
  710  D(Q)=SQR((Xm(Q)-X)^2+(Ym(Q)-Y)^2)
  720 NEXT
  730 IF D(1)>D(2) W=2 ELSE W=1
  740 IF D(W)>D(3) =3 ELSE =W