Slope along the curve

1 view (last 30 days)
Tsung-Sheng Kang
Tsung-Sheng Kang on 4 Feb 2015
Edited: per isakson on 13 Apr 2016
Hi, I have a set of data(X vs Y), which looks smooth to me. I tried to extract the slope along this curve. Both the data and the code are attached.
Therefore, I tried to calculate the slope in the following way:
[Y(i+3)-Y(i-3)]/[X(i+3)-X(i-3)]
All the slopes are collected and to be drew into another curve. However, this curve has serious oscillation. I think it is because that, although the curve of X vs Y looks smooth, there are always consecutive 3 to 5 data has the same Y.
Can anyone help me? thanks....
  2 Comments
Image Analyst
Image Analyst on 4 Feb 2015
Not really unless you attach data or screenshots or both. If the y are the same for stretches, then you'd just have zero slope there in that flat stetch - nothing wrong or oscillatory about that.
Tsung-Sheng Kang
Tsung-Sheng Kang on 5 Feb 2015
Hi Sorry for not attaching the data and the code.
Here I attached the code I used, only this time the way I calculate the slope is (Y(i+3)-Y(i-3))/(X(i+3)-X(i-3)). Also, the data is provided. The first column is X, while the 2nd is Y.
Can you please check for me? thanks.

Sign in to comment.

Answers (4)

Roger Stafford
Roger Stafford on 4 Feb 2015
If the X values are not uniformly-spaced, the expression
(Y(i+1)-Y(i-1))/(X(i+1)-X(i-1))
is not a very accurate approximation to the derivative, dY/dX, at i. Here is a second-order method that, provided the Y values are not too noisy, should be more accurate. For up to a second-order polynomial (parabola) it is exact, even at the two end points.
Xe = [X(3);X;X(end-2)];
Ye = [Y(3);Y;Y(end-2)];
dX = diff(Xe);
dY = diff(Ye);
DYDX = (dY(1:end-1)./dX(1:end-1).*dX(2:end)+...
dY(2:end)./dX(2:end).*dX(1:end-1))./(Xe(3:end)-Xe(1:end-2));
The quantity DYDX is your approximate derivative (slope.)
(Note: In computing Xe and Ye I have assumed that X and Y are column vectors. Use commas in Xe and Ye expressions if they are row vectors.)
  1 Comment
Tsung-Sheng Kang
Tsung-Sheng Kang on 5 Feb 2015
Hi I am new to Matlab, and I don't know how to implant your method into my code.
Here I attached the code I used, only this time the way I calculate the slope is (Y(i+3)-Y(i-3))/(X(i+3)-X(i-3)). Also, the data is provided. The first column is X, while the 2nd is Y.
Can you please check for me? thanks.

Sign in to comment.


John D'Errico
John D'Errico on 5 Feb 2015
Edited: John D'Errico on 5 Feb 2015
As Roger points out, the ratio of first order differences is a common way to approximate a first derivative, but that it is not a terribly good estimator, especially when you have a sequence of constant steps.
In fact, this is a common problem for people who work in imaging science, to deal with what are called shapers. These are often smooth functions that are then quantized by some rounding scheme, but then one sometimes wishes to perform operations on a smooth curve that approximates that shaper. The issue is, any type of interpolant like a spline will produce useless stuff.
x0 = (0:.1:5)'.^2;
x1 = round(x0);
[x0,x1]
ans =
0 0
0.01 0
0.04 0
0.09 0
0.16 0
0.25 0
0.36 0
0.49 0
0.64 1
0.81 1
1 1
1.21 1
1.44 1
1.69 2
1.96 2
2.25 2
2.56 3
2.89 3
3.24 3
3.61 4
4 4
4.41 4
4.84 5
5.29 5
5.76 6
6.25 6
6.76 7
7.29 7
7.84 8
8.41 8
9 9
9.61 10
10.24 10
10.89 11
11.56 12
12.25 12
12.96 13
13.69 14
14.44 14
15.21 15
16 16
16.81 17
17.64 18
18.49 18
19.36 19
20.25 20
21.16 21
22.09 22
23.04 23
24.01 24
25 25
Given this problem, many years ago, I developed ideas for unrounding tools. I.e., find the smoothest function that approximates the data in a way that is consistent with rounding. One could also build unfix or unceil tools in a similar way.
This particular unround tool I wrote uses quadprog to do the estimation, in a very simple way. I've written it several times, with various capabilities.
X2 = unround(x1);
[x0,x1,x2]
ans =
0 0 -0.5
0.01 0 -0.37198
0.04 0 -0.24233
0.09 0 -0.10944
0.16 0 0.028307
0.25 0 0.17254
0.36 0 0.32489
0.49 0 0.48696
0.64 1 0.66038
0.81 1 0.84678
1 1 1.0478
1.21 1 1.265
1.44 1 1.5
1.69 2 1.7545
1.96 2 2.0285
2.25 2 2.322
2.56 3 2.635
2.89 3 2.9676
3.24 3 3.3199
3.61 4 3.6917
4 4 4.0833
4.41 4 4.4945
4.84 5 4.9254
5.29 5 5.3761
5.76 6 5.8465
6.25 6 6.3367
6.76 7 6.8465
7.29 7 7.3761
7.84 8 7.9254
8.41 8 8.4945
9 9 9.0833
9.61 10 9.6917
10.24 10 10.32
10.89 11 10.968
11.56 12 11.635
12.25 12 12.322
12.96 13 13.028
13.69 14 13.754
14.44 14 14.5
15.21 15 15.265
16 16 16.048
16.81 17 16.847
17.64 18 17.66
18.49 18 18.487
19.36 19 19.325
20.25 20 20.173
21.16 21 21.028
22.09 22 21.891
23.04 23 22.758
24.01 24 23.628
25 25 24.5
I've attached unround.m to this answer, but please, don't expect much of it. The code is not terribly sophisticated. A better code would include the capability for bound constraints, and perhaps monotonicity, etc. As well, it is not terribly fast, taking several seconds to solve the problem above.
Another solution that might be appropriate is to eliminate the replicate y values, averaging x for each such fixed step. Then use a spline to smoothly interpolate those averaged points. You could use my consolidator tool (on the file exchange) to do the x averaging step.

Tsung-Sheng Kang
Tsung-Sheng Kang on 5 Feb 2015
Hi Sorry for not attaching the data and the code.
Here I attached the code I used, only this time the way I calculate the slope is (Y(i+3)-Y(i-3))/(X(i+3)-X(i-3)). Also, the data is provided. The first column is X, while the 2nd is Y.
Can you guys please check for me? thanks.
Rodger Thanks for the reply, but I cannot make the code provided work...probably because I did not use it in the right way.
John My vector is not integer, so I am afraid that I cannot use your code. But still thanks.

Torsten
Torsten on 5 Feb 2015

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!