Quadratic Equation Solver
function calculateQuadraticRoots() {
var a = parseFloat(document.getElementById('coefficientA').value);
var b = parseFloat(document.getElementById('coefficientB').value);
var c = parseFloat(document.getElementById('coefficientC').value);
var resultDiv = document.getElementById('quadraticResult');
resultDiv.innerHTML = "; // Clear previous results
if (isNaN(a) || isNaN(b) || isNaN(c)) {
resultDiv.innerHTML = 'Please enter valid numbers for all coefficients.';
return;
}
if (a === 0) {
if (b === 0) {
if (c === 0) {
resultDiv.innerHTML = 'This is the trivial equation 0=0, which has infinite solutions.';
} else {
resultDiv.innerHTML = 'This is the equation ' + c + '=0, which has no solution.';
}
} else {
// Linear equation: bx + c = 0
var x = -c / b;
resultDiv.innerHTML = 'This is a linear equation (a=0). The solution is:
x = ' + x.toFixed(4) + '';
}
return;
}
var discriminant = b * b – 4 * a * c;
if (discriminant > 0) {
var x1 = (-b + Math.sqrt(discriminant)) / (2 * a);
var x2 = (-b – Math.sqrt(discriminant)) / (2 * a);
resultDiv.innerHTML = 'The equation has two distinct real roots:' +
'
x₁ = ' + x1.toFixed(4) + '' +
'
x₂ = ' + x2.toFixed(4) + '';
} else if (discriminant === 0) {
var x = -b / (2 * a);
resultDiv.innerHTML = 'The equation has one real root (a repeated root):' +
'
x = ' + x.toFixed(4) + '';
} else {
// discriminant < 0, complex roots
var realPart = -b / (2 * a);
var imaginaryPart = Math.sqrt(Math.abs(discriminant)) / (2 * a);
resultDiv.innerHTML = 'The equation has two complex conjugate roots:' +
'
x₁ = ' + realPart.toFixed(4) + ' + ' + imaginaryPart.toFixed(4) + 'i' +
'
x₂ = ' + realPart.toFixed(4) + ' – ' + imaginaryPart.toFixed(4) + 'i';
}
}
.calculator-container {
background-color: #f9f9f9;
border: 1px solid #ddd;
padding: 20px;
border-radius: 8px;
max-width: 600px;
margin: 20px auto;
font-family: Arial, sans-serif;
}
.calculator-container h2 {
text-align: center;
color: #333;
margin-bottom: 20px;
}
.calculator-inputs .form-group {
margin-bottom: 15px;
}
.calculator-inputs label {
display: block;
margin-bottom: 5px;
color: #555;
}
.calculator-inputs input[type="number"] {
width: calc(100% – 22px);
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
.calculate-button {
display: block;
width: 100%;
padding: 12px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.calculate-button:hover {
background-color: #0056b3;
}
.calculator-result {
margin-top: 20px;
padding: 15px;
border: 1px solid #e0e0e0;
border-radius: 4px;
background-color: #e9ecef;
color: #333;
}
.calculator-result p {
margin: 5px 0;
line-height: 1.5;
}
.calculator-result strong {
color: #0056b3;
}
.calculator-result .error {
color: #dc3545;
font-weight: bold;
}
Understanding Quadratic Equations and Their Solutions
A quadratic equation is a polynomial equation of the second degree, meaning it contains at least one term in which the unknown variable is raised to the power of two. The standard form of a quadratic equation is:
ax² + bx + c = 0
Where:
x represents the unknown variable.
a, b, and c are coefficients, with a not equal to zero. If a were zero, the equation would become a linear equation (bx + c = 0).
The Quadratic Formula
The most common method to find the solutions (also known as roots) of a quadratic equation is by using the quadratic formula. This formula provides the values of x that satisfy the equation:
x = [-b ± sqrt(b² - 4ac)] / 2a
This formula can yield two distinct real roots, one repeated real root, or two complex conjugate roots, depending on the value of the discriminant.
The Discriminant (Δ)
The term inside the square root, b² - 4ac, is called the discriminant, often denoted by the Greek letter delta (Δ). The value of the discriminant determines the nature of the roots:
- If Δ > 0 (Discriminant is positive): The equation has two distinct real roots. This means there are two different numerical values for
x that will make the equation true.
- If Δ = 0 (Discriminant is zero): The equation has exactly one real root (sometimes called a repeated or double root). Both solutions from the quadratic formula simplify to the same value.
- If Δ < 0 (Discriminant is negative): The equation has two complex conjugate roots. These roots involve the imaginary unit
i (where i² = -1) and appear as a pair (p + qi) and (p - qi).
How to Use the Calculator
To use the Quadratic Equation Solver above, simply input the coefficients a, b, and c from your quadratic equation ax² + bx + c = 0 into the respective fields. For example:
- For the equation
x² - 5x + 6 = 0, you would enter a=1, b=-5, and c=6. The calculator will output the roots x₁ = 3 and x₂ = 2.
- For the equation
x² - 4x + 4 = 0, you would enter a=1, b=-4, and c=4. The calculator will output one real root x = 2.
- For the equation
x² + 2x + 5 = 0, you would enter a=1, b=2, and c=5. The calculator will output complex roots x₁ = -1 + 2i and x₂ = -1 - 2i.
Click the "Calculate Roots" button, and the calculator will instantly display the solutions, indicating whether they are real or complex.