> 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/quadratic-equations.md).

# Quadratic Equations

#### Quadratic Equations

In order to solve quadratic equations you can use the `solve_quadratic()` method. The method accepts 3 parameters: \`a\`, \`b\`, and \`c\` which represent the coefficients in the formula: \`ax^2 + bx +c\`. The method will compute 2 solutions, whether they are real or complex. If you are only interested in the real solutions, you can use the `solve_quadratic_real()` method. Of course, the method internally uses the known quadratic formula: \`x = (-b +- sqrt(b^2-4ac))/(2a) .\`\
For example, lets solve the equation \`x^2 + 6x + 8\` and \`x^2 + x + 2\` with both of the methods

```python
                        
solutions = solve_quadratic(1,6,8)
real_solutions = solve_quadratic_real(1,6,8)
print(solutions)
print(real_solutions)
                        
                    
```

And now lets solve the equation \`x^2 + x + 1\` with both of the methods:

```python
                        
solutions2 = solve_quadratic(1,1,2)
real_solutions2 = solve_quadratic_real(1,1,2)
print(solutions2)
print(real_solutions2)
                        
                    
```

You can also solve quadratic equations with parameters via the `solve_quadratic_params`. For instance:

```python
                        a, b, c = Var('a'), Var('b'), Var('c')
results = solve_quadratic_params(a, b, c)
print(result[0])
print(result[1])
                    
```

It's also possible to use the `QuadraticEquation` class for a wider variety of operations on quadratic equations.

**class `QuadraticEquation`**

```python
 class QuadraticEquation(Equation):
                    
```

**Properties**

The `QuadraticEquation` class has the same properties as the `LinearEquation` class, since both of them inherit from the `Equation` class.

**create a new `QuadraticEquation` object**

You can create a new `QuadraticEquation` object by entering a string that represents the equation, and optionally you can also enter variables that appear in the equation. For instance:

```python
my_equation = QuadraticEquation("x^2 + 6x + 8 = 0")
                    
```

**Solving the equation**

You can solve the quadratic equation with `solve()` method. Here is the signature of the method:

```python
 def solve(self, mode='complex'):
                    
```

You can choose how to solve the quadratic equation via the `mode` parameter:

* `'complex'` - solve for all the solutions, including complex solutions
* `'real'` - solve only for the real solutions

For instance:

```python
my_equation = QuadraticEquation("x^2 + 6x + 8 = 0")
solution = my_equation.solve()
print(solution)
                    
```

```python
my_equation = QuadraticEquation("x^2 + 6x + 8 = 0")
solution = my_equation.solve('real')
print(solution)
                    
```

**Get a simplified equation**

You can simplify quadratic expressions via the `simplified_str()` method. Currently, it's only available for quadratic equations with 1 variable. For instance:

```python
my_equation = QuadraticEquation("x^2 + 2x + 4x + 8 = 0")
print(my_equation)
print(my_equation.simplified_str())
                    
```

**Get the coefficients of the equation**

You can get the coefficients of the equation via the `coefficients()` method. For instance:

```python
my_equation = QuadraticEquation("x^2 + 6x + 8 = 0")
print(my_equation.coefficients())
                    
```

**Generate a random quadratic equation**

You can generate a random quadratic equation via the static method `random()`. The method accepts several optional parameters:

* `values` - the range of the coefficients in the equation. Default: `(-15, 15)`
* `digits_after` - the number of digits after the decimal point of the solutions. Default: `0`
* `variable` - a string that represents the variable that appears in the equation.
* `strict_syntax` - a boolean value that determines whether the equation will be created in the traditional form of \`ax^2 + bx + c = 0\`. The default is `True`.
* `get_solutions` - If set to True, the function will return both the equation and the solutions of the equation as a tuple of `(equation, solutions)`.

For example:

```python
print(QuadraticEquation.random())
                    
```

```python
print(QuadraticEquation.random(digits_after=1, variable='y'))
                    
```

**Export to PDF worksheets**

You can use the `random_worksheet()` method to create a PDF document with 1 page of equations, and optionally also an additional page of solutions. Here is the signature of the method:

```python
@staticmethod
def random_worksheet(path:str=None, title="Quadratic Equations Worksheet", num_of_equations=20,
                     solutions_range=(-15, 15), digits_after: int = 0, get_solutions=True):
                        
                    
```

For instance:

```python
print(QuadraticEquation.random_worksheet("worksheet1.pdf", title="Example"))
                    
```

You can also create a PDF worksheets with several pages via the `random_worksheets()` method. Here is the signature of the method:

```python
@staticmethod
def random_worksheets(path=None, num_of_pages=2, equations_per_page=20, titles=None,
                     solutions_range=(-15, 15), digits_after: int = 0, get_solutions=False):
                    
```

For instance:

```python
print(QuadraticEquation.random_worksheets("worksheet2.pdf", num_of_pages=5))
                    
```
