# Numerical first derivative of irregularly spaced data

10 views (last 30 days)

Show older comments

##### 0 Comments

### Accepted Answer

Matt J
on 11 Dec 2023

Edited: Matt J
on 11 Dec 2023

It seems to work:

t=sort( rand(1,1000)*2*pi );

cos_t=gradient(sin(t))./gradient(t);

I=1:20:1000;

plot(t,cos(t),'b--' , t(I), cos_t(I) ,'o'); legend('True','Finite Difference',Location='southeast')

##### 6 Comments

Star Strider
on 11 Dec 2023

The single gradient call using both vectors is new, and does not appear to be documented as a change.

In earlier versions (perhaps as recently as five years ago), gradient used the first element of the second argument vector (or perhaps the difference between the first and second elements), giving anomalous results. That required dividing the gradient of the dependent variable vector by the gradient of the independent variable vector to get an acceptable result. I just now compared them (using a vector that was not close to being regularly-spaced), and it appears to consider the entire vector, since:

gradient(x,t)

and:

gradient(x) ./ gradient(t)

now give the same result.

I wonder when the change occurred? It would be nice it that were added to the documentation.

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!