Secant root finding: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Find a root of f(x)=x^3-x-2 with explicit convergence, iteration-limit and breakdown outcomes.

Use the two latest function values to form a secant line and take its horizontal-axis intersection. Replace the older iterate and repeat until the residual satisfies tolerance.

Open the 3D visualization

Secant root finding example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Secant root finding

Derivative-free local root refinement from two starting values.

Choosing an alternative

Secant avoids an analytic derivative but has no sign-bracket guarantee and may leave its starting interval. Bisection retains a bracket; Newton uses the exact derivative.

Worked example: input and state changes

Example input

1,2,0.000001,32

These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.

  1. Step 0: Objective f(x)=x^3-x-2; initial residual 4.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10,
        11,
        12,
        13,
        14,
        15,
        16,
        17,
        18,
        19,
        20,
        21,
        22,
        23,
        24,
        25,
        26,
        27,
        28,
        29,
        30,
        31,
        32,
        33,
        34,
        35,
        36,
        37,
        38,
        39,
        40,
        41,
        42
      ],
      "active": [
        41,
        42
      ],
      "kind": "graph",
      "points": [
        [
          1,
          -2
        ],
        [
          1.025,
          -1.948109375
        ],
        [
          1.05,
          -1.892375
        ],
        [
          1.075,
          -1.832703125
        ],
        [
          1.1,
          -1.7689999999999997
        ],
        [
          1.125,
          -1.701171875
        ],
        [
          1.15,
          -1.6291250000000004
        ],
        [
          1.175,
          -1.5527656249999997
        ],
        [
          1.2,
          -1.472
        ],
        [
          1.225,
          -1.3867343749999996
        ],
        [
          1.25,
          -1.296875
        ],
        [
          1.275,
          -1.2023281250000002
        ],
        [
          1.3,
          -1.1029999999999995
        ],
        [
          1.325,
          -0.9987968750000003
        ],
        [
          1.35,
          -0.8896249999999997
        ],
        [
          1.375,
          -0.775390625
        ],
        [
          1.4,
          -0.6560000000000006
        ],
        [
          1.425,
          -0.5313593749999999
        ],
        [
          1.45,
          -0.40137500000000004
        ],
        [
          1.475,
          -0.2659531249999998
        ],
        [
          1.5,
          -0.125
        ],
        [
          1.525,
          0.021578124999999115
        ],
        [
          1.55,
          0.17387500000000067
        ],
        [
          1.575,
          0.33198437499999933
        ],
        [
          1.6,
          0.4960000000000009
        ],
        [
          1.625,
          0.666015625
        ],
        [
          1.65,
          0.8421249999999998
        ],
        [
          1.675,
          1.0244218750000007
        ],
        [
          1.7,
          1.2129999999999992
        ],
        [
          1.725,
          1.407953125000001
        ],
        [
          1.75,
          1.609375
        ],
        [
          1.775,
          1.8173593749999992
        ],
        [
          1.8,
          2.032000000000001
        ],
        [
          1.825,
          2.2533906249999998
        ],
        [
          1.85,
          2.481625000000001
        ],
        [
          1.875,
          2.716796875
        ],
        [
          1.9,
          2.9589999999999996
        ],
        [
          1.925,
          3.2083281250000013
        ],
        [
          1.95,
          3.4648749999999993
        ],
        [
          1.975,
          3.728734375
        ],
        [
          2,
          4
        ],
        [
          2,
          0
        ],
        [
          2,
          4
        ]
      ],
      "nodeLabels": [
        "(1, -2)",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "(2, 4)",
        "(2, 0)",
        "(2, 4)"
      ],
      "pointLabelIndices": [
        42
      ],
      "pointMarkerIndices": [
        0,
        40,
        41,
        42
      ],
      "directed": false,
      "chosen": [
        40
      ],
      "visited": [],
      "result": 0,
      "output": [
        2,
        4,
        0
      ]
    }
  2. Step 2: Secant update 2: residual -0.3333388747951045.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10,
        11,
        12,
        13,
        14,
        15,
        16,
        17,
        18,
        19,
        20,
        21,
        22,
        23,
        24,
        25,
        26,
        27,
        28,
        29,
        30,
        31,
        32,
        33,
        34,
        35,
        36,
        37,
        38,
        39,
        40,
        41,
        42,
        43
      ],
      "active": [
        41,
        42,
        43
      ],
      "kind": "graph",
      "points": [
        [
          1,
          -2
        ],
        [
          1.025,
          -1.948109375
        ],
        [
          1.05,
          -1.892375
        ],
        [
          1.075,
          -1.832703125
        ],
        [
          1.1,
          -1.7689999999999997
        ],
        [
          1.125,
          -1.701171875
        ],
        [
          1.15,
          -1.6291250000000004
        ],
        [
          1.175,
          -1.5527656249999997
        ],
        [
          1.2,
          -1.472
        ],
        [
          1.225,
          -1.3867343749999996
        ],
        [
          1.25,
          -1.296875
        ],
        [
          1.275,
          -1.2023281250000002
        ],
        [
          1.3,
          -1.1029999999999995
        ],
        [
          1.325,
          -0.9987968750000003
        ],
        [
          1.35,
          -0.8896249999999997
        ],
        [
          1.375,
          -0.775390625
        ],
        [
          1.4,
          -0.6560000000000006
        ],
        [
          1.425,
          -0.5313593749999999
        ],
        [
          1.45,
          -0.40137500000000004
        ],
        [
          1.475,
          -0.2659531249999998
        ],
        [
          1.5,
          -0.125
        ],
        [
          1.525,
          0.021578124999999115
        ],
        [
          1.55,
          0.17387500000000067
        ],
        [
          1.575,
          0.33198437499999933
        ],
        [
          1.6,
          0.4960000000000009
        ],
        [
          1.625,
          0.666015625
        ],
        [
          1.65,
          0.8421249999999998
        ],
        [
          1.675,
          1.0244218750000007
        ],
        [
          1.7,
          1.2129999999999992
        ],
        [
          1.725,
          1.407953125000001
        ],
        [
          1.75,
          1.609375
        ],
        [
          1.775,
          1.8173593749999992
        ],
        [
          1.8,
          2.032000000000001
        ],
        [
          1.825,
          2.2533906249999998
        ],
        [
          1.85,
          2.481625000000001
        ],
        [
          1.875,
          2.716796875
        ],
        [
          1.9,
          2.9589999999999996
        ],
        [
          1.925,
          3.2083281250000013
        ],
        [
          1.95,
          3.4648749999999993
        ],
        [
          1.975,
          3.728734375
        ],
        [
          2,
          4
        ],
        [
          1.3333333333333335,
          -0.9629629629629624
        ],
        [
          1.462686567164179,
          0
        ],
        [
          1.462686567164179,
          -0.3333388747951045
        ]
      ],
      "nodeLabels": [
        "(1, -2)",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "(2, 4)",
        "(1.33333, -0.962963)",
        "(1.46269, 0)",
        "(1.46269, -0.333339)"
      ],
      "pointLabelIndices": [
        43
      ],
      "pointMarkerIndices": [
        0,
        40,
        41,
        42,
        43
      ],
      "directed": false,
      "chosen": [
        40,
        41
      ],
      "visited": [],
      "result": 0,
      "output": [
        1.462686567164179,
        -0.3333388747951045,
        2
      ]
    }
  3. Step 4: Secant update 4: residual -0.0026933002019973173.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10,
        11,
        12,
        13,
        14,
        15,
        16,
        17,
        18,
        19,
        20,
        21,
        22,
        23,
        24,
        25,
        26,
        27,
        28,
        29,
        30,
        31,
        32,
        33,
        34,
        35,
        36,
        37,
        38,
        39,
        40,
        41,
        42,
        43
      ],
      "active": [
        41,
        42,
        43
      ],
      "kind": "graph",
      "points": [
        [
          1,
          -2
        ],
        [
          1.025,
          -1.948109375
        ],
        [
          1.05,
          -1.892375
        ],
        [
          1.075,
          -1.832703125
        ],
        [
          1.1,
          -1.7689999999999997
        ],
        [
          1.125,
          -1.701171875
        ],
        [
          1.15,
          -1.6291250000000004
        ],
        [
          1.175,
          -1.5527656249999997
        ],
        [
          1.2,
          -1.472
        ],
        [
          1.225,
          -1.3867343749999996
        ],
        [
          1.25,
          -1.296875
        ],
        [
          1.275,
          -1.2023281250000002
        ],
        [
          1.3,
          -1.1029999999999995
        ],
        [
          1.325,
          -0.9987968750000003
        ],
        [
          1.35,
          -0.8896249999999997
        ],
        [
          1.375,
          -0.775390625
        ],
        [
          1.4,
          -0.6560000000000006
        ],
        [
          1.425,
          -0.5313593749999999
        ],
        [
          1.45,
          -0.40137500000000004
        ],
        [
          1.475,
          -0.2659531249999998
        ],
        [
          1.5,
          -0.125
        ],
        [
          1.525,
          0.021578124999999115
        ],
        [
          1.55,
          0.17387500000000067
        ],
        [
          1.575,
          0.33198437499999933
        ],
        [
          1.6,
          0.4960000000000009
        ],
        [
          1.625,
          0.666015625
        ],
        [
          1.65,
          0.8421249999999998
        ],
        [
          1.675,
          1.0244218750000007
        ],
        [
          1.7,
          1.2129999999999992
        ],
        [
          1.725,
          1.407953125000001
        ],
        [
          1.75,
          1.609375
        ],
        [
          1.775,
          1.8173593749999992
        ],
        [
          1.8,
          2.032000000000001
        ],
        [
          1.825,
          2.2533906249999998
        ],
        [
          1.85,
          2.481625000000001
        ],
        [
          1.875,
          2.716796875
        ],
        [
          1.9,
          2.9589999999999996
        ],
        [
          1.925,
          3.2083281250000013
        ],
        [
          1.95,
          3.4648749999999993
        ],
        [
          1.975,
          3.728734375
        ],
        [
          2,
          4
        ],
        [
          1.5311694321412044,
          0.058626417709449985
        ],
        [
          1.5209264205152802,
          0
        ],
        [
          1.5209264205152802,
          -0.0026933002019973173
        ]
      ],
      "nodeLabels": [
        "(1, -2)",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "(2, 4)",
        "(1.53117, 0.0586264)",
        "(1.52093, 0)",
        "(1.52093, -0.0026933)"
      ],
      "pointLabelIndices": [
        43
      ],
      "pointMarkerIndices": [
        0,
        40,
        41,
        42,
        43
      ],
      "directed": false,
      "chosen": [
        40,
        41
      ],
      "visited": [],
      "result": 0,
      "output": [
        1.5209264205152802,
        -0.0026933002019973173,
        4
      ]
    }
  4. Step 5: Secant update 5: residual -0.000020150192386658716.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10,
        11,
        12,
        13,
        14,
        15,
        16,
        17,
        18,
        19,
        20,
        21,
        22,
        23,
        24,
        25,
        26,
        27,
        28,
        29,
        30,
        31,
        32,
        33,
        34,
        35,
        36,
        37,
        38,
        39,
        40,
        41,
        42,
        43
      ],
      "active": [
        41,
        42,
        43
      ],
      "kind": "graph",
      "points": [
        [
          1,
          -2
        ],
        [
          1.025,
          -1.948109375
        ],
        [
          1.05,
          -1.892375
        ],
        [
          1.075,
          -1.832703125
        ],
        [
          1.1,
          -1.7689999999999997
        ],
        [
          1.125,
          -1.701171875
        ],
        [
          1.15,
          -1.6291250000000004
        ],
        [
          1.175,
          -1.5527656249999997
        ],
        [
          1.2,
          -1.472
        ],
        [
          1.225,
          -1.3867343749999996
        ],
        [
          1.25,
          -1.296875
        ],
        [
          1.275,
          -1.2023281250000002
        ],
        [
          1.3,
          -1.1029999999999995
        ],
        [
          1.325,
          -0.9987968750000003
        ],
        [
          1.35,
          -0.8896249999999997
        ],
        [
          1.375,
          -0.775390625
        ],
        [
          1.4,
          -0.6560000000000006
        ],
        [
          1.425,
          -0.5313593749999999
        ],
        [
          1.45,
          -0.40137500000000004
        ],
        [
          1.475,
          -0.2659531249999998
        ],
        [
          1.5,
          -0.125
        ],
        [
          1.525,
          0.021578124999999115
        ],
        [
          1.55,
          0.17387500000000067
        ],
        [
          1.575,
          0.33198437499999933
        ],
        [
          1.6,
          0.4960000000000009
        ],
        [
          1.625,
          0.666015625
        ],
        [
          1.65,
          0.8421249999999998
        ],
        [
          1.675,
          1.0244218750000007
        ],
        [
          1.7,
          1.2129999999999992
        ],
        [
          1.725,
          1.407953125000001
        ],
        [
          1.75,
          1.609375
        ],
        [
          1.775,
          1.8173593749999992
        ],
        [
          1.8,
          2.032000000000001
        ],
        [
          1.825,
          2.2533906249999998
        ],
        [
          1.85,
          2.481625000000001
        ],
        [
          1.875,
          2.716796875
        ],
        [
          1.9,
          2.9589999999999996
        ],
        [
          1.925,
          3.2083281250000013
        ],
        [
          1.95,
          3.4648749999999993
        ],
        [
          1.975,
          3.728734375
        ],
        [
          2,
          4
        ],
        [
          1.5209264205152802,
          -0.0026933002019973173
        ],
        [
          1.5213763166697438,
          0
        ],
        [
          1.5213763166697438,
          -0.000020150192386658716
        ]
      ],
      "nodeLabels": [
        "(1, -2)",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "(2, 4)",
        "(1.52093, -0.0026933)",
        "(1.52138, 0)",
        "(1.52138, -0.0000201502)"
      ],
      "pointLabelIndices": [
        43
      ],
      "pointMarkerIndices": [
        0,
        40,
        41,
        42,
        43
      ],
      "directed": false,
      "chosen": [
        40,
        41
      ],
      "visited": [],
      "result": 0,
      "output": [
        1.5213763166697438,
        -0.000020150192386658716,
        5
      ]
    }
  5. Step 7: Converged: residual or bisection half-width satisfies tolerance.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10,
        11,
        12,
        13,
        14,
        15,
        16,
        17,
        18,
        19,
        20,
        21,
        22,
        23,
        24,
        25,
        26,
        27,
        28,
        29,
        30,
        31,
        32,
        33,
        34,
        35,
        36,
        37,
        38,
        39,
        40,
        41,
        42
      ],
      "active": [
        41,
        42
      ],
      "kind": "graph",
      "points": [
        [
          1,
          -2
        ],
        [
          1.025,
          -1.948109375
        ],
        [
          1.05,
          -1.892375
        ],
        [
          1.075,
          -1.832703125
        ],
        [
          1.1,
          -1.7689999999999997
        ],
        [
          1.125,
          -1.701171875
        ],
        [
          1.15,
          -1.6291250000000004
        ],
        [
          1.175,
          -1.5527656249999997
        ],
        [
          1.2,
          -1.472
        ],
        [
          1.225,
          -1.3867343749999996
        ],
        [
          1.25,
          -1.296875
        ],
        [
          1.275,
          -1.2023281250000002
        ],
        [
          1.3,
          -1.1029999999999995
        ],
        [
          1.325,
          -0.9987968750000003
        ],
        [
          1.35,
          -0.8896249999999997
        ],
        [
          1.375,
          -0.775390625
        ],
        [
          1.4,
          -0.6560000000000006
        ],
        [
          1.425,
          -0.5313593749999999
        ],
        [
          1.45,
          -0.40137500000000004
        ],
        [
          1.475,
          -0.2659531249999998
        ],
        [
          1.5,
          -0.125
        ],
        [
          1.525,
          0.021578124999999115
        ],
        [
          1.55,
          0.17387500000000067
        ],
        [
          1.575,
          0.33198437499999933
        ],
        [
          1.6,
          0.4960000000000009
        ],
        [
          1.625,
          0.666015625
        ],
        [
          1.65,
          0.8421249999999998
        ],
        [
          1.675,
          1.0244218750000007
        ],
        [
          1.7,
          1.2129999999999992
        ],
        [
          1.725,
          1.407953125000001
        ],
        [
          1.75,
          1.609375
        ],
        [
          1.775,
          1.8173593749999992
        ],
        [
          1.8,
          2.032000000000001
        ],
        [
          1.825,
          2.2533906249999998
        ],
        [
          1.85,
          2.481625000000001
        ],
        [
          1.875,
          2.716796875
        ],
        [
          1.9,
          2.9589999999999996
        ],
        [
          1.925,
          3.2083281250000013
        ],
        [
          1.95,
          3.4648749999999993
        ],
        [
          1.975,
          3.728734375
        ],
        [
          2,
          4
        ],
        [
          1.5213797079848717,
          0
        ],
        [
          1.5213797079848717,
          7.015477976324291e-9
        ]
      ],
      "nodeLabels": [
        "(1, -2)",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "(2, 4)",
        "(1.52138, 0)",
        "(1.52138, 7.01548e-9)"
      ],
      "pointLabelIndices": [
        42
      ],
      "pointMarkerIndices": [
        0,
        40,
        41,
        42
      ],
      "directed": false,
      "chosen": [
        40
      ],
      "visited": [],
      "result": 1,
      "output": [
        1.5213797079848717,
        7.015477976324291e-9,
        6
      ]
    }

Expected final state

Converged: residual or bisection half-width satisfies tolerance.

Complexity and input bounds

Family: Numerical. Time complexity: O(I).

Implementation detail: Objective f(x)=x^3-x-2. Starting values lie in -3..3, tolerance in 1e-12..0.001 and iterations in 1..32. Downloads return 1 converged, 0 iteration limit, -1 invalid parameters, -2 missing bracket or near-zero divisor, or -3 a Newton/secant candidate outside [-10,10]. Output is [lastX,f(lastX),completedIterations]; invalid parameters leave it unchanged. Tests use a known root, hand-checked first steps and breakdown cases in all nine languages. Both displays plot the actual function coordinates; gold lines show bracket or slope updates.

Space complexity: O(1) numerical state excluding saved plot frames.

Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.

a,b,tolerance,maxIterations: starting values -3..3, tolerance 1e-12..0.001, iterations 1..32. Newton/secant stop if a candidate leaves [-10,10].

The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.

Common mistakes and limitations

Do not divide by nearly equal function values. Track the last two iterates in the correct order and report breakdown or domain exit separately from convergence.

Further reading

For background on algorithm analysis and the JavaScript function interface:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

javascript
1
2
3
4
5
6
// Objective f(x)=x^3-x-2, starting values -3..3, tolerance 1e-12..0.001 and iteration limit 1..32. Return 1 converged, 0 iteration limit, -1 invalid parameters (output unchanged), -2 no bracket or near-zero derivative/secant denominator, -3 candidate outside [-10,10]. Output three doubles [lastX,f(lastX),completedIterations]. Status is exact; compare real outputs with explicit tolerances. Bisection stops on residual or half-width, Newton/secant on residual only. Breakdown outputs retain the last finite tested iterate. O(I) work and O(1) auxiliary state. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation.
function algorithm(left, right, tolerance, limit, output) {
    if (Math.abs(left) > 3.0 || Math.abs(right) > 3.0 || tolerance < 1e-12 || tolerance > 0.001 || limit < 1 || limit > 32) { return -1; }let x = right; let fx = x*x*x-x-2.0; let iterations = 0; output[0] = x; output[1] = fx; output[2] = 0.0;
    let fa = left*left*left-left-2.0; let fb = fx; let divisor = 0.0; let next = 0.0;
    if (Math.abs(fx) <= tolerance) { return 1; } while (iterations < limit) { divisor = fb-fa; if (Math.abs(divisor) <= 1e-12) { return -2; } next = right-fb*(right-left)/divisor; if (Math.abs(next) > 10.0) { return -3; } left = right; fa = fb; right = next; fb = right*right*right-right-2.0; x = next; fx = x*x*x-x-2.0; iterations++; output[0] = x; output[1] = fx; output[2] = iterations; if (Math.abs(fx) <= tolerance) { return 1; } } return 0;
}

Source

NIST?s authored numerical-method reference documents this classical method. No original research-paper attribution is claimed. The playground uses the explicit cubic example and its own bounded iteration/status contract.