The gradient of a scalar-valued differentiable, multivariate function stores all of its partial derivatives into a vector. It can be interpreted as the “direction and rate of fastest increase” or “direction of steepest ascent.”

Definition

is a vector-valued function takes all inputs from the function and outputs a vector with all possible partial derivatives of :

If you imagine standing at a point in the input space of ,  tells you at which DIRECTION and RATE you should travel to increase the value of  most rapidly.

also always gives a vector orthogonal (perpendicular) to the line tangent to the level curves passing through the point

Example

  1. Given
  1. Given
  1. The gradient of at

Code

import sympy as sp
import numpy as np
 
def construct_gradient(f, variables):
    """
    Constructs the gradient for a function f with respect to given variables.
 
    Parameters:
    - f: sympy expression representing the function.
    - variables: list of sympy symbols representing the variables.
 
    Returns:
    - A sympy matrix representing the gradient.
    - A lambdified function for evaluating the gradient numerically.
    """
    # Compute the gradient vector (partial derivatives with respect to each variable)
    gradient_vector = sp.Matrix([sp.diff(f, var) for var in variables])
    
    # Lambdify the gradient vector for numerical evaluation
    gradient_lambdified = sp.lambdify(variables, gradient_vector, 'numpy')
    
    return gradient_vector, gradient_lambdified
 
# Example usage
# Define the variables for the given function A(a, θ)
a, theta = sp.symbols('a theta')
 
# Define the function A(a, θ)
A = 3 * a * sp.sin(theta) - a**2 * sp.sin(theta) - a**2 * sp.sin(theta)**2 + (1/2) * a**2 * sp.sin(theta) * sp.cos(theta)
 
# List of variables
variables = [a, theta]
 
# Construct the gradient
gradient_sympy, gradient_numeric = construct_gradient(A, variables)
 
# Display the symbolic gradient
print("Symbolic Gradient Vector:")
sp.pprint(gradient_sympy)
 
# Example point to evaluate the gradient at
point = np.array([1.0, np.pi/4])
 
# Evaluate the gradient at the given point
gradient_value = gradient_numeric(*point)
print("\nGradient evaluated at point", point, ":")
print(np.array(gradient_value, dtype=float))