#!/usr/bin/env node; /****************************************************************************** * This program computes the roots of cubic equations * of the form Ax^3 + Bx^2 + Cx + D = 0 * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ const A = 1.0; const B = -6.0; const C = 11.0; const D = -6.0; const main = async () => { /****************************************************************************** * General cubic can be changed to a depressed cubic * t^3 + pt + q = 0 * by substituting * x = t - B / 3A *****************************************************************************/ let p = (3. * A * C - B * B) / (3. * A * A); let q = (2. * B * B * B - 9. * A * B * C + 27. * A * A * D) / (27. * A * A * A); /****************************************************************************** * Roots of the depressed cubic are... * tk = a cos(b - 2πk/3) for k = 0, 1, 2 * with a = 2 sqrt(-p/3) * b = (1/3) arccos(3q/2p * sqrt(-3/p)) *****************************************************************************/ let a = 2. * Math.sqrt(-p / 3.); let b = (1. / 3.) * Math.acos(3. * q / 2. / p * Math.sqrt(-3. / p)); let t1 = a * Math.cos(b); let t2 = a * Math.cos(b - 2. * Math.PI / 3.); let t3 = a * Math.cos(b - 4. * Math.PI / 3.); let root1 = t1 - B / (3. * A); let root2 = t2 - B / (3. * A); let root3 = t3 - B / (3. * A); console.log(["root #1: ", root1].join('')); console.log(["root #2: ", root2].join('')); console.log(["root #3: ", root3].join('')); } main().catch( e => { console.error(e) } );