#                              Ren-Cang Li, June 1, 1996
#                              na.rcli@na-net.ornl.gov

NewtonItern:=proc(x0L:vector, cx20L, x0U:vector,cx20U, xM:vector)
  local niter, xL, cx2L, yL, xU, cx2U, yU, eps, 
        x0, y0, x1, y1, cx20, cx21, yM, cx2M;
  eps:=10^(-Digits+20): niter:=0:
  xL:=x0L: cx2L:=cx20L: xU:=x0U: cx2U:=cx20U:
  #
  cx2fixed(xL,cx2L): yL:=dcx1cy1overdcx2(xL,cx2L): 
  cx2fixed(xU,cx2U): yU:=dcx1cy1overdcx2(xU,cx2U):
  if yL*yU>=0 then
     print(`Warning: there may be two zeros or NONE.`);
     print(`Please INTERRUPT to stop!`);
  fi:
  x0:=xL: y0:=yL: cx20:=cx2L: x1:=xU: y1:=yU: cx21:=cx2U:
  cx2M:=cx21-y1*((cx21-cx20)/(y1-y0)):
  if cx2M<cx2L or cx2M>cx2U then
     cx2M:=(cx2L+cx2U)/2:
     xM:=evalm((xL+xU)/2):
  else
     xM:=evalm((y0/(y0-y1))*x1+(y1/(y1-y0))*x0):
  fi:
  cx2fixed(xM, cx2M):
  yM:=dcx1cy1overdcx2(xM,cx2M):
  # print(yM):
  while abs(cx2M-cx21)>eps*abs(cx2M) and niter<101 and abs(yM)>eps do
        niter := niter + 1:
        if yL*yM>=0 then
           xL:=xM: yL:=yM: cx2L:=cx2M:
        else
           xU:=xM: yU:=yM: cx2U:=cx2M:
        fi:
        x0:=x1: y0:=y1: cx20:=cx21:
        x1:=xM: y1:=yM: cx21:=cx2M:
        cx2M:=cx21-y1*((cx21-cx20)/(y1-y0)):
        if cx2M<cx2L or cx2M>cx2U then
           cx2M:=(cx2L+cx2U)/2:
           xM:=evalm((xL+xU)/2):
        else
           xM:=evalm((y0/(y0-y1))*x1+(y1/(y1-y0))*x0):
        fi:
        cx2fixed(xM, cx2M):
        yM:=dcx1cy1overdcx2(xM,cx2M):
  od:
  if niter>100 then
     print(`In NewtonItern: Over 100 iterations`):
  fi:
  # print(yM):
  cx2M;
end:
