Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions adv-math/src/equation-solvers/equations.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,48 @@ class Equations {
const x2 = (-b - sqrtDiscriminant) / (2 * a);
return [x1, x2];
}

// Method to solve a cubic equation of the form: ax^3 + bx^2 + cx + d = 0
static solveCubic(a, b, c, d) {
// Implementation of Cardano's method to solve cubic equations
if (a === 0) {
throw new Error('The coefficient of x^3 cannot be zero.');
}

const delta0 = b * b - 3 * a * c;
const delta1 = 2 * b * b * b - 9 * a * b * c + 27 * a * a * d;
const C = Math.cbrt((delta1 + Math.sqrt(delta1 * delta1 - 4 * delta0 * delta0 * delta0)) / 2);
const x1 = -(1 / (3 * a)) * (b + C + delta0 / C);
return [x1];
}

// Method to solve an exponential equation of the form: a^x = b
static solveExponential(a, b) {
if (a <= 0 || a === 1 || b <= 0) {
throw new Error('Values of a and b must be positive, and a cannot be equal to 1.');
}
return Math.log(b) / Math.log(a);
}

// Method to solve a logarithmic equation of the form: log_a(bx + c) = d
static solveLogarithmic(a, b, c, d) {
if (a <= 0 || a === 1 || b <= 0 || c < 0) {
throw new Error('Values of a, b, and c must be positive, and a cannot be equal to 1.');
}
const logValue = d / Math.log(a);
return (Math.pow(a, logValue) - c) / b;
}

// Method to solve a system of linear equations
static solveLinearSystem(a1, b1, c1, a2, b2, c2) {
const determinant = a1 * b2 - a2 * b1;
if (determinant === 0) {
throw new Error('The system of equations has no unique solution.');
}
const x = (c1 * b2 - c2 * b1) / determinant;
const y = (a1 * c2 - a2 * c1) / determinant;
return [x, y];
}
}

module.exports = Equations;