Gradients and Direction Fields

In [87]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sympy as sy
from IPython.display import HTML, IFrame, YouTubeVideo

Sketch solutions

Direction Fields

In [83]:
def df(x):
    return x**2
In [85]:
x = np.linspace(-3,3, 20)
y = np.linspace(-3,3, 20)
X,Y = np.meshgrid(x, y)

# plt.plot(x, x**3/3)
# def f(x, c):
#     return x**3/3 + c
# for c in range(-3,3):
#     plt.plot(x, f(x, c))
# plt.xlim(-5, 5)
# plt.ylim(-4,4)
In [86]:
from IPython.core.display import HTML
def css_styling():
    styles = open("styles/style.css", "r").read()
    return HTML(styles)
css_styling()
Out[86]:
h1 {color: 'red';}

s

In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [5]:
IFrame(src='https://en.wikipedia.org/wiki/Gradient', width = 800, height = 600)
Out[5]:

Example

Let $f(x, y) = x^ + y^2$.

In [20]:
def f(x, y):
    return x**2 + y**2
In [30]:
x = np.linspace(-4,4,100)
y = np.linspace(-2,2,100)
X, Y = np.meshgrid(x, y)
plt.contour(X, Y, f(X, Y), levels = 40, cmap = 'PRGn')
plt.plot(x, 2-2/3*x, color = 'red')
plt.ylim(-2,2)
Out[30]:
(-2.0, 2.0)
In [34]:
def f(x, y):
    return x**2*y + x + y
In [39]:
x = np.linspace(-4,4,100)
y = np.linspace(-2,2,100)
X, Y = np.meshgrid(x, y)
plt.contour(X, Y, f(X, Y), levels = 100, cmap = 'PRGn')
plt.plot(x, 4/x, color = 'red')
plt.ylim(-2,2)
Out[39]:
(-2.0, 2.0)
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [65]:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
In [61]:
x1 = []
y1 = []
for x, y in zip(x,y):
    if x*y == 4:
        x1.append(x)
        y1.append(y)
In [63]:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, f(X, Y))
ax.plot(X, 4/X, color='red', linewidth = 200)
x1 = np.array(x1)
y1 = np.array(y1)
X1, Y1 = np.meshgrid(x1, y1)
ax.plot_surface(X1, Y1, c(X1, Y1))
Out[63]:
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x7ffe967be250>
In [64]:
plt.quiver(X, Y, f(X, Y))
Out[64]:
<matplotlib.quiver.Quiver at 0x7ffe525b9e50>
In [ ]:
 
In [ ]:
 
In [5]:
import numpy as np
In [6]:
np.array([[0, 0, 0]])
Out[6]:
array([[0, 0, 0]])
In [ ]: