Vector Calculus with Python - Gradient, Div, Curl, Stokes, Divergence

preview_player
Показать описание
Here is how to calculate vector functions in python.

I said I would include links to some other videos- here they are:

2D Green's theorem

Green's theorem visualization

Stoke's Theorem with a more complicated path

Maxwell's equations in differential form

Stoke's Theorem for a hemisphere

Divergence Theorem for a cube (not python)

Numerical methods for div grad curl
Рекомендации по теме
Комментарии
Автор

You are doing amazing work sirrr.... Please keep it up...

aryan
Автор

if 'I' wanna implement this on Python, you have to use np.array([x, y, z]) not using vector(x, y, z)
The following code is the gradient code on Python:

# Gradient
# g(x, y, z) = y * z^2 * sin(x)
from math import sin
import numpy as np
def g(p):
return p[1] * p[2]**2 * sin(p[0])

def gradg(p):
ds = 0.001
dx = np.array([ds, 0, 0])
dy = np.array([0, ds, 0])
dz = np.array([0, 0, ds])
# dg/dx, dg/dy, dg/dz
# dg/dx = (g(x+Δx) - g(x-Δx)) / 2Δx
# dg/dy = (g(y+Δz) - g(y-Δz)) / 2Δy
# dg/dz = (g(y+Δz) - g(y-Δz)) / 2Δz
# Δx = Δx = Δx = ds = 0.001 --> very small
gx = (g(p + dx) - g(p - dx)) / (2*ds)
gy = (g(p + dy) - g(p - dy)) / (2*ds)
gz = (g(p + dz) - g(p - dz)) / (2*ds)
return np.array([gx, gy, gz])

rt = np.array([1, 1, 1])
print("g(1, 1, ) = ", g(rt))
print("Delg(1, 1, 1) = ", gradg(rt))


# Divergence
def F(p):
return np.array([p[0]**2 * p[1], 2**p[0] * p[2], p[2]**2 - 2*p[1]])

def divF(p):
ds = 0.001
dx = np.array([ds, 0, 0])
dy = np.array([0, ds, 0])
dz = np.array([0, 0, ds])

tempx = (F(p+dx)[0] - F(p-dx)[0]) / (2*ds)
tempy = (F(p+dy)[1] - F(p-dy)[1]) / (2*ds)
tempz = (F(p+dz)[2] - F(p-dz)[2]) / (2*ds)
return tempx + tempy + tempz

rt = np.array([1, 1, 1])
print("F(1, 1, -1) = ", F(rt))
print("DelF(1, 1, 1) = ", divF(rt))

러블리민희
Автор

I put something like this in every time you do 3D visualizations to indicate axes.
#put arrows for the axes...as a reference.
axisscale = 0.1
xaxis = arrow(pos=vector(0, 0, 0), shaftwidth=.1*axisscale,
axis=axisscale*vector(1, 0, 0), label="x", color=color.purple)
yaxis = arrow(pos=vector(0, 0, 0), shaftwidth=.1*axisscale,
axis=axisscale*vector(0, 1, 0), label="y", color=color.red)
zaxis = arrow(pos=vector(0, 0, 0), shaftwidth=.1*axisscale,
axis=axisscale*vector(0, 0, 1), label="z", color=color.green)

johnh.g.weisenfeld
Автор

Could you please post the full code for the Path Integral, Flux, Stokes theorem, hemisphere, volume integral, and divergence theorem?

HH-mwsq
Автор

Could you please post the full code for the Path Integral, Flux, Stokes theorem, hemisphere, volume integral, and divergence theorem?

HH-mwsq
Автор

Nice. I have been using your Python/Vpython vids for several years with my students, and this will definitely be added o the list. Side note: nabla was chosen as the formal name for the symbol (typesetting, 'del' being the operator name, in the same sense as octothorpe is the name for symbol '#', though it has many names in application) due to the resemblance to a form of phoenician harp. Yes, I know this due to one of my undergrad profs. Yes, I make sure my student know it. And, yes, I read it as 'del dot F' and 'del cross F'. The same prof was a pedant about the dot and cross product forms for divergence and curl. 'It is not a full-fledged vector! You can not treat it as one!'

johnjohn-edqt