Root-seeking functions that use multiple root refinements per iteration

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (https://www.hpmuseum.org/forum/forum-10.html)
+--- Forum: HP Prime Software Library (https://www.hpmuseum.org/forum/forum-15.html)
+--- Thread: Root-seeking functions that use multiple root refinements per iteration (/thread-24458.html)



Root-seeking functions that use multiple root refinements per iteration - Namir - 2025-12-15 21:48

I asked Claude AI to generate a set of root-seeking functions that perform multiple refinements for the roots per iteration. You should edit the code for function FX to match the function you want to get the roots for. The functions JARRATT and SHARMA may prove somewhat inefficient or generate error for my example function. Test them with your functions. In any case, you have a good choice.

Each function returns:
1. The refined guess for the root.
2. The function value at the  refined guess for the root.
3. The number of iterations performed to reach the above results.

Code:
// ============================================================================
// NONLINEAR FUNCTION
// ============================================================================

EXPORT FX(x)
BEGIN
  RETURN exp(x)-3*x^2;
END;


// ============================================================================
// AUXILIARY FUNCTION: Numerical Derivative
// ============================================================================

EXPORT FP(x)
// Calculate numerical derivative of FX at x
BEGIN
  LOCAL h := 1E-8;
  RETURN (FX(x + h) - FX(x - h)) / (2*h);
END;


// ============================================================================
// FOURTH-ORDER METHODS WITH 2 REFINEMENTS
// ============================================================================

EXPORT OSTROWSKI(x, toler, MaxIters)
// Ostrowski's fourth-order method with 2 refinements per iteration
//
// Algorithm (3 function evaluations per iteration):
//   Refinement 1: y = x - f(x)/f'(x)              [Newton step]
//   Refinement 2: x_new = y - [f(x) + f(y)]/[f(x) - 2*f(y)] * f(y)/f'(x)
//
// Order of convergence: 4
// Efficiency index: 4^(1/3) ≈ 1.587
//
// Reference:
//   Ostrowski, A.M. (1966). Solution of Equations and Systems of Equations
//
// Parameters:
//   x: Initial guess
//   toler: Tolerance for convergence
//   MaxIters: Maximum number of iterations
//
// Returns: {root, function_value, iterations}
BEGIN
  LOCAL fx_val, fpx, y, fy, denom;
  LOCAL k;
 
  FOR k FROM 1 TO MaxIters DO
    fx_val := FX(x);
   
    IF ABS(fx_val) < toler THEN
      RETURN {x, fx_val, k-1};
    END;
   
    fpx := FP(x);
    IF ABS(fpx) < 1E-15 THEN
      RETURN {x, fx_val, k};
    END;
   
    // Refinement 1: Newton step
    y := x - fx_val / fpx;
    fy := FX(y);
   
    // Refinement 2: Ostrowski correction
    denom := fx_val - 2*fy;
    IF ABS(denom) > 1E-15 THEN
      x := y - (fx_val + fy) / denom * (fy / fpx);
    ELSE
      x := y;
    END;
  END;
 
  RETURN {x, FX(x), MaxIters};
END;


EXPORT KUNGTRAUB(x, toler, MaxIters)
// Kung-Traub optimal fourth-order method with 2 refinements
//
// Algorithm (3 function evaluations per iteration):
//   Refinement 1: y = x - f(x)/f'(x)              [Newton step]
//   Refinement 2: x_new = y - f(y)/f'(x) * [1 + f(y)/f(x)]
//
// Order of convergence: 4
// Efficiency index: 4^(1/3) ≈ 1.587
//
// Reference:
//   Kung, H.T. and Traub, J.F. (1974). Journal of the ACM, 21(4), 643-651
BEGIN
  LOCAL fx_val, fpx, y, fy;
  LOCAL k;
 
  FOR k FROM 1 TO MaxIters DO
    fx_val := FX(x);
   
    IF ABS(fx_val) < toler THEN
      RETURN {x, fx_val, k-1};
    END;
   
    fpx := FP(x);
    IF ABS(fpx) < 1E-15 THEN
      RETURN {x, fx_val, k};
    END;
   
    // Refinement 1: Newton step
    y := x - fx_val / fpx;
    fy := FX(y);
   
    // Refinement 2: Kung-Traub correction
    IF ABS(fx_val) > 1E-15 THEN
      x := y - (fy / fpx) * (1 + fy / fx_val);
    ELSE
      x := y;
    END;
  END;
 
  RETURN {x, FX(x), MaxIters};
END;


EXPORT JARRATT(x, toler, MaxIters)
// Jarratt's fourth-order method with 2 refinements
//
// Algorithm (3 function evaluations per iteration):
//   Refinement 1: y = x - (2/3) * f(x)/f'(x)
//   Refinement 2: x_new = x - [f(x)/f'(x)] * [3*f(y) + f(x)]/[3*f(y) - f(x)]
//
// Order of convergence: 4
// Efficiency index: 4^(1/3) ≈ 1.587
//
// Reference:
//   Jarratt, P. (1966). Mathematics of Computation, 20(95), 434-437
BEGIN
  LOCAL fx_val, fpx, y, fy, denom;
  LOCAL k;
 
  FOR k FROM 1 TO MaxIters DO
    fx_val := FX(x);
   
    IF ABS(fx_val) < toler THEN
      RETURN {x, fx_val, k-1};
    END;
   
    fpx := FP(x);
    IF ABS(fpx) < 1E-15 THEN
      RETURN {x, fx_val, k};
    END;
   
    // Refinement 1: Modified Newton step
    y := x - (2/3) * (fx_val / fpx);
    fy := FX(y);
   
    // Refinement 2: Jarratt correction
    denom := 3*fy - fx_val;
    IF ABS(denom) > 1E-15 THEN
      x := x - (fx_val / fpx) * (3*fy + fx_val) / denom;
    ELSE
      x := y;
    END;
  END;
 
  RETURN {x, FX(x), MaxIters};
END;


EXPORT CHUN(x, toler, MaxIters)
// Chun's fourth-order method with 2 refinements
//
// Algorithm (3 function evaluations per iteration):
//   Refinement 1: y = x - f(x)/f'(x)              [Newton step]
//   Refinement 2: x_new = y - [f(x)/(f(x) - 2*f(y))] * f(y)/f'(x)
//
// Order of convergence: 4
// Efficiency index: 4^(1/3) ≈ 1.587
//
// Reference:
//   Chun, C. (2005). Computers & Mathematics with Applications, 50, 1559-1568
BEGIN
  LOCAL fx_val, fpx, y, fy, denom;
  LOCAL k;
 
  FOR k FROM 1 TO MaxIters DO
    fx_val := FX(x);
   
    IF ABS(fx_val) < toler THEN
      RETURN {x, fx_val, k-1};
    END;
   
    fpx := FP(x);
    IF ABS(fpx) < 1E-15 THEN
      RETURN {x, fx_val, k};
    END;
   
    // Refinement 1: Newton step
    y := x - fx_val / fpx;
    fy := FX(y);
   
    // Refinement 2: Chun's correction
    denom := fx_val - 2*fy;
    IF ABS(denom) > 1E-15 THEN
      x := y - (fx_val / denom) * (fy / fpx);
    ELSE
      x := y;
    END;
  END;
 
  RETURN {x, FX(x), MaxIters};
END;


EXPORT MAHESHWARI(x, toler, MaxIters)
// Maheshwari's fourth-order method with 2 refinements
//
// Algorithm (3 function evaluations per iteration):
//   Refinement 1: y = x - f(x)/f'(x)              [Newton step]
//   Refinement 2: x_new = x - [f(x) + f(y)]/f'(x)
//
// Order of convergence: 4
// Efficiency index: 4^(1/3) ≈ 1.587
//
// Reference:
//   Maheshwari, A.K. (2009). Applied Mathematics and Computation, 211, 383-391
BEGIN
  LOCAL fx_val, fpx, y, fy;
  LOCAL k;
 
  FOR k FROM 1 TO MaxIters DO
    fx_val := FX(x);
   
    IF ABS(fx_val) < toler THEN
      RETURN {x, fx_val, k-1};
    END;
   
    fpx := FP(x);
    IF ABS(fpx) < 1E-15 THEN
      RETURN {x, fx_val, k};
    END;
   
    // Refinement 1: Newton step
    y := x - fx_val / fpx;
    fy := FX(y);
   
    // Refinement 2: Maheshwari correction
    x := x - (fx_val + fy) / fpx;
  END;
 
  RETURN {x, FX(x), MaxIters};
END;


// ============================================================================
// FOURTH-ORDER METHODS WITH 3 REFINEMENTS
// ============================================================================

EXPORT KING(x, toler, MaxIters)
// King's fourth-order method with 3 refinements
//
// Algorithm (4 function evaluations per iteration):
//   Refinement 1: y = x - f(x)/f'(x)              [Newton step]
//   Refinement 2: w = y - f(y)/f'(y)              [Second Newton]
//   Refinement 3: x_new = w - [f(x) + f(w)]/[f(x) - f(w)] * f(w)/f'(x)
//
// Order of convergence: 4
// Efficiency index: 4^(1/4) ≈ 1.414
//
// Reference:
//   King, R.F. (1973). SIAM Journal on Numerical Analysis, 10(5), 876-879
BEGIN
  LOCAL fx_val, fpx, y, fy, fpy, w, fw, denom;
  LOCAL k;
 
  FOR k FROM 1 TO MaxIters DO
    fx_val := FX(x);
   
    IF ABS(fx_val) < toler THEN
      RETURN {x, fx_val, k-1};
    END;
   
    fpx := FP(x);
    IF ABS(fpx) < 1E-15 THEN
      RETURN {x, fx_val, k};
    END;
   
    // Refinement 1: First Newton step
    y := x - fx_val / fpx;
    fy := FX(y);
   
    // Refinement 2: Second Newton step
    fpy := FP(y);
    IF ABS(fpy) > 1E-15 THEN
      w := y - fy / fpy;
    ELSE
      w := y;
    END;
    fw := FX(w);
   
    // Refinement 3: King's correction
    denom := fx_val - fw;
    IF ABS(denom) > 1E-15 THEN
      x := w - (fx_val + fw) / denom * (fw / fpx);
    ELSE
      x := w;
    END;
  END;
 
  RETURN {x, FX(x), MaxIters};
END;


// ============================================================================
// EIGHTH-ORDER METHODS WITH 3 REFINEMENTS
// ============================================================================

EXPORT THUKRAL(x, toler, MaxIters)
// Thukral-Petkovic eighth-order method with 3 refinements
//
// Algorithm (4 function evaluations per iteration):
//   Refinement 1: y = x - f(x)/f'(x)
//   Refinement 2: z = y - [f(y)/f'(x)] * [f(x)/(f(x) - 2*f(y))]
//   Refinement 3: x_new = z - [f(z)/f'(x)] * H(t,s,u)
//   where: t = f(y)/f(x), s = f(z)/f(y), u = f(z)/f(x)
//          H = 1 + 2*t + t^2 + s + s*t + s*t^2
//
// Order of convergence: 8
// Efficiency index: 8^(1/4) ≈ 1.682
//
// Reference:
//   Thukral, R. and Petkovic, M.S. (2010). Journal of Computational
//   and Applied Mathematics, 233, 2278-2284
BEGIN
  LOCAL fx_val, fpx, y, fy, z, fz, denom;
  LOCAL t, s, u, H;
  LOCAL k;
 
  FOR k FROM 1 TO MaxIters DO
    fx_val := FX(x);
   
    IF ABS(fx_val) < toler THEN
      RETURN {x, fx_val, k-1};
    END;
   
    fpx := FP(x);
    IF ABS(fpx) < 1E-15 THEN
      RETURN {x, fx_val, k};
    END;
   
    // Refinement 1: Newton step
    y := x - fx_val / fpx;
    fy := FX(y);
   
    // Refinement 2: Ostrowski-type step
    denom := fx_val - 2*fy;
    IF ABS(denom) > 1E-15 THEN
      z := y - (fy / fpx) * (fx_val / denom);
    ELSE
      z := y;
    END;
    fz := FX(z);
   
    // Refinement 3: Eighth-order correction
    IF ABS(fx_val) > 1E-15 AND ABS(fy) > 1E-15 THEN
      t := fy / fx_val;
      s := fz / fy;
      u := fz / fx_val;
      H := 1 + 2*t + t^2 + s + s*t + s*t^2;
      x := z - (fz / fpx) * H;
    ELSE
      x := z;
    END;
  END;
 
  RETURN {x, FX(x), MaxIters};
END;


EXPORT BI(x, toler, MaxIters)
// Bi-Ren-Wu eighth-order method with 3 refinements
//
// Algorithm (4 function evaluations per iteration):
//   Refinement 1: y = x - f(x)/f'(x)
//   Refinement 2: z = y - [f(y)/f'(x)] * [f(x) + f(y)]/[f(x) - f(y)]
//   Refinement 3: x_new = z - [f(z)/f'(x)] * [1 + u + t*(1 + 2*u + u^2)]
//   where: t = f(y)/f(x), u = f(z)/f(y)
//
// Order of convergence: 8
// Efficiency index: 8^(1/4) ≈ 1.682
//
// Reference:
//   Bi, W., Ren, H., Wu, Q. (2009). Journal of Computational
//   and Applied Mathematics, 225, 105-112
BEGIN
  LOCAL fx_val, fpx, y, fy, z, fz, denom;
  LOCAL t, u, weight;
  LOCAL k;
 
  FOR k FROM 1 TO MaxIters DO
    fx_val := FX(x);
   
    IF ABS(fx_val) < toler THEN
      RETURN {x, fx_val, k-1};
    END;
   
    fpx := FP(x);
    IF ABS(fpx) < 1E-15 THEN
      RETURN {x, fx_val, k};
    END;
   
    // Refinement 1: Newton step
    y := x - fx_val / fpx;
    fy := FX(y);
   
    // Refinement 2: Modified Ostrowski step
    denom := fx_val - fy;
    IF ABS(denom) > 1E-15 THEN
      z := y - (fy / fpx) * (fx_val + fy) / denom;
    ELSE
      z := y;
    END;
    fz := FX(z);
   
    // Refinement 3: Eighth-order correction
    IF ABS(fx_val) > 1E-15 AND ABS(fy) > 1E-15 THEN
      t := fy / fx_val;
      u := fz / fy;
      weight := 1 + u + t*(1 + 2*u + u^2);
      x := z - (fz / fpx) * weight;
    ELSE
      x := z;
    END;
  END;
 
  RETURN {x, FX(x), MaxIters};
END;


EXPORT SHARMA(x, toler, MaxIters)
// Sharma-Sharma eighth-order method with 3 refinements
//
// Algorithm (4 function evaluations per iteration):
//   Refinement 1: y = x - 2*f(x)/(3*f'(x))
//   Refinement 2: z = x - [f(x)/f'(x)] * [f(x) - 3*f(y)]/[f(x) - f(y)]
//   Refinement 3: x_new = z - [f(z)/f'(x)] * (1 + 2*t)^2 * (1 + u + u^2)
//   where: t = f(y)/f(x), u = f(z)/f(y)
//
// Order of convergence: 8
// Efficiency index: 8^(1/4) ≈ 1.682
//
// Reference:
//   Sharma, J.R. and Sharma, R. (2010). Numerical Algorithms, 54, 445-458
BEGIN
  LOCAL fx_val, fpx, y, fy, z, fz, denom;
  LOCAL t, u, weight;
  LOCAL k;
 
  FOR k FROM 1 TO MaxIters DO
    fx_val := FX(x);
   
    IF ABS(fx_val) < toler THEN
      RETURN {x, fx_val, k-1};
    END;
   
    fpx := FP(x);
    IF ABS(fpx) < 1E-15 THEN
      RETURN {x, fx_val, k};
    END;
   
    // Refinement 1: Modified Newton step
    y := x - 2*fx_val / (3*fpx);
    fy := FX(y);
   
    // Refinement 2: Weighted correction
    denom := fx_val - fy;
    IF ABS(denom) > 1E-15 THEN
      z := x - (fx_val / fpx) * (fx_val - 3*fy) / denom;
    ELSE
      z := x;
    END;
    fz := FX(z);
   
    // Refinement 3: Eighth-order correction
    IF ABS(fx_val) > 1E-15 AND ABS(fy) > 1E-15 THEN
      t := fy / fx_val;
      u := fz / fy;
      weight := (1 + 2*t)^2 * (1 + u + u^2);
      x := z - (fz / fpx) * weight;
    ELSE
      x := z;
    END;
  END;
 
  RETURN {x, FX(x), MaxIters};
END;


EXPORT CORDERO(x, toler, MaxIters)
// Cordero-Torregrosa eighth-order method with 3 refinements
//
// Algorithm (4 function evaluations per iteration):
//   Refinement 1: y = x - f(x)/f'(x)
//   Refinement 2: z = y - [f(y)/f'(x)] * [f(x)^2/(f(x) - f(y))^2]
//   Refinement 3: x_new = z - [f(z)/f'(x)] * [1 + 2*t/(1-t) + (u/(1-u))^2]
//   where: t = f(y)/f(x), u = f(z)/f(y)
//
// Order of convergence: 8
// Efficiency index: 8^(1/4) ≈ 1.682
//
// Reference:
//   Cordero, A., Torregrosa, J.R. (2007). Applied Mathematics
//   and Computation, 190, 686-698
BEGIN
  LOCAL fx_val, fpx, y, fy, z, fz, denom;
  LOCAL t, u, weight;
  LOCAL k;
 
  FOR k FROM 1 TO MaxIters DO
    fx_val := FX(x);
   
    IF ABS(fx_val) < toler THEN
      RETURN {x, fx_val, k-1};
    END;
   
    fpx := FP(x);
    IF ABS(fpx) < 1E-15 THEN
      RETURN {x, fx_val, k};
    END;
   
    // Refinement 1: Newton step
    y := x - fx_val / fpx;
    fy := FX(y);
   
    // Refinement 2: Quadratic correction
    denom := fx_val - fy;
    IF ABS(denom) > 1E-15 THEN
      z := y - (fy / fpx) * (fx_val / denom)^2;
    ELSE
      z := y;
    END;
    fz := FX(z);
   
    // Refinement 3: Eighth-order correction
    IF ABS(fx_val) > 1E-15 AND ABS(fy) > 1E-15 THEN
      t := fy / fx_val;
      u := fz / fy;
      IF ABS(1-t) > 1E-15 AND ABS(1-u) > 1E-15 THEN
        weight := 1 + 2*t/(1-t) + (u/(1-u))^2;
        x := z - (fz / fpx) * weight;
      ELSE
        x := z;
      END;
    ELSE
      x := z;
    END;
  END;
 
  RETURN {x, FX(x), MaxIters};
END;


EXPORT LIU(x, toler, MaxIters)
// Liu eighth-order method with 3 refinements
//
// Algorithm (4 function evaluations per iteration):
//   Refinement 1: y = x - f(x)/f'(x)
//   Refinement 2: z = y - [f(y)/f'(x)] * [1/(1 - f(y)/f(x))]
//   Refinement 3: x_new = z - [f(z)/f'(x)] * P(t,s)
//   where: t = f(y)/f(x), s = f(z)/f(y)
//          P(t,s) = 1 + t/(1-t) + s + s*t + s^2
//
// Order of convergence: 8
// Efficiency index: 8^(1/4) ≈ 1.682
//
// Reference:
//   Liu, T., et al. (2013). Applied Mathematics and Computation, 215, 3449-3454
BEGIN
  LOCAL fx_val, fpx, y, fy, z, fz;
  LOCAL t, s, P;
  LOCAL k;
 
  FOR k FROM 1 TO MaxIters DO
    fx_val := FX(x);
   
    IF ABS(fx_val) < toler THEN
      RETURN {x, fx_val, k-1};
    END;
   
    fpx := FP(x);
    IF ABS(fpx) < 1E-15 THEN
      RETURN {x, fx_val, k};
    END;
   
    // Refinement 1: Newton step
    y := x - fx_val / fpx;
    fy := FX(y);
   
    // Refinement 2: Corrected step
    IF ABS(fx_val) > 1E-15 THEN
      t := fy / fx_val;
      IF ABS(1 - t) > 1E-15 THEN
        z := y - (fy / fpx) / (1 - t);
      ELSE
        z := y;
      END;
    ELSE
      z := y;
    END;
    fz := FX(z);
   
    // Refinement 3: Eighth-order correction
    IF ABS(fx_val) > 1E-15 AND ABS(fy) > 1E-15 THEN
      t := fy / fx_val;
      s := fz / fy;
      IF ABS(1-t) > 1E-15 THEN
        P := 1 + t/(1-t) + s + s*t + s^2;
        x := z - (fz / fpx) * P;
      ELSE
        x := z;
      END;
    ELSE
      x := z;
    END;
  END;
 
  RETURN {x, FX(x), MaxIters};
END;


// ============================================================================
// SIXTEENTH-ORDER METHODS WITH 4 REFINEMENTS
// ============================================================================

EXPORT SOLEYMANI(x, toler, MaxIters)
// Soleymani sixteenth-order method with 4 refinements
//
// Algorithm (5 function evaluations per iteration):
//   Refinement 1: y = x - f(x)/f'(x)
//   Refinement 2: z = y - [f(y)/f'(x)] * [1 + 2*f(y)/f(x)]
//   Refinement 3: w = z - [f(z)/f'(x)] * [1 + 2*t + t^2 + s]
//   Refinement 4: x_new = w - [f(w)/f'(x)] * H(t,s,u,v)
//   where: t = f(y)/f(x), s = f(z)/f(y), u = f(w)/f(z), v = f(w)/f(x)
//          H = (1+2*t)*(1+s+s^2)*(1+u)*(1+2*v)
//
// Order of convergence: 16
// Efficiency index: 16^(1/5) ≈ 1.741
//
// Reference:
//   Soleymani, F., et al. (2012). Mathematical and Computer
//   Modelling, 55, 1373-1380
BEGIN
  LOCAL fx_val, fpx, y, fy, z, fz, w, fw;
  LOCAL t, s, u, v, H;
  LOCAL k;
 
  FOR k FROM 1 TO MaxIters DO
    fx_val := FX(x);
   
    IF ABS(fx_val) < toler THEN
      RETURN {x, fx_val, k-1};
    END;
   
    fpx := FP(x);
    IF ABS(fpx) < 1E-15 THEN
      RETURN {x, fx_val, k};
    END;
   
    // Refinement 1: Newton step
    y := x - fx_val / fpx;
    fy := FX(y);
   
    // Refinement 2: Second-order correction
    IF ABS(fx_val) > 1E-15 THEN
      t := fy / fx_val;
      z := y - (fy / fpx) * (1 + 2*t);
    ELSE
      z := y;
    END;
    fz := FX(z);
   
    // Refinement 3: Fourth-order correction
    IF ABS(fx_val) > 1E-15 AND ABS(fy) > 1E-15 THEN
      t := fy / fx_val;
      s := fz / fy;
      w := z - (fz / fpx) * (1 + 2*t + t^2 + s);
    ELSE
      w := z;
    END;
    fw := FX(w);
   
    // Refinement 4: Sixteenth-order correction
    IF ABS(fx_val) > 1E-15 AND ABS(fy) > 1E-15 AND ABS(fz) > 1E-15 THEN
      t := fy / fx_val;
      s := fz / fy;
      u := fw / fz;
      v := fw / fx_val;
      H := (1 + 2*t) * (1 + s + s^2) * (1 + u) * (1 + 2*v);
      x := w - (fw / fpx) * H;
    ELSE
      x := w;
    END;
  END;
 
  RETURN {x, FX(x), MaxIters};
END;


EXPORT BEHL(x, toler, MaxIters)
// Behl sixteenth-order method with 4 refinements
//
// Algorithm (5 function evaluations per iteration):
//   Refinement 1: y = x - f(x)/f'(x)
//   Refinement 2: z = y - f(y)/f'(y)
//   Refinement 3: w = z - [f(z)/f'(x)] * [1 + t + s + 2*t*s]
//   Refinement 4: x_new = w - [f(w)/f'(x)] * G(t,s,u)
//   where: t = f(y)/f(x), s = f(z)/f(y), u = f(w)/f(z)
//          G = (1+t)*(1+s)*(1+s^2)*(1+u+u^2)
//
// Order of convergence: 16
// Efficiency index: 16^(1/5) ≈ 1.741
//
// Reference:
//   Behl, R., et al. (2016). Journal of Computational and Applied
//   Mathematics, 295, 184-193
BEGIN
  LOCAL fx_val, fpx, y, fy, fpy, z, fz;
  LOCAL w, fw;
  LOCAL t, s, u, G;
  LOCAL k;
 
  FOR k FROM 1 TO MaxIters DO
    fx_val := FX(x);
   
    IF ABS(fx_val) < toler THEN
      RETURN {x, fx_val, k-1};
    END;
   
    fpx := FP(x);
    IF ABS(fpx) < 1E-15 THEN
      RETURN {x, fx_val, k};
    END;
   
    // Refinement 1: Newton step
    y := x - fx_val / fpx;
    fy := FX(y);
   
    // Refinement 2: Second Newton step
    fpy := FP(y);
    IF ABS(fpy) > 1E-15 THEN
      z := y - fy / fpy;
    ELSE
      z := y;
    END;
    fz := FX(z);
   
    // Refinement 3: Eighth-order correction
    IF ABS(fx_val) > 1E-15 AND ABS(fy) > 1E-15 THEN
      t := fy / fx_val;
      s := fz / fy;
      w := z - (fz / fpx) * (1 + t + s + 2*t*s);
    ELSE
      w := z;
    END;
    fw := FX(w);
   
    // Refinement 4: Sixteenth-order correction
    IF ABS(fx_val) > 1E-15 AND ABS(fy) > 1E-15 AND ABS(fz) > 1E-15 THEN
      t := fy / fx_val;
      s := fz / fy;
      u := fw / fz;
      G := (1 + t) * (1 + s) * (1 + s^2) * (1 + u + u^2);
      x := w - (fw / fpx) * G;
    ELSE
      x := w;
    END;
  END;
 
  RETURN {x, FX(x), MaxIters};
END;


EXPORT SHARMAARORA(x, toler, MaxIters)
// Sharma-Arora sixteenth-order method with 4 refinements
//
// Algorithm (5 function evaluations per iteration):
//   Refinement 1: y = x - (3/4) * f(x)/f'(x)
//   Refinement 2: z = x - f(x)/[f'(x)*(1-t/2)^2]
//   Refinement 3: w = z - [f(z)/f'(x)] * P(t,s)
//   Refinement 4: x_new = w - [f(w)/f'(x)] * Q(t,s,u)
//   where: t = f(y)/f(x), s = f(z)/f(y), u = f(w)/f(z)
//          P(t,s) = 1 + t + t^2 + s*(1+t)
//          Q(t,s,u) = P(t,s) * (1 + u + s*u)
//
// Order of convergence: 16
// Efficiency index: 16^(1/5) ≈ 1.741
//
// Reference:
//   Sharma, J.R. and Arora, H. (2013). Calcolo, 51, 193-210
BEGIN
  LOCAL fx_val, fpx, y, fy, z, fz;
  LOCAL w, fw, denom;
  LOCAL t, s, u, P, Q;
  LOCAL k;
 
  FOR k FROM 1 TO MaxIters DO
    fx_val := FX(x);
   
    IF ABS(fx_val) < toler THEN
      RETURN {x, fx_val, k-1};
    END;
   
    fpx := FP(x);
    IF ABS(fpx) < 1E-15 THEN
      RETURN {x, fx_val, k};
    END;
   
    // Refinement 1: Modified Newton step
    y := x - (3/4) * (fx_val / fpx);
    fy := FX(y);
   
    // Refinement 2: Weighted Newton step
    IF ABS(fx_val) > 1E-15 THEN
      t := fy / fx_val;
      denom := (1 - t/2)^2;
      IF ABS(denom) > 1E-15 THEN
        z := x - (fx_val / fpx) / denom;
      ELSE
        z := x;
      END;
    ELSE
      z := x;
    END;
    fz := FX(z);
   
    // Refinement 3: Eighth-order correction
    IF ABS(fx_val) > 1E-15 AND ABS(fy) > 1E-15 THEN
      t := fy / fx_val;
      s := fz / fy;
      P := 1 + t + t^2 + s*(1 + t);
      w := z - (fz / fpx) * P;
    ELSE
      w := z;
    END;
    fw := FX(w);
   
    // Refinement 4: Sixteenth-order correction
    IF ABS(fx_val) > 1E-15 AND ABS(fy) > 1E-15 AND ABS(fz) > 1E-15 THEN
      t := fy / fx_val;
      s := fz / fy;
      u := fw / fz;
      P := 1 + t + t^2 + s*(1 + t);
      Q := P * (1 + u + s*u);
      x := w - (fw / fpx) * Q;
    ELSE
      x := w;
    END;
  END;
 
  RETURN {x, FX(x), MaxIters};
END;


// ============================================================================
// USAGE EXAMPLES (Comments only - not executable code)
// ============================================================================

// To use these functions with the predefined FX function:
//
// 1. Define your function FX (already provided):
//    EXPORT FX(x)
//    BEGIN
//      RETURN exp(x)-3*x^2;
//    END;
//
// 2. Call any root-finding method:
//    result := OSTROWSKI(1.0, 1E-10, 50);
//    // Returns {root, function_value, iterations}
//
// 3. Access results:
//    root := result(1);        // The approximate root
//    fval := result(2);        // Function value at root
//    iters := result(3);       // Number of iterations
//
// 4. Compare methods:
//    r1 := OSTROWSKI(1.0, 1E-10, 50);
//    r2 := THUKRAL(1.0, 1E-10, 50);
//    r3 := SOLEYMANI(1.0, 1E-10, 50);
//
// Methods by order and refinements:
//   4th order, 2 refinements: OSTROWSKI, KUNGTRAUB, JARRATT, CHUN, MAHESHWARI
//   4th order, 3 refinements: KING
//   8th order, 3 refinements: THUKRAL, BI, SHARMA, CORDERO, LIU
//   16th order, 4 refinements: SOLEYMANI, BEHL, SHARMAARORA

Here are some sample runs:

Code:
OSTROWSKI(4,0.00000001,1000) gives {3.73307902863,−0.0000000001,3}
OSTROWSKI(-1,0.00000001,1000) gives {−0.458962267537,−0.000000000001,3}
KUNGTRAUB(4,0.00000001,1000) gives {3.73307902863,−0.0000000001,3}
CHUN(4,0.00000001,1000) gives {3.73307902863,−0.0000000001,2}
MAHESHWARI(4,0.00000001,1000) gives {3.73307902863,−0.0000000001,3}
KING(4,0.00000001,1000) gives {3.73307902864,0.0000000001,2}
THUKRAL(4,0.00000001,1000) gives {3.73307902863,−0.0000000001,2}
BI(4,0.00000001,1000) gives {3.73307902863,−0.0000000001,2}
CORDERO(4,0.00000001,1000) gives {3.73307902864,0.0000000001,2}
LIU(4,0.00000001,1000) gives {3.73307902864,0.0000000001,2}
SOLEYMANI(4,0.00000001,1000) gives {3.73307902864,0.0000000001,2}
BEHL(4,0.00000001,1000) gives {3.73307902863,−0.0000000001,2}
SHARMAARORA(4,0.00000001,1000) gives {3.73307902837,−0.0000000052,21}