# Solving equations by addition

Let's solve a linear equation using addition.

**Note**: Lesson inspired by https://www.basic-mathematics.com/solving-equations-using-addition.html

Here we import a few functions from SymPy. Each of these functions will be explained.


```python
from sympy import symbols, Eq, simplify, solve
```

## Overview

Given we have an equation

$$x + -b = c$$

we want to solve for the value of $x$.

We need to "move" the $b$ value over to the other side so we can have $x$ by itself.

Notice the negative or minus sign in front of $b$. We can add a postive version of the negative $b$ to move it over to the other side.

$$x + -b + b = c + b$$

Once we do this, the negative and positive $b$ values will cancel out to zero.

$$x + 0 = c + b$$

This will simplify to having $x$ on one side like we want it to.

$$x = c + b$$

## Setup

The above uses variables instead of numbers. Let's play around with this ourselves with Python and SymPy using real numbers.

Let's say we want to solve for $x$ in the following equation.

$$x + (-2) = 8$$

First we need to create our $x$ variable for our equation using the `symbols()` function.


```python
x = symbols('x')
```

We can write Python code very similar to our equation above.

Let us define our equation. This can be done using the `Eq()` function.

The first and second arguments for this function are the left and right sides of the equal side, respectively.


```python
eq1 = Eq(x + (-2), 8)
eq1
```




$\displaystyle x - 2 = 8$



You may notice the left side of our equation looks a bit different from above, which was $x + (-2)$.

These two expressions are equal and Python can check this for us.


```python
x + (-2) == x - 2
```




    True



## Using Python and SymPy to Help Us

Another great thing about Python and SymPy is that we can solve our equation using the `solve()` function. Why show this? Because we can have an end point we can go towards to check our math work immediately.

The `solve()` function takes two parameters, the first one is the equation and the second one is variable symbol you want to solve for.


```python
solve(eq1, x)
```




    [10]



This tells us that solving for $x$ in our equation $x + (-2) = 8$ requires $x$ to be $10$, or $x = 10$.

Let's use math to double check this.

## Adding on both sides

Based on our Overview above, we want to add the positive version of our values on both sides of the equation.

We can access the left hand side and the right hand side using the methods `.lhs` and `rhs` on our equation object, respectively.


```python
# Left side of the equals
eq1.lhs
```




$\displaystyle x - 2$




```python
# Right side of the equals
eq1.rhs
```




$\displaystyle 8$



Now that we have the left and right sides, let's add the positive version of 2 because we are subtracting from it.


```python
eq1 = Eq(eq1.lhs + 2, eq1.rhs + 2)
eq1
```




$\displaystyle x = 10$



## Exercise

Solve the following equation using addition.

$$x + (-6) = 5$$

**Hint**: Start by translating the math equation in to a SymPy equation using the `Eq()` function.


```python

```

5da8828e61eda631945b9d40ca311670a61f4933
ipynb
Jupyter Notebook
algebra/solve_equations_by_addition.ipynb
erictleung/machine-learning
9467e9dc951697a962062b2c1c1caa225cc65bb8
MIT
2021-02-04T20:16:35.000Z
2021-06-15T20:02:23.000Z
algebra/solve_equations_by_addition.ipynb
erictleung/data-science
9467e9dc951697a962062b2c1c1caa225cc65bb8
MIT
2017-06-10T18:14:31.000Z
2017-11-01T02:32:51.000Z
algebra/solve_equations_by_addition.ipynb
erictleung/machine-learning
9467e9dc951697a962062b2c1c1caa225cc65bb8
MIT
2021-02-04T20:16:37.000Z
2021-03-13T15:03:09.000Z
Qwen/Qwen-72B
1. YES
2. YES