### 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).

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 . Find .

The gradient is just the vector of partial derivatives. The partial derivatives of f at the point are:

Let's calculate gradient of a function in R with numDeriv package.

# install.packages("numDeriv")

library("numDeriv")

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

# calculate 