> For the complete documentation index, see [llms.txt](https://jona-projects.gitbook.io/kiwicalc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jona-projects.gitbook.io/kiwicalc/equations/linear-equations.md).

# Linear Equations

#### Linear Equations

The quickest way to solve a linear equation is to use the `solve_linear` method. The method accepts a string that represents the equation, and optionally also a collection of the variables\_dict in the equation. For example:

```python
from kiwicalc import solve_linear
linear_equation = "3x + 7 = -x -5"
result = solve_linear(linear_equation)
print(result)         
```

If you want to speed up this method, specify the variables that appear in it:

```python
result = solve_linear(linear_equation, ('x',))
```

You can also use the `LinearEquation` class, which offers more features.

**class `LinearEquation`**

```python
class LinearEquation(Equation):
```

Properties

* `equation` - the string that represents the linear equation
* `variables` - a list of the variables that appear in the equation
* `num_of_variables` - the number of variables that appear in the equation
* `first_side` - a string that represents the first side of the equation
* `second_side` - a string that represents the second side of the equation
* `solution` - the solution of the equation
* `variables_dict` - An internal dictionary that represents the coefficients of the variables in the equation, when all moved to the first side.

**Create a new `LinearEquation` object**

```python
def __init__(self, equation: str, variables: Iterable = None, calc_now: bool = False):
```

You have to enter the equation as a string. For instance: `"3x+5 = 8x-4"`. In addition, you can specify the variables that appear in the equation, so we won't have to figure it out ourselves. This results in a significant runtime improvement (even though it's pretty fast regardless). You can also set the `calc_now` parameter to `True` if you want that the equation will be solved when the `LinearEquation` object is created. For instance:

```python
my_equation = LinearEquation("3x + 5 = 8", variables=('x',))                  
```

**Fetching the solutions of the equation**

You can either get the solutions with the `solve()` method or the `solution` property. For example:

```python
my_equation = LinearEquation("3x + 5 = 8", variables=('x',))
print(my_equation.solution)
print(my_equation.solve())           
```

**Solve while printing the steps to the solution**

```python
my_equation = LinearEquation("3x - 7 + 2x + 6 = 4x - 4 + 6x")
print(my_equation.show_steps())
```

Output:

<figure><img src="/files/9cBRWWSSv3fJLKxhql7y" alt=""><figcaption></figcaption></figure>

yet well optimizedThis method is not well optimized yet, but it works pretty well!

**Simplifying the equation**

You can simplify the linear equation (without necessarily solving it) via the `simplify()` method. For instance:

```python
my_equation = LinearEquation("4 + 3x + 2y + 5x + 6 = 2 + x - y", variables=('x', 'y'))
my_equation.simplify()
print(my_equation)
```

**Plot the solution**

If the equation indeed has a solution, you can plot it as the intersection of two linear functions. Here is the signature of the method:

```python
 def plot_solution(self, start: float = -10, stop: float = 10, step: float = 0.01, ymin: float = -10,
      ymax: float = 10, show_axis=True, show=True, title: str = None, with_legend=True):            
```

<pre class="language-python"><code class="lang-python">  my_equation = LinearEquation("3x - 7 + 2x + 6 = 4x - 4 + 6x")
<strong>     print(my_equation.plot_solution())             
</strong></code></pre>

<figure><img src="/files/aY2s6POA1nQGLyZsh4j4" alt=""><figcaption></figcaption></figure>
