Solving Equations
Prerequisites: Symbolic algebra
Equations use ==. Solutions come back as lists of rules.
Solve
Solve[x^2 - 5 x + 6 == 0, x]
Solve[x^2 == 2, x]
Solve[a x + b == 0, x]
Solve[{x + y == 10, x - y == 2}, {x, y}]
{{x -> 2}, {x -> 3}}
{{x -> -Sqrt[2]}, {x -> Sqrt[2]}}
{{x -> -(b/a)}}
{{x -> 6, y -> 4}}
- Each inner list is one solution.
- Solutions are exact, and symbolic parameters like
aandbare fine.
Choosing the domain
Solve[x^2 + 1 == 0, x]
Solve[x^2 + 1 == 0, x, Reals]
Reduce[x^2 < 4, x]
{{x -> -I}, {x -> I}}
{}
-2 < x < 2
- By default,
Solvefinds complex solutions. AddRealsorIntegersto restrict them —{}means "no solutions". Reducehandles inequalities and describes all solutions, including special cases.
Numerical solutions
NSolve[x^3 - 2 x - 5 == 0, x, Reals]
FindRoot[Cos[x] == x, {x, 1}]
{{x -> 2.09455}}
{x -> 0.739085}
NSolvefinds all numeric solutions of polynomial equations.FindRootsearches near a starting point (x = 1here) and works for any equation — but finds only one root.
Differential equations
DSolve[y'[x] == y[x], y[x], x]
DSolve[{y'[x] == y[x], y[0] == 2}, y[x], x]
{{y[x] -> E^x C[1]}}
{{y[x] -> 2 E^x}}
C[1] is an arbitrary constant (notebooks display it as a subscripted
c₁). An initial condition fixes it.
For equations without a formula solution, NDSolve returns an
interpolating function you can evaluate and plot:
sol = NDSolve[{y'[x] == -y[x], y[0] == 1}, y, {x, 0, 5}];
y[1] /. sol
{0.367879}
That's e⁻¹, as expected. Plot[y[x] /. sol, {x, 0, 5}] draws the
solution.
Common mistakes
Solve[x^2 = 4, x]— a single=assigns4tox^2and fails with a message. Equations need==.- Using a variable that already has a value: if
x = 3, thenSolve[x^2 == 4, x]complains that3is not a valid variable.Clear[x]first.
Practice
Find where the line y = 2 x + 1 meets the parabola y = x^2 - 2.
Show solution
Solve[{y == 2 x + 1, y == x^2 - 2}, {x, y}]
{{x -> -1, y -> -1}, {x -> 3, y -> 7}}