# Solving equations¶

To solve an equation like $$x^2 - 4 = 45$$ is to find value(s) of the unknown variable $$x$$ for which the equation is true.

To find $$x$$, we can manipulate the original equation, transforming it into a different equation (as true as the first) that looks like $$x = \textrm{only numbers}$$.

The only requirement is that the manipulations we make transform one true equation into another true equation. We can do this by applying the same operation on both sides of the equation.

## Notebooks setup¶

# import sympy
from sympy import *
from sympy.abc import *

# import and apply mathcube7's hack for
# auto-broadcasting operations to Eq.lhs and Eq.rhs
from customizer import *


## Example 1¶

Let’s see the similification steps that allow us to solve the equation $$x^2 - 4 = 45$$.

We start by defining the equaiton using the Eq class:

eqn1 = Eq(x**2 - 4, 45)
eqn1

$\displaystyle x^{2} - 4 = 45$

Add 4 to both sides of the equation:

eqn2 = eqn1 + 4
eqn2

$\displaystyle x^{2} = 49$

To “undo” the x-squared operation, we’ll apply the square root function, which is inverse function of $$x^2$$:

eqn3 = sqrt(eqn2)
eqn3

$\displaystyle \sqrt{x^{2}} = 7$

The combination of squaring a number then taking the square root is equivalent to the absolute value function, which erases the sign of the number. The solutions to the equation whose absolute value is $$7$$.

The equation has two solutions: $$x=7$$ and $$x=-7$$.

### Direct solution using solve¶

Note we could jump straight to the solution instead of manually doing the steps by calling the solve function:

solve(eqn1)

[-7, 7]


Another approach is to use numerical solution (necessary for complicated expressions where no analytical solution is possible):

# start the solution algorithm near the value x=1 and look for a solution nearby
nsolve(eqn1, 1)

$\displaystyle 7.0$
# same thing as above, but we start near x=-1 in order to find the other solution
nsolve(eqn1, -1)

$\displaystyle -7.0$