# Compute the gradient of a quantiy on a grid

• matteo86bo
In summary, to compute the gradient of a quantity on a grid, you can use the "5-point finite difference approximation" or extend the formula to take into account the "diagonal" of the cube.
matteo86bo
Hi, this might be a stupid question, but I was wondering how to computer the gradient of a quantity on a grid. I mean I have a grid made of cells (not necessarily of the same size) where the variable $\rho$ is defined at the center of every cell. How can I compute the gradient of this quantity?
It is wrong doing this:
$\nabla\rho(i)=\frac{\rho(i)}{\Delta(i)}$
where $\Delta(i)$ is the size of cell i.
I can't come up with other methods ...

I assume you are talking about a two-dimensional grid. The length of the gradient at each point is the fastest rate of increase over all directions and its direction is the direction in which you have that fastest increase. Assuming a rectangular grid so that each point has four immediate neighbors, each point has only four "neighbors" so you can only calculate four derivatives (difference between values at neighboring points divided by distance between those two points). Determine which of those is largest, take the value as the length of the gradient and its direction as the direction from the point to that neighbor (so there are only four possible directions.

You can extend that slightly by also using the "secondary" neighbors, lying on the "diagonal" of a grid square, giving 8 possible direction. You will have to take the longer diagonal distance into account.

I didn't understand all you said ... I think I found the solution but I'm still a little bit concerned.

I need to computer a 3D gradient and for every direction I can use the "5-point finite difference approximation" of the gradient...

$$(\nabla\rho)_x=\frac{4}{3}\frac{\rho_{i+1}-\rho_{i-1}}{2\Delta x}-\frac{1}{3}\frac{\rho_{i+2}-\rho_{i-2}}{4\Delta x}$$

But how can I take into account the "diagonal" of the cube? The above formula compute the gradient only for one specific directorion ... I would like to know if there exists a formula to compute it by taking into account all the 27 neighbouring cells ...

## 1. What is a gradient on a grid?

A gradient on a grid is a mathematical concept that represents the rate of change of a quantity with respect to its surrounding points. It is typically used to describe the direction and magnitude of the change in a quantity across a grid or surface.

## 2. How is the gradient of a quantity on a grid computed?

The gradient of a quantity on a grid is computed by taking the partial derivative of the quantity with respect to the x-coordinate and the y-coordinate of each point on the grid. These partial derivatives are then combined to form a vector that represents the direction and magnitude of the gradient at each point.

## 3. Why is computing the gradient on a grid important?

Computing the gradient on a grid is important because it allows us to understand how a quantity is changing across a grid or surface. This information can be used in various fields such as physics, engineering, and computer graphics to analyze and model complex systems.

## 4. What are some real-world applications of computing the gradient on a grid?

Some real-world applications of computing the gradient on a grid include weather forecasting, terrain analysis, and image processing. It is also commonly used in machine learning and optimization algorithms to find the direction of steepest descent.

## 5. Are there any limitations to computing the gradient on a grid?

Yes, there are some limitations to computing the gradient on a grid. The accuracy of the gradient depends on the spacing of the points on the grid, so a denser grid will provide a more accurate representation of the gradient. Additionally, the gradient may not be well-defined at certain points on the grid, such as sharp edges or corners.

• Calculus
Replies
18
Views
2K
• Calculus
Replies
3
Views
2K
• Calculus
Replies
5
Views
1K
• General Math
Replies
5
Views
1K
• Calculus and Beyond Homework Help
Replies
4
Views
499
• Calculus
Replies
3
Views
2K
• Calculus
Replies
1
Views
3K
• Calculus and Beyond Homework Help
Replies
8
Views
747