```python
## This cell just imports necessary modules
%pylab notebook
from sympy import sin, cos, exp, Function, Symbol, diff, integrate, solve
from mpl_toolkits import mplot3d
```


```python
###### SCALAR FIELDS ######
###### Lecture 3, slide 3 ######
# Create a mesh of 2D Cartesian coordinates, where -5 <= x <= 5 and -5 <= y <= 5
x = numpy.arange(-2., 2., 0.025)
y = numpy.arange(-2., 2., 0.025)
X, Y = numpy.meshgrid(x, y)

# Computes the value of the scalar field at each (x,y) coordinate, and stores it in Z.
f = 16 - 2*(X**2) - Y**2 + X*Y
```


```python
# The scalar field on a contour plot
print("Plotting contours of the scalar field f(x,y) = 16 - 2*(x**2) - y**2 + x*y...")
fig = pylab.figure()
contour_plot = pylab.contour(X, Y, f)
pylab.clabel(contour_plot, inline=1)
pylab.xlabel("x")
pylab.ylabel("y")
```


```python
fig = pylab.figure()
ax = pylab.axes(projection='3d')
ax.plot_surface(X, Y, f, cmap='gray', edgecolor = 'k', lw=0.25)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("h")
```


```python
fig = pylab.figure()
ax = pylab.axes(projection='3d')
ax.contour3D(X, Y, f, 10, colors = 'k', linestyles='-')
ax.plot_surface(X, Y, f, cmap='gray', alpha=0.5)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("h")
```


```python
###### VECTOR FIELDS ######
###### Lecture 3, slide 4 ######
# Create a mesh of 2D Cartesian coordinates, where -5 <= x <= 5 and -5 <= y <= 5
x = numpy.arange(-5.0, 5.0, 0.25)
y = numpy.arange(-5.0, 5.0, 0.25)
X, Y = numpy.meshgrid(x, y)

# Computes the value of the vector field at each (x,y) coordinate.
# Z1 and Z2 hold the i and j component of the vector field respectively.
Z1 = -(X**2)
Z2 = -(Y**2)

print("Plotting the vector field f(x,y) = [-x**2, -y**2] ...")
fig = pylab.figure()
plt = pylab.quiver(X,Y,Z1,Z2,angles='xy',scale=1000,color='r')
pylab.quiverkey(plt,-5,5.5,50,"50 units",coordinates='data',color='r')
pylab.xlabel("x")
pylab.ylabel("y")
```


```python
###### GRADIENTS ######
###### Lecture 3, slide 6 ######

# Define the independent variables using Symbol
x = Symbol('x')
y = Symbol('y')
# Define the function f(x,y)
f = 16 - 2*(x**2) - y**2 + x*y

# The gradient of f (a scalar field) is a vector field:
grad_f = [diff(f,x), diff(f,y)]
print("The gradient of the scalar field f(x,y) = 16 - 2*(x**2) - y**2 + x*y is: ")
print(grad_f)

print("The point where the gradient is zero is: ")
# We solve a simultaneous equation such that grad_f[0] == 0 and grad_f[1] == 0
print(solve([grad_f[0], grad_f[1]], [x, y]))

```


```python
###### DIRECTIONAL DERIVATIVES ######
###### Lecture 3, slide 12 ######

# Define the independent variables using Symbol
x = Symbol('x')
y = Symbol('y')
# Define the function h(x,y)
h = 3*x*(y**2)

# The gradient of h
grad_h = [diff(h,x), diff(h,y)]
print("The gradient of h(x,y) = 3*x*(y**2) is: ")
print(grad_h, "\n")

print("At the point (1,2), the gradient is: ")
# Use evalf to evaluate a function, with subs to substitute in specific values for x and y
grad_h_at_point = [grad_h[0].evalf(subs={x:1, y:2}), grad_h[1].evalf(subs={x:1, y:2})]
print(grad_h_at_point, "\n")

# Find the unit vector in the direction 3i + 4j
a = numpy.array([3, 4])
a_magnitude = numpy.linalg.norm(a, ord=2)
unit_a = a/a_magnitude

print("The unit vector in the direction 3i + 4j is:")
print(unit_a, "\n")

# Dot product to get the directional derivative 
# (i.e. the gradient of h in the direction of the vector unit_a)
slope = numpy.dot(grad_h_at_point, unit_a)
print("The slope of h in the direction ", unit_a, " at (1,2) is: ", slope)


```


```python
###### DIVERGENCE ######
###### Lecture 3, slide 15 ######

# Define the independent variables using Symbol
x = Symbol('x')
y = Symbol('y')
# Define the vector field v(x,y)
v = [-(x**2), -(y**2)]

# Compute the divergence using diff. 
# NOTE 1: A neater way would be to use SymPy's dot function. 
# However, there doesn't seem to be a way of defining a gradient vector
# in SymPy without specifying the function we wish to operate on,
# so we'll compute the divergence the long way.
# NOTE 2: this is the dot product of the gradient vector and v,
# which will always result in a scalar.
# d/dx is applied to the first component of v (i.e. v[0]),
# d/dy is applied to the second component of v (i.e. v[1])
divergence = diff(v[0],x) + diff(v[1],y)
print("The divergence of the vector field ", v, " is: ")
print(divergence)

```


```python
###### CURL ######
###### Lecture 3, slide 19 ######

# Define the independent variables using Symbol
x = Symbol('x')
y = Symbol('y')
# Define the vector field v(x,y)
v = [cos(pi*y), -cos(pi*x)]

# Compute the curl using diff.
# Remember: the curl of a vector always results in another vector.
# The first two components of the curl are zero because v has a zero k-component.
curl = [0, 0, diff(v[1], x) - diff(v[0], y)]
print("The curl of the vector field ", v, " is: ")
print(curl)
print()
print("At the point (0, -0.5), the curl is: ")
print([0, 0, curl[2].evalf(subs={x:0, y:-0.5})])
```


```python
###### LAPLACIAN ######
###### Lecture 3, slide 22 ######

# Define the independent variables using Symbol
x = Symbol('x')
y = Symbol('y')
# Define the scalar field f(x,y)
f = x*y + 3*exp(x*y)

# In SymPy we can specify the order of the derivative as an optional argument
# (in this case, it is '2' to get the second derivative).
print("The Laplacian of ", f, " is: ")
laplacian = diff(f, x, 2) + diff(f, y, 2)
print(laplacian)
```


```python

```

c330f0a2866478f8a0ce6deb2e90f1cf4ae2fe4a
ipynb
Jupyter Notebook
mathematics/mm1/Lecture_3_Vector_Calculus.ipynb
jrper/thebe-test
554484b1422204a23fe47da41c6dc596a681340f
MIT
mathematics/mm1/Lecture_3_Vector_Calculus.ipynb
jrper/thebe-test
554484b1422204a23fe47da41c6dc596a681340f
MIT
mathematics/mm1/Lecture_3_Vector_Calculus.ipynb
jrper/thebe-test
554484b1422204a23fe47da41c6dc596a681340f
MIT
Qwen/Qwen-72B
1. YES
2. YES