# MATLAB gradient derivative troubleshooting

I have an array $A$ having the next 145 values

I would like to calculate the $\frac{dA}{dX}$, having a 1D grid, $x$: 1:286:41468

I use the function gradient:

DA_DX = gradient(A, 41468/145)

I am trying to understand why the $\frac{dA}{dX}$ output graph has peaks which cause problems to my code. Please, do you see something wrong in my steps? Is any other way to calculate the $\frac{dA}{dX}$ without gradient function?

## 1 Answer

You can calculate the derivative using the diff function and then dividing by the time interval between the "samples" in your vector like this:

dA = diff(A) / 286

The diff() function calculates the difference between an element and the previous one in the vector. With this you'll be calculating the derivative for each element in the vector as the increment in A divided by the increment in x. Check out the help for the diff() for more info.

• the problems is in the region that I have local min and max, so the dA/dx becomes almost zero – user1640255 Nov 18 '13 at 0:41
• I don't understand what the problem is. When you have a maximum, minimum or an inflection point the derivative should be zero, or close to zero. What do you mean when you say (in your question) that you have peaks that cause problems to your code? You are calculating the derivative the correct way. – Pedro G. Nov 18 '13 at 11:31
• can I sent you the output pictures? Look I calculate the velocity u of a 2nd order nonlinear equation. In two regions u is almost flat. A is the derivative f of the u derivative. The first derivative takes in these regions two small values, and the second derivative have not logical values in these regions, is like noise. – user1640255 Nov 19 '13 at 2:31
• Yes, some pictures would help because it is hard to understand your problem without looking at the actual data. You can edit your question and add links to the output pictures. Upload them to an image hosting service like tinypic.com and just paste the link in your question. – Pedro G. Nov 19 '13 at 15:21