NB. Newton's Method on Systems
NB. Cliff Reiter -- revisiting a basins of attraction 
NB. previously explored:
NB. Mark A. Motyka and Clifford A. Reiter, 
NB. Chaos and Newton's Method on Systems, 
NB. Computers & Graphics, 14 1 (1990),131-134.
NB. April 2, 2002
NB. requires raster5.ijs (or version 4)

NB. system 1 and its Jacobian
f1=: <:@((*/ . ^)&2 3) , -/
Df1=: ((2: * (*/ . ^)&1 3) , (3: * (*/ . ^)&2 2)),:1 _1"_

NB. 2x2 direct system solver 
ss2=:3 : 0
(2 2$1 _1 _1 1*3 1 2 0{,y.)%-/ . * y.
:
(ss2 y.)+/ . * x.
)

NB. Step of Newton's method using ss2 system solving
newt=: 2 : '- u. ss2 v.'

NB. script required to save *.bmp
require 'fvj2\raster5'

NB. Newton's method on system
NB. f Newt Df x0,y0
Newt =: 2 : 0
([:n_post[:u. n_step v.^:n_test^:_ n_pre)
)

n_step=:2 : '(u. newt v.,])@:(0 1&{),>:@{:'
n_test=:(0 1&{ -.@-: 2 3&{)*.{: < 253"_
n_post=:0 1 4&{
n_pre=:], _:, _: , 0:

NB. gives an array of complex initial points
NB. ver_num_pix zl_clur2 cenleft upright
zl_clur2=: 4 : 0
w=.-~/9 o.y.
h=.-~/11 o.y.
(j.h-(+:h)*(i.%<:)>.x.*2*h%w) +/ ({.y.)+w*(i.%<:)x.
)

NB. Gives a palette of black and 3 hues of 6 hues
p18q=:0,,/0 2|:<.1 0.72 0.45 */ hue 2 4 0 1 5 3{(i.%])6

NB. makes a picture of the basins of attraction
NB. [numpix cenleft upright c] mk_newt_sys 'filename.bmp'
mk_newt_sys=:2 : 0
100 _2 2j2 _ u. mk_newt_sys v. y.
:
]ev=.^2r5p1 *j. i.5
z=.({.x.) zl_clur2 }.}:x.
if. _~:{:x. do. z=.z,"0 {:x. else. z=.z,"0 z end.
Z=:i.0 1 3
for_k. i.#z do.
  Z=.Z,(u. Newt v.) :: (0 0 253"_)"1 k{z
  if. 0=10|k do. (": k) (1!:2) 2 end.
  end.
'b bb k'=.0 1|: Z
b=.{.@/:@:|@(ev&-)"0 b
(p18q;(253~:k)*1+(3|k)+3*b) writebmp8 y.
)

NB. The following makes the first figure in the J directory
200 _6 2j4 0j1 f1 mk_newt_sys Df1 'fig01.bmp'