## Classical Mechanics - Week 7
 
 
### Last Week:
- Simulated planetary motion
- Saw the limitations of Euler's Method
- Gained experience with the Velocity Verlet Method

### This Week:
- Introduce the SymPy package
- Visualize Potential Energy surfaces
- Explore packages in Python

# Why use packages, libraries, and functions in coding?
Another great question! 

**Simply put:** We could hard code every little algorithm into our program and retype them every time we need them, OR we could call upon the functions from packages and libraries to do these tedious calculations and reduce the possibility of error.

We have done this with numpy.linalg.norm() to calculate the magnitude of vectors.

We will be introducing a new package call SymPy, a very useful [symbolic mathematics](https://en.wikipedia.org/wiki/Computer_algebra) library.


```python
# Let's import packages, as usual
import numpy as np
import matplotlib.pyplot as plt
import sympy as sym
sym.init_printing(use_unicode=True)
```

Let's analyze a simple projectile motion again, but this time using SymPy. 

Assume we have the following equation to express our trajectory in the $x-y$ coordinates:

$y = y_0 - (\beta -x)^2$, where $y_0$ and $\beta$ are constants.


```python
# First we must declare and define our variables. Examine the syntax in this cell then run it. Notice that ordering matters
x, y0, beta = sym.symbols('x, y_0, beta')
```


```python
# Next we will define our function
y = y0 - (beta - x)**2
y # This line outputs a visualization of our equation y(x) below
```


```python
# Now we will set our constants, but leave x alone so we can plot as a function of x
y1 = sym.simplify(y.subs({y0:10, beta:1}))
y1 # This line outputs a visualization of our equation y1(x) below
```


```python
# Run this cell. What happens as you play around with the constants?
sym.plot(y1,(x,0,5), title='Height vs Distance',xlabel='x',ylabel='y')
```

## Q1.) How would you compare plotting with sympy versus what we have done so far? Which method do you prefer?

&#9989; Double click this cell, erase its content, and put your answer to the above question here.

#### Using what we just learned, please set up the following equation using sympy where $U(\vec{r})$ is our potential energy:

$U(\vec{r}) = -e^{-(x^2+y^2)}$


```python
 = sym.symbols() ## Set up our variables here. What should be on the left-hand and right-hand side?
U = -sym.exp() ## What should go in the exp? Notice that using SymPy we need to use the SymPy function for exp
U
```

### We have two ways in which we can graph this:

Either perform the substitution $r^2 = x^2+y^2$ in order to plot in a 2D space ($U(r)$) or we could plot in a 3D space keeping $x$ and $y$ in our equation (U(x,y)). 

## Q2.) What do you think are the benefits/draw-backs of these two methods of analyzing our equation? Which do you prefer?

&#9989; Double click this cell, erase its content, and put your answer to the above question here.

#### Now let's graph the potential
For now we will use sympy to perform both a 2D and 3D plot. Let's do the 2D version first using what we just learned.


```python
r = sym.symbols('r') # Creating our variables
U2D = -sym.exp() # Finish the equation using our replacement for x^2 + y^2
sym.plot(U2D,title='Potential vs Distance',xlabel='Distance (r)',ylabel='Potential (U)')
```

## Q3.) What can you learn from this 2D graph?

&#9989; Double click this cell, erase its content, and put your answer to the above question here.

The cell below imports a function from the sympy package that allows us to graph in 3D. Using the "plot3d" call, make a 3D plot of our originally initalized equation. 

For the 3D plot, try setting the x and y plot limits as close as you can to the origin while having a clear picture of what is happening at the origin. For example x and y could range from (-4,4)


```python
# The below import will allow us to plot in 3D with sympy
# Define "U" as your potential
from sympy.plotting import plot3d
```


```python
# Once you have your potential function set up, execute this cell to obtain a graph
# Play around with the x and y scale to get a better view of the potential curve
plot3d(U,(x,-10,10),(y,-10,10))
```

## Q4.) What can you learn from this 3D graph? (Feel free to make the graph's x and y-range smaller to observe the differences)

&#9989; Double click this cell, erase its content, and put your answer to the above question here.

##### Let's get some more in-depth coding experience:
Try to graph this last potential energy function using SymPy or with Numpy (there is a 3d plotting example back in Week 2's notebook).

$U(r) = 3.2\cdot e^{-(0.5x^2+0.25y^2)}$


```python
## Set up and graph the potential here


```

## Q5.) How would you describe the new potential?

&#9989; Double click this cell, erase its content, and put your answer to the above question here.

### Try this: 
Center the new potential at (1,1) instead of (0,0).  (That is, move the peak of the graph from (0,0) to (1,1).)


```python
## Plot the adjustment here
```

## Q6.) How did you move the peak of the graph?

&#9989; Double click this cell, erase its content, and put your answer to the above question here.

# Notebook Wrap-up. 
Run the cell below and copy-paste your answers into their corresponding cells.


```python
from IPython.display import HTML
HTML(
"""

"""
)
```

# Congratulations! Another week, another Notebook.

As we can see, there are many tools we can use to model and analyze different problems in Physics on top of Numerical methods. Libraries and packages are such tools that have been developed by scientists to work on different topics, each package specific to a different application. But this is just food for thought. Although we use some basic package functions, we won't be using advanced scientific packages to do simulations and calculations in this class.


```python

```

3cbcf15e13755154ea45e27796040309ef74f489
ipynb
Jupyter Notebook
doc/AdminBackground/PHY321/CM_Jupyter_Notebooks/Student_Work/CM_Notebook7.ipynb
Shield94/Physics321
9875a3bf840b0fa164b865a3cb13073aff9094ca
CC0-1.0
2020-01-09T17:41:16.000Z
2022-03-09T00:48:58.000Z
doc/AdminBackground/PHY321/CM_Jupyter_Notebooks/Student_Work/CM_Notebook7.ipynb
Shield94/Physics321
9875a3bf840b0fa164b865a3cb13073aff9094ca
CC0-1.0
2020-01-08T03:47:53.000Z
2020-12-15T15:02:57.000Z
doc/AdminBackground/PHY321/CM_Jupyter_Notebooks/Student_Work/CM_Notebook7.ipynb
Shield94/Physics321
9875a3bf840b0fa164b865a3cb13073aff9094ca
CC0-1.0
2020-01-10T20:40:55.000Z
2022-02-11T20:28:41.000Z
Qwen/Qwen-72B
1. YES
2. YES