';
} else {
return '
`;
}
function calculateSubstitution() {
// 1. Get coefficients
const a1 = parseFloat(document.getElementById('a1').value);
const b1 = parseFloat(document.getElementById('b1').value);
const c1 = parseFloat(document.getElementById('c1').value);
const a2 = parseFloat(document.getElementById('a2').value);
const b2 = parseFloat(document.getElementById('b2').value);
const c2 = parseFloat(document.getElementById('c2').value);
// 2. Get precision
const precisionInput = document.getElementById('precision');
const precision = parseInt(precisionInput.value);
precisionInput.value = (isNaN(precision) || precision < 0) ? 3 : precision;
const currentPrecision = parseInt(precisionInput.value);
const solutionResult = document.getElementById('solutionResult');
const stepByStepContent = document.getElementById('stepByStepContent');
// 3. Initial Validation
if (isNaN(a1) || isNaN(b1) || isNaN(c1) || isNaN(a2) || isNaN(b2) || isNaN(c2)) {
solutionResult.innerHTML = `
Enter coefficients to solve the system
`;
stepByStepContent.innerHTML = '';
return;
}
const fn = (n) => formatNum(n, currentPrecision);
// 4. Calculation (using determinants/Cramer's rule derivation for robust solution)
const determinant = a1 * b2 - a2 * b1;
let resultHTML = '';
let stepsHTML = '';
let x, y;
if (Math.abs(determinant) < 1e-9) { // Determinant is close to zero
// Check for consistent (infinite solutions) or inconsistent (no solution)
const detC1 = c1 * b2 - c2 * b1;
const detC2 = a1 * c2 - a2 * c1;
if (Math.abs(detC1) < 1e-9 && Math.abs(detC2) < 1e-9) {
resultHTML = displayResult('Solution', 'Infinitely many solutions (dependent system)');
} else {
resultHTML = displayResult('Solution', 'No solution (inconsistent system)');
}
stepsHTML = generateSteps(a1, b1, c1, a2, b2, c2, 0, 0, currentPrecision, determinant);
} else {
// Unique Solution
x = (c1 * b2 - c2 * b1) / determinant;
y = (a1 * c2 - a2 * c1) / determinant;
resultHTML = displayResult('Solution', `x = ${fn(x)}, y = ${fn(y)}`);
stepsHTML = generateSteps(a1, b1, c1, a2, b2, c2, x, y, currentPrecision, determinant);
}
// 5. Display results
solutionResult.innerHTML = resultHTML;
stepByStepContent.innerHTML = stepsHTML;
// 6. Rerender MathJax to display the LaTeX equations properly
if (window.MathJax) {
window.MathJax.Hub.Queue(["Typeset", window.MathJax.Hub, stepByStepContent]);
}
}
function clearAll() {
document.getElementById('a1').value = '';
document.getElementById('b1').value = '';
document.getElementById('c1').value = '';
document.getElementById('a2').value = '';
document.getElementById('b2').value = '';
document.getElementById('c2').value = '';
document.getElementById('precision').value = '3';
// Reset results display
document.getElementById('solutionResult').innerHTML = `
Enter coefficients to solve the system
`;
document.getElementById('stepByStepContent').innerHTML = '';
}
// Initialize the calculator in a clean state on load.
window.onload = function() {
// Call clearAll to ensure fields are empty and the initial empty state message is displayed.
clearAll();
};
The determinant is 0, but the equations are not proportional. **No solution** exists (Inconsistent System).
'; } } let steps = '- ';
const originalEq1 = formatEquationLine(a1, 'x', b1, 'y', c1, precision);
const originalEq2 = formatEquationLine(a2, 'x', b2, 'y', c2, precision);
// --- Determine the easiest variable to solve for from Eq 1 (x or y) ---
let solveVar = 'x'; // Default to solving for x in Eq 1
let a = a1, b = b1, c = c1;
// Prioritize solving for y in Eq 1 if b1 is simpler than a1
if (Math.abs(b1) > 0 && (Math.abs(b1) <= Math.abs(a1) || Math.abs(a1) < 1e-9)) {
solveVar = 'y';
a = b1; b = a1; // Swap coefficients for generic math steps
}
const otherVar = (solveVar === 'x') ? 'y' : 'x';
// --- Step 1: Isolate the chosen variable (e.g., x) from Equation 1 ---
// Step 1.1: Move the other variable term (e.g., y term)
// a * (solveVar) = c - b * (otherVar)
const signB = b > 0 ? ' - ' : ' + ';
let step1_1_eq = `\$${fn(a)}${solveVar} = ${fn(c)}${signB}${fn(Math.abs(b))}${otherVar}\$`;
steps += `
-
Step 1: Solve Equation 1 for ${solveVar}Current equations:
$1: ${originalEq1}$
$2: ${originalEq2}$First, subtract ${fn(Math.abs(b))}${otherVar} from both sides of the First equation:`; // Step 1.2: Divide by a // solveVar = (c - b*otherVar) / a const c_over_a = c / a; const b_over_a = b / a; const signB_over_A = b_over_a > 0 ? ' - ' : ' + '; const coeffB_over_A = fn(Math.abs(b_over_a)); steps += `
${step1_1_eq}Next, divide both sides by ${fn(a)}:
$${solveVar} = \\frac{${fn(c)} ${b < 0 ? ' + ' : ' - '}${fn(Math.abs(b))}${otherVar}}{${fn(a)}}$
$${solveVar} = ${fn(c_over_a)}${signB_over_A}${coeffB_over_A}${otherVar}$
`;
// --- Step 2: Substitute the expression into Equation 2 ---
let a_sub = a2, b_sub = b2, c_sub = c2;
if (solveVar === 'y') {
a_sub = b2; b_sub = a2; // Switch roles in Eq 2 if we are substituting y
}
const other_coeff = (solveVar === 'x') ? b2 : a2; // The coefficient of the remaining variable in Eq 2
// Substitute expression
const sub_term_coeff = (Math.abs(a_sub) === 1) ? '' : fn(a_sub);
const other_term_coeff = (Math.abs(other_coeff) === 1) ? '' : fn(other_coeff);
let substitution_eq = '';
// Term 1: a_sub * (expression)
substitution_eq += `${sub_term_coeff}\\left(${fn(c_over_a)}${signB_over_A}${coeffB_over_A}${otherVar}\\right)`;
// Term 2: other_coeff * otherVar
if (Math.abs(other_coeff) > 1e-9) {
let sign = (other_coeff > 0) ? ' + ' : ' - ';
substitution_eq += `${sign}${other_term_coeff}${otherVar}`;
}
substitution_eq = `\$${substitution_eq} = ${fn(c_sub)}\$`;
steps += `
-
Step 2: Substitute the expression for ${solveVar} into the Second equation${substitution_eq}
`;
// --- Step 3: Simplify and Solve for the remaining variable (otherVar) ---
const const_term = a_sub * c_over_a;
const other_var_from_sub = a_sub * (-b_over_a);
const other_var_combined_coeff = other_coeff + other_var_from_sub;
const new_const_term = c_sub - const_term;
steps += `
-
Step 3: Simplify and solve for ${otherVar}Simplify (Distribute and combine constants):
$${fn(const_term)} ${other_var_combined_coeff > 0 ? ' + ' : ' - '}${fn(Math.abs(other_var_combined_coeff))}${otherVar} = ${fn(c_sub)}$Move constants to the right side:
${other_var_combined_coeff > 0 ? '' : '-'}${fn(Math.abs(other_var_combined_coeff))}${otherVar} = ${fn(new_const_term)}Divide to solve for ${otherVar}:
$${otherVar} = \\frac{${fn(new_const_term)}}{${fn(other_var_combined_coeff)}} = ${fn(solveVar === 'x' ? y : x)}$$\mathbf{${otherVar} = ${fn(solveVar === 'x' ? y : x)}}$
`;
// --- Step 4: Substitute the found value back in to find the first variable (solveVar) ---
const final_val = solveVar === 'x' ? x : y;
const final_other_val = solveVar === 'x' ? y : x;
steps += `
-
Step 4: Substitute ${otherVar} value back to find ${solveVar}Substitute $${otherVar} = ${fn(final_other_val)}$ into $${solveVar} = ${fn(c_over_a)}${signB_over_A}${coeffB_over_A}${otherVar}$:
$${solveVar} = ${fn(c_over_a)}${signB_over_A}${coeffB_over_A}(${fn(final_other_val)})$
$${solveVar} = ${fn(final_val)}$$\mathbf{${solveVar} = ${fn(final_val)}}$
`;
steps += '
${label}:
${value}
Enter coefficients to solve the system
Enter coefficients to solve the system




