#*****************************************************************************80
#
## gradient_vector makes a vector plot of the gradient of a function f(x,y).
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 06 September 2020
#
# Author:
#
# John Burkardt
#
library ( OceanView )
library ( pracma )
cat ( "\n" )
cat ( "gradient_vector:\n" )
cat ( " ", version$version.string, "\n" )
cat ( " Plot the gradient vector field of a function f(x,y).\n" )
#
# Define the grid.
#
x <- seq ( from = -2.0, to = +2.0, length.out = 31 )
y <- seq ( from = -2.0, to = +2.0, length.out = 31 )
XY <- meshgrid ( x, y )
X <- XY$X
Y <- XY$Y
#
# Create the data.
#
Z <- 2.0 * X^2 - 1.05 * X^4 + X^6 / 6.0 + X * Y + Y^2
dZdX <- - ( 4.0 * X - 4.20 * X^3 + X^5 + Y )
dZdY <- - ( X + 2.0 * Y )
#
# Plot.
#
filename = 'gradient_vector.png'
png ( filename )
quiver2D ( dZdX, dZdY, X, Y,
arr.max = 0.4,
type = "triangle",
scale = 4.0,
contour = list ( z = Z, x = x, y = y, levels = seq ( 0.0, 10.0, by = 0.5 ) ),
main = "Gradient vector field" )
cat ( "\n" )
cat ( " Graphics saved as '", filename, "'\n" )
#
# Terminate.
#
cat ( "\n" )
cat ( "gradient_vector:\n" )
cat ( " Normal end of execution.\n" )
quit ( )