Equation Solver & Plotter
.add('show');
};
// Solve Quadratic Equation
window.eqSolveQuadratic = function() {
const input = document.getElementById('eqQuadraticInput');
const resultDiv = document.getElementById('eqQuadraticResult');
if (!input || !resultDiv) return;
const equation = input.value.trim();
if (!equation) {
showError(resultDiv, 'Please enter an equation');
return;
}
try {
const parsed = parseEquation(equation);
if (!parsed) throw new Error('Invalid equation format');
const coeffs = extractQuadraticCoeffs(parsed.left, parsed.right);
if (coeffs.a === 0) {
throw new Error('Not a quadratic equation (a = 0)');
}
const discriminant = coeffs.b * coeffs.b - 4 * coeffs.a * coeffs.c;
let solutions = [];
if (discriminant > 0) {
const x1 = (-coeffs.b + Math.sqrt(discriminant)) / (2 * coeffs.a);
const x2 = (-coeffs.b - Math.sqrt(discriminant)) / (2 * coeffs.a);
solutions = [x1, x2];
} else if (discriminant === 0) {
const x = -coeffs.b / (2 * coeffs.a);
solutions = [x];
}
displayQuadraticSolution(resultDiv, solutions, coeffs, discriminant, equation);
} catch (error) {
showError(resultDiv, error.message);
}
};
function extractQuadraticCoeffs(left, right) {
const a = getCoefficient(left, 'x^2') + getCoefficient(left, 'x²') - getCoefficient(right, 'x^2') - getCoefficient(right, 'x²');
const b = getCoefficient(left, 'x') - getCoefficient(right, 'x');
const c = getConstant(left) - getConstant(right);
return { a, b, c };
}
function displayQuadraticSolution(resultDiv, solutions, coeffs, discriminant, original) {
let solutionHTML = '';
if (discriminant > 0) {
solutionHTML = `
Two Real Solutions:
x₁ = ${solutions[0].toFixed(4)}
x₂ = ${solutions[1].toFixed(4)}
`;
} else if (discriminant === 0) {
solutionHTML = `
One Real Solution (Double Root):
x = ${solutions[0].toFixed(4)}
`;
} else {
const realPart = -coeffs.b / (2 * coeffs.a);
const imagPart = Math.sqrt(-discriminant) / (2 * coeffs.a);
solutionHTML = `
Two Complex Solutions:
x₁ = ${realPart.toFixed(4)} + ${imagPart.toFixed(4)}i
x₂ = ${realPart.toFixed(4)} - ${imagPart.toFixed(4)}i
`;
}
resultDiv.innerHTML = `
✨ Solution
${solutionHTML}
Step-by-Step Solution:
Step 1:
Original: ${original}
Step 2:
Standard form: ${coeffs.a}x² ${coeffs.b >= 0 ? '+' : ''}${coeffs.b}x ${coeffs.c >= 0 ? '+' : ''}${coeffs.c} = 0
Step 3:
Discriminant: Δ = b² - 4ac = ${discriminant.toFixed(4)}
Step 4:
${discriminant > 0 ? 'Δ > 0: Two real solutions' : discriminant === 0 ? 'Δ = 0: One real solution' : 'Δ < 0: Two complex solutions'}
${discriminant >= 0 ? `
Step 5:
Using quadratic formula: x = (-b ± √Δ) / 2a
` : ''}
`;
resultDiv.classList