Skip to content

Commit

Permalink
Added Polynomial Roots Calculator (#1170)
Browse files Browse the repository at this point in the history
  • Loading branch information
DiwareNamrata23 authored Jun 5, 2024
1 parent 4fcd179 commit 550be82
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Calculators/Polynomial-Roots-Calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# <p align="center">Polynomial Roots Calculator</p>

## 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)
1 change: 1 addition & 0 deletions Calculators/Polynomial-Roots-Calculator/assets/boy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions Calculators/Polynomial-Roots-Calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/aos/2.3.4/aos.css">
<link rel="stylesheet" href="style.css">
<title>Polynomial Roots Calculator</title>
</head>
<body>
<div class="hero-section">
<div class="calculator" >
<h1 data-aos="fade-right">Roots Calculator</h1>
<div class="input-group" >
<label for="degree">Degree of Polynomial:</label>
<select id="degree" onchange="showCoefficientInputs()" style="width: 25rem;">
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
<div id="coefficients">
<div class="input-group" >
<label for="a">Coefficient a:</label>
<input type="number" id="a" placeholder="e.g., 3">
</div>
<div class="input-group">
<label for="b">Coefficient b:</label>
<input type="number" id="b" placeholder="e.g., 2">
</div>
<div class="input-group">
<label for="c">Coefficient c:</label>
<input type="number" id="c" placeholder="e.g., 1">
</div>
<div class="input-group" id="d-group" style="display: none;">
<label for="d">Coefficient d:</label>
<input type="number" id="d" placeholder="e.g., 0">
</div>
<div class="input-group" id="e-group" style="display: none;">
<label for="e">Coefficient e:</label>
<input type="number" id="e" placeholder="e.g., 0">
</div>
</div>
<div class="input-group">
<label for="xValue">Value of x:</label>
<input type="number" id="xValue" placeholder="e.g., 2">
</div>
<div class="buttons" data-aos="fade-right">
<button class="btn" onclick="findRoots()">Find Roots</button>
</div>
<div class="output-group">
<label for="roots">Roots:</label>
<input type="text" id="roots" disabled>
</div>
</div>
</div>
<!-- AOS JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/aos/2.3.4/aos.js"></script>
<script>
// Initialize AOS
AOS.init();
</script>
<script src="script.js"></script>
</body>
</html>
125 changes: 125 additions & 0 deletions Calculators/Polynomial-Roots-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -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];
}
78 changes: 78 additions & 0 deletions Calculators/Polynomial-Roots-Calculator/style.css
Original file line number Diff line number Diff line change
@@ -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;
}
14 changes: 14 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1968,6 +1968,20 @@ <h3>Calculates various parameters of planetary motion using Kepler's laws.</h3>
</div>
</div>
</div>
<div class="box">
<div class="content">
<h2>Polynomial Roots Calculator</h2>
<h3>Calculates the real and imaginary roots of a given polynomial up to degree 2-4.</h3>
<div class="card-footer">
<a href="./Calculators/Polynomial-Roots-Calculator/" target="_blank">
<button>Try Now</button>
</a>
<a href="https://github.com/Rakesh9100/CalcDiverse/tree/main/Calculators/Polynomial-Roots-Calculator" title="Source Code" target="_blank">
<img src="./assets/images/github.png" alt="Source Code"></img>
</a>
</div>
</div>
</div>
<div class="box">
<div class="content">
<h2>Population Density Calculator</h2>
Expand Down

0 comments on commit 550be82

Please sign in to comment.