The gradient of a scalar-valued differentiable, multivariate function
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
Example
- Given
- Given
- 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))