# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="XVJXkWBn8yOQ"
# # Vector Fields
# We can denote a vector field with the vector F has a function of x and y like this: F(x,y)
#
# We can also write it in 3d like this: F(x,y,z)
#
# $ \vec{F}(x,y,z) = P(x,y,z)*\vec{i}+Q(x,y,z)*\vec{j}+R(x,y,z)*\vec{k} $

# + [markdown] id="xqVHuhHN81ub"
# ## Example Problem 1:
#
# $ \vec{F}(x,y) = -y*\vec{i}+x*\vec{j} $
#
# Graph the Vector Field

# + colab={"base_uri": "https://localhost:8080/", "height": 265} id="ny5Jevt88u_N" outputId="9796f633-9dc7-4462-eb8d-5ee13899ea5b"
import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inline

x,y = np.meshgrid(np.linspace(-5,5,10),np.linspace(-5,5,10))

u = -y
v = x

plt.quiver(x,y,u,v)
plt.show()

# + [markdown] id="djScBVV09G34"
# As you can see, the function follows a circular path centered at the origin. The length of each arrow is dependent on the x and y value chosen. The further away the end of the arrow is from the origin, the larger the arrow will be. Each arrow is tangent to a circle centered at the origin.
#
# To calculate the radius of the circle, we take the following steps:
#
# $ 
#   r = \sqrt{x^2+y^2}
# $
#

# + [markdown] id="ZUQf0Z9G-CWo"
# ## Example Problem 2:
#
# $ \vec{f}(x,y) = x^2y\vec{i}-y^3\vec{j} $
#
# Plot the gradient vector field and the contour map of the original function

# + [markdown] id="3_KEP8aW-Vjk"
# Our first step is to calculate the gradient of $ \vec{f} $. We do this by taking the derivative of the P and Q function in f:
#
# $ P = df/dx $
#
# $ df/dx = 2xy $
#
# $ Q = df/dy $
#
# $ df/dy = x^2-3y^2 $
#
# So our final gradient of f is as follows:
#
# $ \nabla{\vec{f}} = 2xy\vec{i}+(x^2-3y^2)\vec{j} $
#
# Now we can graph the gradient and our contour map

# + colab={"base_uri": "https://localhost:8080/", "height": 265} id="dn3ElEiT-Agx" outputId="d426f0f0-4c2f-409c-a15a-39a1878d0861"
x,y = np.meshgrid(np.linspace(-5,5,10),np.linspace(-5,5,10))

u = 2*x*y
v = x**2-3*y**2

plt.quiver(x,y,u,v)

xlist = np.linspace(-5.0, 5.0, 100)
ylist = np.linspace(-5.0, 5.0, 100)
X, Y = np.meshgrid(xlist, ylist)
Z = Y*X**2 - Y**3
plt.contour(X,Y,Z)

plt.show()


# + [markdown] id="kKKW7kBVBeES"
# The Vector Field shown here are perpendicular to the contour map, which would make sense considering the vector field is of the gradient vector of f.
#
# **If $ \vec{F} $ is conservative, then $ f $ exists such that $ \vec{F} = \nabla{f} $**

# + id="Lzl9a9l7CA2x"


StarcoderdataJupyterScriptsDedupFiltered