'Program NNMAP.BAS trains a neural network to a 1-D map '(c) 2004 by J. C. Sprott 'Compile with PowerBASIC for DOS defext a-z randomize timer np&=2 'Number of parameters dim a(np&), abest(np&) for i&=1 to np&: abest(i&)=1: next i& eps=1: ebest=1e6 while inkey$<>chr$(27) for i&=1 to np& a(i&)=abest(i&)+eps*gauss next i& c=-a(2)*tanh(a(1)) f1=a(2)*tanh(a(1)/2##) + c/2## - 1## f2=a(1)*a(2)*sech(a(1)/2##)^2 + c e=f1*f1+f2*f2 if e<=ebest then ebest=e cls print"Error =";e print"Neighborhood =";eps print"Slope @ zero =";a(1)*a(2)+c print print"a =";a(1) print"b =";a(2) print"c =";c for i&=1 to np& abest(i&)=a(i&) next i& eps=min(2*eps,1) else eps=0.9999*eps end if wend 'plot results screen 12 color 8,63 cls line(0,0)-(639,479),,b y1=rnd: y2=y1 while inkey$<>chr$(27) x=y1 y1=4##*x*(1##-x) pset(639*x,479-479*y1) x=y2 y2=a(2)*tanh(a(1)*x)+c*x pset(639*x,479-479*y2),3 le=le+log(abs(a(1)*a(2)*sech(a(1)*x)^2+c)) incr n&& if (n&& mod 1000)=0 then locate 2,2: print"LE =";csng(le/n&&) wend end FUNCTION tanh (x) 'Returns hyperbolic tangent of x tanh = 1## - 2## / (EXP(2## * x) + 1##) END FUNCTION FUNCTION sech (x) 'Returns hyperbolic cosecant of x sech = 2## / (EXP(x) + EXP(-x)) END FUNCTION FUNCTION gauss 'Returns a normally (Gaussian) distributed random deviate 'with mean of zero and standard deviation of 1.0 DO V1 = 2## * RND - 1## V2 = 2## * RND - 1## R = V1 * V1 + V2 * V2 LOOP WHILE R >= 1## OR R = 0## GAUSS = V2 * SQR(-2## * LOG(R) / R) END FUNCTION