I am trying to draw a potential field for a given object using the following formula:
U=-α_goal*e^(-((x-x_goal )^2/a_goal +(y-y_goal^2)/b_goal ) )
using the following code
# Set limits and number of points in grid
xmax = 10.0
xmin = -xmax
NX = 20
ymax = 10.0
ymin = -ymax
NY = 20
# Make grid and calculate vector components
x = linspace(xmin, xmax, NX)
y = linspace(ymin, ymax, NY)
X, Y = meshgrid(x, y)
x_obstacle = 0
y_obstacle = 0
alpha_obstacle = 1
a_obstacle = 1
b_obstacle = 1
P = -alpha_obstacle * exp(-(X - x_obstacle)**2 / a_obstacle + (Y - y_obstacle)**2 / b_obstacle)
Ey,Ex = gradient(P)
print Ey
print Ex
QP = quiver(X, Y, Ex, Ey)
show()
This code calculates a potential field. How can I plot this potential field nicely? Also, given a potential field, what is the best way to convert it to a vector field? (vector field is the minus gradient of the potential field. )
I would appreciate any help.
I have tried using np.gradient() but the result is not what I have expected:
What I do expect, is something along these lines:
EDIT: After changing the two lines in the code:
y, x = np.mgrid[500:-100:200j, 1000:-100:200j]
p = -1 * np.exp(-((x - 893.6)**2 / 1000 + (y - 417.35)**2 / 1000))
I have an incorrect plot: it seems to be inverted left-right (arrows seem to be in correct spot but not the field):
EDIT:
Fixed by changing to
y, x = np.mgrid[500:-100:200j, -100:1000:200j]
Any idea why?