Symbolic and Numerical Equation Solving
Find solutions to equations symbolically and numerically. Supports linear, quadratic, polynomial, and transcendental equations with automatic strategy selection. Includes symbolic solving, numerical fallback, and parametric solutions.
Code Examples
Linear Equations
Solve linear equations ax + b = 0
use mathhook::prelude::*;
let x = symbol!(x);
// Solve: 2x + 3 = 0
let eq1 = expr!(2 * x + 3);
let mut solver = MathSolver::new();
let sol1 = solver.solve(&eq1, &x);
// Result: x = -3/2
// Solve: 5x - 10 = 0
let eq2 = expr!(5 * x - 10);
let sol2 = solver.solve(&eq2, &x);
// Result: x = 2
Quadratic Equations
Solve quadratic equations using quadratic formula
use mathhook::prelude::*;
let x = symbol!(x);
// Solve: x² - 5x + 6 = 0
let eq1 = expr!(x ^ 2 - 5 * x + 6);
let mut solver = MathSolver::new();
let solutions = solver.solve(&eq1, &x);
// Result: [x = 2, x = 3]
// Solve: x² - 4 = 0 (difference of squares)
let eq2 = expr!(x ^ 2 - 4);
let sol2 = solver.solve(&eq2, &x);
// Result: [x = -2, x = 2]
// Complex roots: x² + 1 = 0
let eq3 = expr!(x ^ 2 + 1);
let sol3 = solver.solve(&eq3, &x);
// Result: [x = i, x = -i]
Polynomial Equations
Solve polynomial equations via factorization
use mathhook::prelude::*;
let x = symbol!(x);
// Solve: x³ - 6x² + 11x - 6 = 0
// Factors: (x - 1)(x - 2)(x - 3)
let cubic = expr!(x ^ 3 - 6 * (x ^ 2) + 11 * x - 6);
let mut solver = MathSolver::new();
let solutions = solver.solve(&cubic, &x);
// Result: [x = 1, x = 2, x = 3]
// Solve: x⁴ - 1 = 0
let quartic = expr!(x ^ 4 - 1);
let sol2 = solver.solve(&quartic, &x);
// Result: [x = 1, x = -1, x = i, x = -i]
Transcendental Equations
Solve equations with trigonometric, exponential functions
use mathhook::prelude::*;
let x = symbol!(x);
// Solve: sin(x) = 0
let eq1 = expr!(sin(x));
let mut solver = MathSolver::new();
let solutions = solver.solve(&eq1, &x);
// Result: [x = 0, x = π, x = 2π, ...] (periodic)
// Solve: e^x = 5
let eq2 = expr!(exp(x) - 5);
let sol2 = solver.solve(&eq2, &x);
// Result: x = ln(5)
// Numerical fallback: x = cos(x)
let eq3 = expr!(x - cos(x));
let mut solver = MathSolver::new()
.with_numerical_fallback(true)
.with_tolerance(1e-10);
let sol3 = solver.solve(&eq3, &x);
// Result: x ≈ 0.739085133...
Real-World Application: Projectile Motion
Physics application finding time to hit ground
use mathhook::prelude::*;
let t = symbol!(t);
let v0 = symbol!(v0); // Initial velocity
let h = symbol!(h); // Initial height
// Position: y(t) = -16t² + v₀t + h
let position = expr!(-16 * (t ^ 2) + v0 * t + h);
// Substitute values: v₀ = 64 ft/s, h = 80 ft
let position_vals = position.substitute(&v0, &expr!(64))
.substitute(&h, &expr!(80));
// Find time when projectile hits ground (y = 0)
let mut solver = MathSolver::new();
let times = solver.solve(&position_vals, &t);
// Result: t ≈ 5 seconds (ignoring negative solution)
Parametric Solutions
Solve in terms of parameters
use mathhook::prelude::*;
let x = symbol!(x);
let a = symbol!(a);
let b = symbol!(b);
// Solve: ax + b = 0 (parametric in a, b)
let equation = expr!(a * x + b);
let mut solver = MathSolver::new();
let solution = solver.solve(&equation, &x);
// Result: x = -b/a (symbolic solution)
// Now substitute specific values
let specific = solution.substitute(&a, &expr!(2))
.substitute(&b, &expr!(6));
// Result: x = -3