```python
from sympy import *
from sympy.solvers.solveset import solveset
init_printing()
x, y, z = symbols('x,y,z')
```

## Solveset

Equation solving is both a common need also a common building block for more complicated symbolic algorithms.  

Here we introduce the `solveset` function.


```python
solveset(x**2 - 4, x)
```

Solveset takes two arguments and one optional argument specifying the domain, an equation like $x^2 - 4$ and a variable on which we want to solve, like $x$ and an optional argument domain specifying the region in which we want to solve.

Solveset returns the values of the variable, $x$, for which the equation, $x^2 - 4$ equals 0.

### Exercise

What would the following code produce?  Are you sure?


```python
solveset(x**2 - 9 == 0, x)
```

## Infinite Solutions

One of the major improvements of `solveset` is that it also supports infinite solution.


```python
solveset(sin(x), x)
```

## Domain argument


```python
solveset(exp(x) -1, x)
```

`solveset` by default solves everything in the complex domain. In complex domain $exp(x) == cos(x) + i\ sin(x)$ and solution is basically equal to solution to $cos(x) == 1$. If you want only real solution, you can specify the domain as `S.Reals`.


```python
solveset(exp(x) -1, x, domain=S.Reals)
```

## Condition Set

`solveset` isn't always able to solve a given equation, such cases it returns a `ConditionSet` object. `ConditionSet` represents a set satisfying a given condition.


```python
solveset(exp(x) + cos(x) + 1, x, domain=S.Reals)
```

`solveset` aims to return all the solutions of the equation. In cases where it able to find some solution but not all it returns a union of the known solutions and `ConditionSet`.


```python
solveset((x - 1)*(exp(x) + cos(x) + 1), x, domain=S.Reals)
```

## Symbolic use of `solveset`

Results of `solveset` don't need to be numeric, like `{-2, 2}`.  We can use solveset to perform algebraic manipulations.  For example if we know a simple equation for the area of a square

    area = height * width
    
we can solve this equation for any of the variables.  For example how would we solve this system for the `height`, given the `area` and `width`?


```python
height, width, area = symbols('height, width, area')
solveset(area - height*width, height)
```

Note that we would have liked to have written

    solveset(area == height * width, height)
    
But the `==` gotcha bites us.  Instead we remember that `solveset` expects an expression that is equal to zero, so we rewrite the equation

    area = height * width
    
into the equation

    0 = height * width - area
    
and that is what we give to solveset.

### Exercise

Compute the radius of a sphere, given the volume.  Reminder, the volume of a sphere of radius `r` is given by

$$ V = \frac{4}{3}\pi r^3 $$


```python
# Solve for the radius of a sphere, given the volume

```

You will probably get several solutions, this is fine.  The first one is probably the one that you want.

## Substitution

We often want to substitute in one expression for another.  For this we use the subs method


```python
x**2
```


```python
# Replace x with y
(x**2).subs({x: y})
```

### Exercise

Subsitute $x$ for $sin(x)$ in the equation $x^2 + 2\cdot x + 1$


```python
# Replace x with sin(x)


```

## Subs + Solveset

We can use subs and solve together to plug the solution of one equation into another


```python
# Solve for the height of a rectangle given area and width

soln = list(solveset(area - height*width, height))[0]
soln
```


```python
# Define perimeter of rectangle in terms of height and width

perimeter = 2*(height + width)
```


```python
# Substitute the solution for height into the expression for perimeter

perimeter.subs({height: soln})
```

### Exercise

In the last section you solved for the radius of a sphere given its volume


```python
V, r = symbols('V,r', real=True)
4*pi/3 * r**3
```


```python
list(solveset(V - 4*pi/3 * r**3, r))[0]
```

Now lets compute the surface area of a sphere in terms of the volume.  Recall that the surface area of a sphere is given by

$$ 4 \pi r^2 $$


```python
(?).subs(?)
```

Does the expression look right?  How would you expect the surface area to scale with respect to the volume?  What is the exponent on $V$?

## Plotting

SymPy can plot expressions easily using the `plot` function.  By default this links against matplotlib.


```python
%matplotlib inline
```


```python
plot(x**2)
```

### Exercise

In the last exercise you derived a relationship between the volume of a sphere and the surface area.  Plot this relationship using `plot`.


```python
plot(?)
```

## Low dependencies

You may know that SymPy tries to be a very low-dependency project.  Our user base is very broad.  Some entertaining aspects result.  For example, `textplot`.


```python
textplot(x**2, -3, 3)
```

### Exercise

Play with `textplot` and enjoy :)

90f413f64fcfbd36f53378b48bfa9ba920c5c446
ipynb
Jupyter Notebook
tutorial_exercises/02-Solveset-Subs-Plot.ipynb
gvvynplaine/scipy-2016-tutorial
aa417427a1de2dcab2a9640b631b809d525d7929
BSD-3-Clause
2016-06-21T21:11:02.000Z
2021-02-04T07:51:03.000Z
tutorial_exercises/02-Solveset-Subs-Plot.ipynb
gvvynplaine/scipy-2016-tutorial
aa417427a1de2dcab2a9640b631b809d525d7929
BSD-3-Clause
2016-07-02T20:24:06.000Z
2016-07-11T11:31:44.000Z
tutorial_exercises/02-Solveset-Subs-Plot.ipynb
gvvynplaine/scipy-2016-tutorial
aa417427a1de2dcab2a9640b631b809d525d7929
BSD-3-Clause
2016-06-25T09:04:24.000Z
2021-08-09T06:46:01.000Z
Qwen/Qwen-72B
1. YES
2. YES