1

I want to solve the equation of the form ax^2 -bx + c for various values of defined parameters a,b, and c in 1D arrays A, B, and C respectively.

const A = [1, 1, 1]
const B = [5, 3, 2];
const C = [4, 2, 1];
var i = 0
var x0 = [10, 12, 11];
var x1 = [];
var fx = [];
var fxp = [];
const tol = [0.1, 0.1, 0.1]
for (let j = 0; j < A.length; j++) {
    do {
        x1[j] = x0[j];
        fx[j] = A[j] * x1[j] * x1[j] - B[j] * x1[j] + C[j]; //function 
        fxp[j] = 2 * A[j] * x1[j] - B[j]; //first derivative of f(x)
        x0[j] = x1[j] - fx[j] / fxp[j];
        i++
        if (i > 100) break;
    } while (x0[j] - x1[j] < tol[j])
}

console.log("x1", x1)

I placed the do-while loop iterator in a for-loop, but my concerns/issues are:

  1. Only the first equation is solved (i.e A[0], B[0], C[0]), the other two only return the initial guesses of x1.
  2. Is there a better way to achieve my goal, as looping the iterator in a for-loop can be computationally expensive when A.length >>> large?
yqlim
  • 6,898
  • 3
  • 19
  • 43
Isaac
  • 396
  • 3
  • 13
  • What is `x0`, `x1`, `fx`, `fxp` and `tol`? What is the purpose of `if (i > 100) break;`? What really is the relationship between `A`, `B` and `C`? – yqlim Mar 14 '19 at 01:28
  • fx is the function whose root(s) is/are to be determined, fxp is the first derivative of fx, x0 is the initial guess of the root but it is updated after each iteration, x1 is the root, tol is the tolerance value. I put if (i > 100) break to put a break when the iteration exceeds 100 – Isaac Mar 14 '19 at 02:49
  • In the case `B = [5, 3, 2]`, is this correct: `a = 5; b = 3; c = 2`? – yqlim Mar 14 '19 at 03:24
  • There are three equations, the coefficients are in the arrays A, B, C. So all the a's are in array A, the b's in array B and c's in array C. The equations are thus: (1) 1x^2-5x+4 (2) 1x^2-3x+2 (3) 1x^2-2x+1 – Isaac Mar 14 '19 at 03:39
  • @YongQuan I only used three equations in this case, but it can be n-number of equations. – Isaac Mar 14 '19 at 04:43

1 Answers1

1

The code is fashioned for quadratic equation but can be easily adapted to other equations, provided you get the first derivative of the equation. "fx" is the function, while "fxp" is the first derivative.

Newton-Raphson is very sensitive to initialization, as this example presents; and for quadratic functions with two roots, you will get two different depending on the initial values...so choose initial values carefully.

//CASE ONE
const A = [1, 1, 1]
const B = [5, 3, 2];
const C = [4, 2, 1];
var x0 = [14, 13, 11];
var x1 = [14, 13, 11];
var fx = [];
var fxp = [];
const tol = [0.000001, 0.000001, 0.000001]
const error = [];
for (i = 0; i < 1000; i++) {
    for (let j = 0; j < A.length; j++) {
        x1[j] = x0[j];
        fx[j] = A[j] * x1[j] * x1[j] - B[j] * x1[j] + C[j]; //function 
        fxp[j] = 2 * A[j] * x1[j] - B[j]; //first derivative of f(x)
        x0[j] = x1[j] - fx[j] / fxp[j];
        error[j] = Math.abs(x1[j] - x0[j]);
        if (error[j] < tol[j]) {
            break;
        }
    }
}

console.log("x1", x1, "error", error)

//CASE TWO
const A = [1, 1, 1]
const B = [5, 3, 2];
const C = [4, 2, 1];
var x0 = [0, 0, 0];
var x1 = [0, 0, 0];
var fx = [];
var fxp = [];
const tol = [0.000001, 0.000001, 0.000001]
const error = [];
for (i = 0; i < 1000; i++) {
    for (let j = 0; j < A.length; j++) {
        x1[j] = x0[j];
        fx[j] = A[j] * x1[j] * x1[j] - B[j] * x1[j] + C[j]; //function 
        fxp[j] = 2 * A[j] * x1[j] - B[j]; //first derivative of f(x)
        x0[j] = x1[j] - fx[j] / fxp[j];
        error[j] = Math.abs(x1[j] - x0[j]);
        if (error[j] < tol[j]) {
            break;
        }
    }
}

console.log("x1", x1, "error", error)
Isaac
  • 396
  • 3
  • 13