MATLAB Answers

## am trying to find the first and second derivates of a set of data points I've imported

Asked by M

### M (view profile)

on 24 Apr 2019
Latest activity Answered by dpb

### dpb (view profile)

on 24 Apr 2019
Hi,
I am trying to find the first and second derivates of a set of data points I've imported. I tried using the diff function but that just gave me an error. Also how would i create two seperate plots for each differentiation operation?
Thank you

KSSV

### KSSV (view profile)

on 24 Apr 2019
What/How is the data you have in csv file?
dpb

### dpb (view profile)

on 24 Apr 2019
Undefined function 'diff' for input arguments of type 'table'.
You insist on making it difficult by not providing sufficient information but M is the name of the overall table -- you've got to reference whatever particular variable it is out of the table to operate on...something like
dy=diff(M.X);
where X is the name of the variable you want to take difference of. Of, course, to actualy compute a derivative, you need the difference of both the displacement and time vectors if it were the velocity trying to compute--
v=diff(M.x)./diff(M.t);
M

### M (view profile)

on 24 Apr 2019
i didnt know i could attach a file im not intending to make it difficult. i only have one row of data and i need the first and second derivatives of the data

Sign in to comment.

## 1 Answers

Answer by dpb

### dpb (view profile)

on 24 Apr 2019

That's essentially trivial file that
data=csvread('data_10.csv');
data(:,2)=[nan;diff(data(:,1))]; % first difference
data(:,3)=[nan(2,1); diff(data(:,1),2)]; % second difference
will leave you with an array of three columns for data, first and second differences, respectively. The difference vectors are each one element shorter than the previous of course, so I used NaN as filler so plotting routines will ignore them silently. Since they're columns, simply
plot(data)
will plot all three on one axes; you can select columns as desired and/or use two axes or whatever for aesthetics as desired.

#### 0 Comments

Sign in to comment.