Skip to content

Commit 5b41c20

Browse files
authored
Merge pull request #6 from Pedrop19/patch-3
Update equations.js
2 parents 35eac41 + ffa7d0a commit 5b41c20

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

adv-math/src/equation-solvers/equations.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,48 @@ class Equations {
1818
const x2 = (-b - sqrtDiscriminant) / (2 * a);
1919
return [x1, x2];
2020
}
21+
22+
// Method to solve a cubic equation of the form: ax^3 + bx^2 + cx + d = 0
23+
static solveCubic(a, b, c, d) {
24+
// Implementation of Cardano's method to solve cubic equations
25+
if (a === 0) {
26+
throw new Error('The coefficient of x^3 cannot be zero.');
27+
}
28+
29+
const delta0 = b * b - 3 * a * c;
30+
const delta1 = 2 * b * b * b - 9 * a * b * c + 27 * a * a * d;
31+
const C = Math.cbrt((delta1 + Math.sqrt(delta1 * delta1 - 4 * delta0 * delta0 * delta0)) / 2);
32+
const x1 = -(1 / (3 * a)) * (b + C + delta0 / C);
33+
return [x1];
34+
}
35+
36+
// Method to solve an exponential equation of the form: a^x = b
37+
static solveExponential(a, b) {
38+
if (a <= 0 || a === 1 || b <= 0) {
39+
throw new Error('Values of a and b must be positive, and a cannot be equal to 1.');
40+
}
41+
return Math.log(b) / Math.log(a);
42+
}
43+
44+
// Method to solve a logarithmic equation of the form: log_a(bx + c) = d
45+
static solveLogarithmic(a, b, c, d) {
46+
if (a <= 0 || a === 1 || b <= 0 || c < 0) {
47+
throw new Error('Values of a, b, and c must be positive, and a cannot be equal to 1.');
48+
}
49+
const logValue = d / Math.log(a);
50+
return (Math.pow(a, logValue) - c) / b;
51+
}
52+
53+
// Method to solve a system of linear equations
54+
static solveLinearSystem(a1, b1, c1, a2, b2, c2) {
55+
const determinant = a1 * b2 - a2 * b1;
56+
if (determinant === 0) {
57+
throw new Error('The system of equations has no unique solution.');
58+
}
59+
const x = (c1 * b2 - c2 * b1) / determinant;
60+
const y = (a1 * c2 - a2 * c1) / determinant;
61+
return [x, y];
62+
}
2163
}
2264

2365
module.exports = Equations;

0 commit comments

Comments
 (0)