## 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.

################ Possible Answer #################

Sympy Gives a more direct way of creating graphs. 
Numpy is better for numerical analysis. 

################ Possible Answer #################


#### 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
U, x, y = sym.symbols('U, x, y') ## Set up our variables here. What should be on the left-hand and right-hand side?
U = -sym.exp(-(x**2+y**2)) ## 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.

####### Possible Answer #######

The 2D method keeps things simple and allows us to see how potential changes as the magnitude of our distance changes. A drawback is that we don't get to see how the individual x and y components affect our potential.

The 3D method lets us see how potential changes as a result of both x and y, allowing us to see a more in-depth analysis of the potential.  For instance, the rotational symmetry in the xy plane is apparent in the 3D plot. However, the graph is a bit more complicated since visualizing 3 dimensions on a 2D surface (a sheet of paper or a computer screen) is sometimes difficult.

I prefer the air.

####### Possible Answer #######

#### 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(-r**2) # 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.

####### Possible Answer #######

The Potential Energy has a minimum at $r=0$.  As we get closer to the origin the potential energy becomes more negative with $U(0) = -1$.  The width of the well is of order 2.

####### Possible Answer #######

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,-2,2),(y,-2,2))
```

## 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.

####### Possible Answer #######

It shows similar information as the 2D plot, but now we can see the azimuthal symmetry in the xy plane.

####### Possible Answer #######

##### 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^3)}$


```python
## Set up and graph the potential here
U = 3.2*sym.exp(-(0.5*x**2+0.25*y**2))
plot3d(U,(x,-4,4),(y,-4,4))
```

## 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.

####### Possible Answer #######

Now the Potential Energy has a maximum at $r=0$, with a value of $U(0)=3.2$.  It is no longer azimuthally symmetric in the xy plane, although it is difficult to see this in the 3D plot.

####### Possible Answer #######

### 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
U = 3.2*sym.exp(-(0.5*(x-1)**2+0.25*(y-1)**2))
plot3d(U,(x,-4,4),(y,-4,4))
```

## 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.

####### Possible Answer #######

Replace $x$ with $x-1$ and $y$ with $y-1$ in the potential.

####### Possible Answer #######

# 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

```

7670a822f4b25988c64f2db33670118cec7fd388
ipynb
Jupyter Notebook
doc/AdminBackground/PHY321/CM_Jupyter_Notebooks/Answers/CM_Notebook7_Answers.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/Answers/CM_Notebook7_Answers.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/Answers/CM_Notebook7_Answers.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