# Solving Multiple Equations/Multiple Unknowns in SymPy

Joseph C. Slater, Oct. 2018


```python
# import symbolic capability to Python
from sympy import *

# print things all pretty. Not as important in this notebook. 
from sympy.abc import *
init_printing()
```


```python
# Need to define the variables as symbolic for sympy to use them. 
x, y = symbols("x, y", real = True)
```

All of this is based on using the [general symbolic solvers](https://docs.sympy.org/latest/modules/solvers/solvers.html). They are not as robust as putting things in matrix form. A function to do that wouldn't be too hard, but would take a couple hours to make.

Let's do a simple test case.

$$ x^4 = 4$$

Note that we have to solve it to be equal to zero, so we use the form

$$x^4-4=0$$


```python
equation = x**2 - 4
solve(equation, x)
```

That worked. Let's make some equation. I'm going to store them in variable to make this easier to keep track of. 

Here we have

$$3x + 4y - 5 = 0$$


```python
equation1 = 3*x + 4*y - 5
equation1
```

Now we have

$$
6x+7y-8=0
$$


```python
equation2 = 6*x + 7*y - 8
equation2
```

``solve`` would like to be told which variables to solve for, but it can usually make a reasonable guess. 

The (seemingly) extra parentheses are because there is only a single argument being sent in, the list of equations: ``(equation1, equation2)``. 


```python
solve((equation1, equation2))
```

Below I'm more explicit in sending it two arguments (two lists), the first is a list of equations, the second is a list of variables to solve for. 


```python
solve((equation1, equation2), (x, y))
```


```python
checksol(equation1, {x: -1, y: 2})
```




    True



If the second equation had been nonlinear, such as

$$6x+7y^2-8=0$$


```python
equation2 = 6*x + 7*y**2 - 8
equation2
```

We now get multiple solutions:


```python
ans = solve((equation1, equation2))
ans
```

Engineers like decimals in most cases, so, if any number given were a float, we wouldn't get the square roots and all. 


```python
equation2 = 6*x + 7*y**2 - 8.0
equation2
```


```python
solve((equation1, equation2))
```

We can create an equation with two sides by using the ``Eq`` command as below. 


```python
equation2 = Eq(6*x + 7*y**2 , 8.0)
equation2
```


```python
solve((equation1, equation2))
```

99ae3c7d951014ad75e899b91ee55c8cc8fadb6d
ipynb
Jupyter Notebook
Solving_Multiple_Symbolic_Algebraic_Equations.ipynb
josephcslater/JupyterExamples
4f2af75b9fda80dcab6cac0b9210713eeb703839
BSD-3-Clause
Solving_Multiple_Symbolic_Algebraic_Equations.ipynb
josephcslater/JupyterExamples
4f2af75b9fda80dcab6cac0b9210713eeb703839
BSD-3-Clause
Solving_Multiple_Symbolic_Algebraic_Equations.ipynb
josephcslater/JupyterExamples
4f2af75b9fda80dcab6cac0b9210713eeb703839
BSD-3-Clause
2020-02-07T20:28:39.000Z
2020-02-07T20:28:39.000Z
Qwen/Qwen-72B
1. YES
2. YES