### What is gradient function and how to calc in R

Introduction

In short, gradient is a measure of steepness or rate of change.

The gradient is a fancy word for derivative, or the rate of change of a function. It’s a vector (a direction to move) that

• points in the direction of greatest increase of a function;
• is zero at a local maximum or local minimum (because there is no single direction of increase).

$grad f(x,y,z)=\nabla f(x,y,z) = (\frac{df}{dx}, \frac{df}{dy}, \frac{df}{dz})$

The term gradient is typically used for functions with several inputs and a single output (a scalar field). We can represent these multiple rates of change in a vector, with one component for each derivative.

Similarly to the usual derivative, the gradient represents the slope of the tangent of the graph of the function. More precisely, the gradient points in the direction of the greatest rate of increase of the function and its magnitude is the slope of the graph in that direction.

So, the gradient points to the direction of greatest increase; keeping following the gradient, and you will reach the local maximum. (source)

Usefull video - The gradient function dy/dx.

Example

Consider a room in which the temperature is given by a scalar field, T, so at each point (x, y, z) the temperature is T(x, y, z) (we will assume that the temperature does not change over time.) At each point in the room, the gradient of T at that point will show the direction the temperature rises most quickly. The magnitude of the gradient will determine how fast the temperature rises in that direction.

Be careful not to confuse the coordinates and the gradient. The coordinates are the current location, measured on the x-y-z axis. The gradient is a direction to move from our current location, such as move up, down, left or right.

The gradient at any location points in the direction of greatest increase of a function. In this case, our function measures temperature. So, the gradient tells us which direction to move T to get a location with a higher temperature. Remember that the gradient does not give us the coordinates of where to go; it gives us the direction to move to increase our temperature.

Calculation

Let $f(x,y)=x^2y$. Find $\nabla f(3,2)$.

The gradient is just the vector of partial derivatives. The partial derivatives of f at the point $(x,y)=(3,2)$ are:

$\frac{df}{dx} (x,y) = 2xy$

$\frac{df}{dx} (3,2) =12$

$\frac{df}{dy} (x,y) = 2x^2$

$\frac{df}{dy} (3,2) =9$

Therefore, the gradient is $\nabla f(3,2) =(12,9)$

Let's calculate gradient of a function $f(x,y)=x^2y$ in R with numDeriv package.

# install.packages("numDeriv")

library("numDeriv")

# define function
myfunc <- function(lst) {lst[1]^2 * lst[2]}

# calculate