Published by Dimitris Kougioumtzis · Updated
Find a root of f(x)=x^3-x-2 with explicit convergence, iteration-limit and breakdown outcomes.
Evaluate the derivative at the current iterate and use its tangent intersection with the horizontal axis as the next iterate. Stop only when the residual satisfies tolerance.
When to use Newton–Raphson root finding
Fast local root refinement when a usable derivative and a suitable starting point are available.
Choosing an alternative
Newton uses an analytic derivative; secant estimates slope from two iterates. Neither has bisection?s bracket guarantee.
Worked example: input and state changes
Example input
1.5,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.
Step 0: Objective f(x)=x^3-x-2; initial residual -0.125.
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.5, 0 ], [ 1.5, -0.125 ] ], "nodeLabels": [ "(1, -2)", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "(2, 4)", "(1.5, 0)", "(1.5, -0.125)" ], "pointLabelIndices": [ 42 ], "pointMarkerIndices": [ 0, 40, 41, 42 ], "directed": false, "chosen": [ 40 ], "visited": [], "result": 0, "output": [ 1.5, -0.125, 0 ] }Step 1: Tangent update 1: residual 0.0021369277554041943.
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.5, -0.125 ], [ 1.5217391304347827, 0 ], [ 1.5217391304347827, 0.0021369277554041943 ] ], "nodeLabels": [ "(1, -2)", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "(2, 4)", "(1.5, -0.125)", "(1.52174, 0)", "(1.52174, 0.00213693)" ], "pointLabelIndices": [ 43 ], "pointMarkerIndices": [ 0, 40, 41, 42, 43 ], "directed": false, "chosen": [ 40, 41 ], "visited": [], "result": 0, "output": [ 1.5217391304347827, 0.0021369277554041943, 1 ] }Step 2: Tangent update 2: residual 5.893874259754739e-7.
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.5217391304347827, 0.0021369277554041943 ], [ 1.5213798059647863, 0 ], [ 1.5213798059647863, 5.893874259754739e-7 ] ], "nodeLabels": [ "(1, -2)", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "(2, 4)", "(1.52174, 0.00213693)", "(1.52138, 0)", "(1.52138, 5.89387e-7)" ], "pointLabelIndices": [ 43 ], "pointMarkerIndices": [ 0, 40, 41, 42, 43 ], "directed": false, "chosen": [ 40, 41 ], "visited": [], "result": 0, "output": [ 1.5213798059647863, 5.893874259754739e-7, 2 ] }Step 3: 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.5213798059647863, 0 ], [ 1.5213798059647863, 5.893874259754739e-7 ] ], "nodeLabels": [ "(1, -2)", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "(2, 4)", "(1.52138, 0)", "(1.52138, 5.89387e-7)" ], "pointLabelIndices": [ 42 ], "pointMarkerIndices": [ 0, 40, 41, 42 ], "directed": false, "chosen": [ 40 ], "visited": [], "result": 1, "output": [ 1.5213798059647863, 5.893874259754739e-7, 2 ] }
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.
x0,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
A small derivative can create a very large step. Detect near-zero derivatives and bounded-domain exits separately from iteration exhaustion. A finite step does not prove convergence.
Further reading
For background on algorithm analysis and the JavaScript function interface:
- MIT OpenCourseWare: algorithm design and analysis foundations
- MDN: JavaScript function parameters and return values
Editorial and testing approach · Browse all algorithm guides
Complete JavaScript implementation
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(start, tolerance, limit, output) { if (Math.abs(start) > 3.0 || tolerance < 1e-12 || tolerance > 0.001 || limit < 1 || limit > 32) { return -1; }let x = start; let fx = x*x*x-x-2.0; let iterations = 0; output[0] = x; output[1] = fx; output[2] = 0.0; let divisor = 0.0; let next = 0.0; if (Math.abs(fx) <= tolerance) { return 1; } while (iterations < limit) { divisor = 3.0*x*x-1.0; if (Math.abs(divisor) <= 1e-12) { return -2; } next = x-fx/divisor; if (Math.abs(next) > 10.0) { return -3; } 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.
- DLMF 3.8: Nonlinear Equations
N. M. Temme; NIST Digital Library of Mathematical Functions (2010?present). Authoritative numerical reference.