'Program ringplot.bas displays spatio-temporal chaos multispecies Lotka-Volterra system 'with only 4 nearest neighbors on a ring DEFEXT a-z DECLARE SUB DERIVS (x(), dxdt(), n&) DECLARE SUB RK4 (x(), dxdt(), n&, h, xnew()) screen 12 call grayscale CLS n&=480 'Number of variables tmax&=1e4 'Time scale of plot (full scale) h=0.05 'Step size plot&=2 'Default plot type DIM x(n&), xnew(n&), dxdt(n&), plot(n&,4) 'randomize timer FOR i&=1 TO n& 'Initial conditions 'x(i&)=RND x(i&)=.4+.2*sin(6.2832*i&/n&) NEXT i& FOR j&=1 TO 5*640-1 'Loop 5x across the screen to get on attractor q\$=inkey\$ if q\$=chr\$(27) then exit for if q\$=" " then plot&=(plot&+1) mod 5: q\$="" for t&=1 to tmax&/(640*h) RK4 x(), dxdt(), n&, h, x() next t& for i&=1 to n& plot(i&,0)=x(i&) i1&=1+(i& mod n&) plot(i&,1)=.5*(x(i1&)-x(i&)) plot(i&,2)=abs(x(i1&)-x(i&)) i2&=1+(i1& mod n&) plot(i&,3)=.5*(x(i&)/2-x(i1&)+x(i2&)/2) plot(i&,4)=abs(x(i&)/2-x(i1&)+x(i2&)/2) next i& xp&=(xp&+1) mod 640 for yp&=0 to 479 i1&=1+int(n&*yp&/480) i2&=1+(i1& mod n&) di=n&*yp&/480-i1&+1 c=(1-di)*plot(i1&,plot&)+di*plot(i2&,plot&) pset(xp&,yp&),15-int(16*c) next yp& NEXT j& beep while q\$<>chr\$(27): q\$=inkey\$: wend end SUB DERIVS (x(), dxdt(), n&) 'Returns the time derivatives dxdt(i&) of x(i&) FOR i&=1 TO n& i1&=1+(i&-3+n&) mod n& i3&=1+i& mod n& dxdt(i&)=x(i&)*(1##-x(i1&)-x(i&)-x(i3&)) NEXT i& END SUB SUB RK4 (X(), DXDT(), N&, H, XNEW()) 'Fourth-order Runge-Kutta integrator DIM XT(N&), DXT(N&), DXM(N&) HH = H * .5## H6 = H / 6## CALL DERIVS(X(), DXDT(), N&) FOR I& = 1 TO N& XT(I&) = X(I&) + HH * DXDT(I&) NEXT I& CALL DERIVS(XT(), DXT(), N&) FOR I& = 1 TO N& XT(I&) = X(I&) + HH * DXT(I&) NEXT I& CALL DERIVS(XT(), DXM(), N&) FOR I& = 1 TO N& XT(I&) = X(I&) + H * DXM(I&) DXM(I&) = DXT(I&) + DXM(I&) NEXT I& CALL DERIVS(XT(), DXT(), N&) FOR I& = 1 TO N& XNEW(I&) = X(I&) + H6 * (DXDT(I&) + DXT(I&) + 2## * DXM(I&)) NEXT I& ERASE DXM, DXT, XT END SUB SUB grayscale () 'Sets the RGB palette to a gray scale in 16-color VGA screen modes FOR pal% = 0 TO 15 PALETTE pal%, pal% c% = INT(4.2 * pal%) OUT &h3C8, pal% OUT &h3C9, c% OUT &h3C9, c% OUT &h3C9, c% NEXT pal% END SUB