Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn The Gradient Vector | Section
Understanding Multivariate Calculus
SectionΒ 1. ChapterΒ 4
single

single

bookThe Gradient Vector

Swipe to show menu

The gradient vector is a fundamental concept in multivariate calculus, representing the direction and rate of the steepest ascent of a scalar-valued function of several variables. If you have a function f(x,y,...,n)f(x, y, ..., n), the gradient of ff at any point is a vector composed of the partial derivatives of ff with respect to each variable. Mathematically, for a function f(x,y)f(x, y), the gradient is defined as:

βˆ‡f(x,y)=[βˆ‚fβˆ‚x,βˆ‚fβˆ‚y]βˆ‡f(x, y) = \left[\frac{βˆ‚f}{βˆ‚x}, \frac{βˆ‚f}{βˆ‚y}\right]

where βˆ‚fβˆ‚x\frac{\raisebox{1pt}{$βˆ‚f$}}{\raisebox{-1pt}{$βˆ‚x$}} and βˆ‚fβˆ‚y\frac{\raisebox{1pt}{$βˆ‚f$}}{\raisebox{-1pt}{$βˆ‚y$}} are the partial derivatives of ff with respect to xx and yy, respectively. The gradient vector always points in the direction where the function increases most rapidly, and its length tells you how fast the function increases in that direction.

Some important properties of the gradient vector include:

  • The gradient is perpendicular (normal) to the level curves (contours) of the function;
  • If the gradient at a point is zero, that point is a critical point (which could be a maximum, minimum, or saddle point);
  • The gradient can be generalized to functions of more than two variables by including all partial derivatives as components of the vector.

To compute the gradient, you simply calculate all the first-order partial derivatives with respect to each variable at the point of interest.

1234567891011121314151617181920212223242526272829
import numpy as np import matplotlib.pyplot as plt # Define the sample function def f(x, y): return x**2 + y**2 # Compute the gradient of f def grad_f(x, y): df_dx = 2 * x df_dy = 2 * y return np.array([df_dx, df_dy]) # Create a grid of points x = np.linspace(-2, 2, 20) y = np.linspace(-2, 2, 20) X, Y = np.meshgrid(x, y) # Compute gradient at each point U = 2 * X V = 2 * Y plt.figure(figsize=(6, 6)) plt.quiver(X, Y, U, V, color="blue", angles='xy') plt.title("Gradient Vector Field for f(x, y) = xΒ² + yΒ²") plt.xlabel("x") plt.ylabel("y") plt.grid(True) plt.show()
copy
Task

Swipe to start coding

Calculate the gradient vector of the function $f(x, y) = x^3 + y^2$ at a given point using the sympy library in the global scope.

  • Define the symbols x and y utilizing sympy.symbols().
  • Define the mathematical expression f=x3+y2f = x^3 + y^2.
  • Compute the partial derivative of ff with respect to x and y utilizing sympy.diff().
  • Evaluate both derivatives at the coordinates provided by the val_x and val_y variables utilizing the .subs() method.
  • Assign the evaluated results to the variables grad_x and grad_y as floats.
  • Print the result as a tuple (grad_x, grad_y).

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 4
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt