diff --git a/Calculators/Polynomial-Roots-Calculator/README.md b/Calculators/Polynomial-Roots-Calculator/README.md new file mode 100644 index 000000000..fa86e25ee --- /dev/null +++ b/Calculators/Polynomial-Roots-Calculator/README.md @@ -0,0 +1,15 @@ +#

Polynomial Roots Calculator

+ +## Description :- + +This Calculator is used to calculate the real and imaginary roots of a given polynomial more effectively and easily. It is designed to calculate the roots for degrees 2, 3, and 4 of polynomials. + +## Tech Stacks :- + +- HTML +- CSS +- JavaScript + +## Screenshots :- + +![image](https://github.com/Rakesh9100/CalcDiverse/assets/142529986/054ad35e-88ad-41f2-b235-0d25b7b65bfb) diff --git a/Calculators/Polynomial-Roots-Calculator/assets/boy.svg b/Calculators/Polynomial-Roots-Calculator/assets/boy.svg new file mode 100644 index 000000000..c053239e9 --- /dev/null +++ b/Calculators/Polynomial-Roots-Calculator/assets/boy.svg @@ -0,0 +1 @@ + diff --git a/Calculators/Polynomial-Roots-Calculator/index.html b/Calculators/Polynomial-Roots-Calculator/index.html new file mode 100644 index 000000000..29a20e21c --- /dev/null +++ b/Calculators/Polynomial-Roots-Calculator/index.html @@ -0,0 +1,65 @@ + + + + + + + + Polynomial Roots Calculator + + +
+
+

Roots Calculator

+
+ + +
+
+
+ + +
+
+ + +
+
+ + +
+ + +
+
+ + +
+
+ +
+
+ + +
+
+
+ + + + + + diff --git a/Calculators/Polynomial-Roots-Calculator/script.js b/Calculators/Polynomial-Roots-Calculator/script.js new file mode 100644 index 000000000..68794f18d --- /dev/null +++ b/Calculators/Polynomial-Roots-Calculator/script.js @@ -0,0 +1,125 @@ +// Function to show/hide coefficient inputs based on selected degree +function showCoefficientInputs() { + const degree = parseInt(document.getElementById('degree').value); + document.getElementById('d-group').style.display = degree >= 3 ? 'block' : 'none'; + document.getElementById('e-group').style.display = degree >= 4 ? 'block' : 'none'; +} + +// Function to evaluate polynomial at a given value of x +function evaluatePolynomial() { + const degree = parseInt(document.getElementById('degree').value); + const a = parseFloat(document.getElementById('a').value) || 0; + const b = parseFloat(document.getElementById('b').value) || 0; + const c = parseFloat(document.getElementById('c').value) || 0; + const d = degree >= 3 ? (parseFloat(document.getElementById('d').value) || 0) : 0; + const e = degree >= 4 ? (parseFloat(document.getElementById('e').value) || 0) : 0; + const x = parseFloat(document.getElementById('xValue').value); + + if (isNaN(x)) { + alert('Please enter a valid number for the value of x.'); + return; + } + + let result = a * Math.pow(x, degree) + b * Math.pow(x, degree - 1) + c * Math.pow(x, degree - 2); + if (degree >= 3) result += d * Math.pow(x, degree - 3); + if (degree >= 4) result += e * Math.pow(x, degree - 4); + + document.getElementById('result').value = result.toFixed(4); +} + +// Function to find roots of polynomial +function findRoots() { + const degree = parseInt(document.getElementById('degree').value); + const a = parseFloat(document.getElementById('a').value) || 0; + const b = parseFloat(document.getElementById('b').value) || 0; + const c = parseFloat(document.getElementById('c').value) || 0; + const d = degree >= 3 ? (parseFloat(document.getElementById('d').value) || 0) : 0; + const e = degree >= 4 ? (parseFloat(document.getElementById('e').value) || 0) : 0; + + let roots = []; + + if (degree === 2) { + roots = solveQuadratic(a, b, c); + } else if (degree === 3) { + roots = solveCubic(a, b, c, d); + } else if (degree === 4) { + roots = solveQuartic(a, b, c, d, e); + } + + document.getElementById('roots').value = roots.length > 0 ? roots.join(', ') : 'No real roots'; +} + +// Function to solve quadratic equation +function solveQuadratic(a, b, c) { + const discriminant = b * b - 4 * a * c; + if (discriminant > 0) { + const root1 = (-b + Math.sqrt(discriminant)) / (2 * a); + const root2 = (-b - Math.sqrt(discriminant)) / (2 * a); + return [root1.toFixed(4), root2.toFixed(4)]; + } else if (discriminant === 0) { + const root = -b / (2 * a); + return [root.toFixed(4)]; + } else { + const realPart = (-b / (2 * a)).toFixed(4); + const imaginaryPart = (Math.sqrt(-discriminant) / (2 * a)).toFixed(4); + return [`${realPart} + ${imaginaryPart}i`, `${realPart} - ${imaginaryPart}i`]; + } +} + +// Function to solve cubic equation (Cardano's method for simplicity) +function solveCubic(a, b, c, d) { + // Normalized coefficients + b /= a; + c /= a; + d /= a; + + const p = (3 * c - b * b) / 3; + const q = (2 * b * b * b - 9 * b * c + 27 * d) / 27; + const discriminant = Math.pow(q / 2, 2) + Math.pow(p / 3, 3); + + let roots = []; + if (discriminant > 0) { + const u = Math.cbrt(-q / 2 + Math.sqrt(discriminant)); + const v = Math.cbrt(-q / 2 - Math.sqrt(discriminant)); + roots.push((u + v - b / 3).toFixed(4)); + } else if (discriminant === 0) { + const u = Math.cbrt(-q / 2); + roots.push((2 * u - b / 3).toFixed(4)); + roots.push((-u - b / 3).toFixed(4)); + } else { + const r = Math.sqrt(Math.pow(-p / 3, 3)); + const phi = Math.acos(-q / (2 * r)); + for (let k = 0; k < 3; k++) { + const root = 2 * Math.cbrt(r) * Math.cos((phi + 2 * k * Math.PI) / 3) - b / 3; + roots.push(root.toFixed(4)); + } + } + return roots; +} + +// Function to solve quartic equation (Ferrari's method for simplicity) +function solveQuartic(a, b, c, d, e) { + // Normalized coefficients + b /= a; + c /= a; + d /= a; + e /= a; + + // Substitutions and solving steps (simplified) + const p = c - (3 * b * b) / 8; + const q = b * b * b / 8 - b * c / 2 + d; + const r = -3 * b * b * b * b / 256 + b * b * c / 16 - b * d / 4 + e; + + const cubicRoots = solveCubic(1, p / 2, (p * p - 4 * r) / 16, -q * q / 64); + const z = cubicRoots[0]; // One real root + + const u = Math.sqrt(z * z - 4 * r); + const v = Math.sqrt(2 * z - p); + + const root1 = (-b / 4 + (u + v) / 2).toFixed(4); + const root2 = (-b / 4 + (u - v) / 2).toFixed(4); + const root3 = (-b / 4 - (u + v) / 2).toFixed(4); + const root4 = (-b / 4 - (u - v) / 2).toFixed(4); + + return [root1, root2, root3, root4]; +} diff --git a/Calculators/Polynomial-Roots-Calculator/style.css b/Calculators/Polynomial-Roots-Calculator/style.css new file mode 100644 index 000000000..adf0bcbba --- /dev/null +++ b/Calculators/Polynomial-Roots-Calculator/style.css @@ -0,0 +1,78 @@ +body { + font-family: Arial, sans-serif; + display: flex; + justify-content: center; + align-items: center; + height: 100vh; + margin: 0; + background-color: rgb(244, 152, 81); + background: url("assets/boy.svg"); + background-size: cover; +} + +.calculator { + background: white; + padding: 20px; + border-radius: 8px; + box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); + max-width: 400px; + width: 110%; + box-shadow: 7px 0px 15px #e5d64b, -7px 0px 15px #faf9f9; +} + +h1 { + text-align: center; + margin-bottom: 20px; + color: rgb(244, 152, 81); + width: 200%; + margin-left: -200px; + font-size: 50px; +} + +.input-group, .output-group { + margin-bottom: 15px; +} + +label { + display: block; + margin-bottom: 5px; +} + +input[type="text"], input[type="number"], select { + width: calc(100% - 22px); + padding: 10px; + border: 1px solid #ccc; + border-radius: 4px; +} + +.buttons { + display: flex; + justify-content: center; + gap: 10px; +} + +.btn { + padding: 10px 20px; + background-color: #f6f8fa; + color: rgb(244, 152, 81); + border: none; + border-radius: 7px; + cursor: pointer; + box-shadow: 7px 0px 15px #e5d64b, -7px 0px 15px #e5d64b; + height:50%; + width: 50%; + font-size: 20px; + font-weight: bolder; +} + +.btn:hover { + background-color: #f7f1f3; +} + +#result, #roots { + width: calc(100% - 22px); + padding: 10px; + border: 1px solid #ccc; + border-radius: 4px; + background-color: #e9ecef; +} diff --git a/index.html b/index.html index f85fd6313..60f0488da 100644 --- a/index.html +++ b/index.html @@ -1968,6 +1968,20 @@

Calculates various parameters of planetary motion using Kepler's laws.

+
+
+

Polynomial Roots Calculator

+

Calculates the real and imaginary roots of a given polynomial up to degree 2-4.

+ +
+

Population Density Calculator