%! % x is an initial guess, /f the name of a function % with one argument x that returns the pair [ f(x) f'(x) ] % x /f /newton-solve { load % now the function itself is on the stack top 1 dict begin /f exch def /x exch def 10 { % a somewhat arbitary number of iterations /x x x f 0 get x f 1 get div sub def % x = x - f(x)/f'(x) } repeat x } def % x -> [ x^2-2 2x ] /f2 { 1 dict begin /x exch def [ x dup mul 2 sub 2 x mul ] end } def % x -> [ x^3-2 3x^2 ] /f3 { 1 dict begin /x exch def [ x dup dup mul mul 2 sub x dup mul 3 mul ] end } def % =============================================================== 1 /f2 newton-solve == 1 /f3 newton-solve == quit