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:
- Only the first equation is solved (i.e A[0], B[0], C[0]), the other two only return the initial guesses of x1.
- 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?