MATLAB Answers

Calculate slope of a discrete points

277 views (last 30 days)
BeeTiaw
BeeTiaw on 8 Jan 2019
Commented: sisir regmi on 21 Apr 2019
Hi all,
Can anyone please help me to find the instantaneous gradient (slope) at each points from the following datasets.
data=[
% X Y
%===================
0.7761 0.5715
0.794 0.5729
0.8117 0.5744
0.8292 0.5762
0.8465 0.5782
0.8637 0.5804
0.8807 0.5828
0.8977 0.5853
0.9144 0.5879
0.9311 0.5907
0.9477 0.5937
0.9641 0.5968
0.9805 0.6
0.9967 0.6033
1.0129 0.6067
1.0289 0.6103
1.0449 0.6139
1.0608 0.6176
1.0767 0.6215
1.0924 0.6254
1.1081 0.6294
1.1238 0.6334
1.1393 0.6376
1.1548 0.6418
1.1703 0.6461
1.1857 0.6505
1.201 0.6549
1.2163 0.6593
1.2316 0.6639
1.2468 0.6685
1.2619 0.6731
1.277 0.6778
1.2921 0.6825
1.3071 0.6873
1.3221 0.6921
1.3371 0.697
1.352 0.7019
1.3669 0.7069
1.3818 0.7119
1.3966 0.7169
1.4114 0.722
1.4262 0.727
1.441 0.7322
1.4557 0.7373
1.4704 0.7425
1.4851 0.7477
1.4997 0.753
1.5143 0.7583
1.5289 0.7635
1.5435 0.7689
1.5581 0.7742
1.5726 0.7796
1.5872 0.785
1.6017 0.7904
1.6162 0.7958
1.6307 0.8013
1.6451 0.8067
1.6596 0.8122
1.674 0.8178
1.6884 0.8233
1.7028 0.8288
1.7172 0.8344
1.7315 0.84
1.7459 0.8456
1.7602 0.8512
1.7746 0.8568
1.7889 0.8624
1.8032 0.8681
1.8175 0.8737
1.8318 0.8794
1.8461 0.8851
1.8603 0.8908
1.8746 0.8965
1.8888 0.9023
1.9031 0.908
1.9173 0.9137
1.9315 0.9195
1.9457 0.9253
1.9599 0.931
1.9741 0.9368
1.9883 0.9426
2.0025 0.9484
2.0166 0.9543
2.0308 0.9601
2.0449 0.9659
2.0591 0.9718
2.0732 0.9776
2.0874 0.9835
2.1015 0.9893
2.1156 0.9952
2.1297 1.0011
2.1438 1.007
2.1579 1.0128
2.172 1.0187
2.1861 1.0247
2.2002 1.0306
2.2143 1.0365
2.2284 1.0424
2.2425 1.0483
2.2565 1.0543];
The plot of the dataset is shown below
I have calculated the slope based on the two neighboring points, the results is copied below.
the gradient (m) is calculated using the following formula:
and the angle is just the arc-tan of the result.
Now, is it possible to obtain the instantaneous slope (gradient) at each point without using any curve-fitting to find the equation of the dataset? How can we do this numerically?
out =[
% X Y m atan atandeg
%================================================================
0.7761 0.5715
0.794 0.5729 0.078212291 0.078053394 4.472130064
0.8117 0.5744 0.084745763 0.084543756 4.844000375
0.8292 0.5762 0.102857143 0.102496699 5.872628281
0.8465 0.5782 0.115606936 0.115096 6.594515032
0.8637 0.5804 0.127906977 0.127216217 7.288952324
0.8807 0.5828 0.141176471 0.14024961 8.035710711
0.8977 0.5853 0.147058824 0.146012258 8.365886124
0.9144 0.5879 0.155688623 0.154448697 8.849258471
0.9311 0.5907 0.167664671 0.166119551 9.517949161
0.9477 0.5937 0.180722892 0.178793055 10.24408745
0.9641 0.5968 0.18902439 0.186820161 10.70400675
0.9805 0.6 0.195121951 0.192700759 11.04094018
0.9967 0.6033 0.203703704 0.200954264 11.51383118
1.0129 0.6067 0.209876543 0.206873949 11.85300417
1.0289 0.6103 0.225 0.221314442 12.68038349
1.0449 0.6139 0.225 0.221314442 12.68038349
1.0608 0.6176 0.232704403 0.228635393 13.09984308
1.0767 0.6215 0.245283019 0.240534248 13.78159724
1.0924 0.6254 0.248407643 0.243479414 13.95034281
1.1081 0.6294 0.25477707 0.249469651 14.29355811
1.1238 0.6334 0.25477707 0.249469651 14.29355811
1.1393 0.6376 0.270967742 0.264613602 15.16124258
1.1548 0.6418 0.270967742 0.264613602 15.16124258
1.1703 0.6461 0.277419355 0.270614072 15.50504418
1.1857 0.6505 0.285714286 0.278299659 15.9453959
1.201 0.6549 0.287581699 0.280025283 16.04426686
1.2163 0.6593 0.287581699 0.280025283 16.04426686
1.2316 0.6639 0.300653595 0.292056315 16.73359422
1.2468 0.6685 0.302631579 0.293869335 16.83747263
1.2619 0.6731 0.304635762 0.295704342 16.94261077
1.277 0.6778 0.311258278 0.30175322 17.28918598
1.2921 0.6825 0.311258278 0.30175322 17.28918598
1.3071 0.6873 0.32 0.309702945 17.74467163
1.3221 0.6921 0.32 0.309702945 17.74467163
1.3371 0.697 0.326666667 0.315738603 18.09048937
1.352 0.7019 0.32885906 0.317718318 18.20391871
1.3669 0.7069 0.33557047 0.323762624 18.55023193
1.3818 0.7119 0.33557047 0.323762624 18.55023193
1.3966 0.7169 0.337837838 0.325799115 18.66691427
1.4114 0.722 0.344594595 0.331851221 19.01367439
1.4262 0.727 0.337837838 0.325799115 18.66691427
1.441 0.7322 0.351351351 0.337878188 19.35899418
1.4557 0.7373 0.346938776 0.333945072 19.13364321
1.4704 0.7425 0.353741497 0.340004105 19.48080023
1.4851 0.7477 0.353741497 0.340004105 19.48080023
1.4997 0.753 0.363013699 0.348220949 19.95159069
1.5143 0.7583 0.363013699 0.348220949 19.95159069
1.5289 0.7635 0.356164384 0.342155885 19.60408815
1.5435 0.7689 0.369863014 0.354259423 20.29756977
1.5581 0.7742 0.363013699 0.348220949 19.95159069
1.5726 0.7796 0.372413793 0.356501385 20.42602476
1.5872 0.785 0.369863014 0.354259423 20.29756977
1.6017 0.7904 0.372413793 0.356501385 20.42602476
1.6162 0.7958 0.372413793 0.356501385 20.42602476
1.6307 0.8013 0.379310345 0.362544237 20.77225468
1.6451 0.8067 0.375 0.35877067 20.55604522
1.6596 0.8122 0.379310345 0.362544237 20.77225468
1.674 0.8178 0.388888889 0.370891289 21.25050551
1.6884 0.8233 0.381944444 0.364845007 20.90407908
1.7028 0.8288 0.381944444 0.364845007 20.90407908
1.7172 0.8344 0.388888889 0.370891289 21.25050551
1.7315 0.84 0.391608392 0.373251365 21.38572793
1.7459 0.8456 0.388888889 0.370891289 21.25050551
1.7602 0.8512 0.391608392 0.373251365 21.38572793
1.7746 0.8568 0.388888889 0.370891289 21.25050551
1.7889 0.8624 0.391608392 0.373251365 21.38572793
1.8032 0.8681 0.398601399 0.379300105 21.73229519
1.8175 0.8737 0.391608392 0.373251365 21.38572793
1.8318 0.8794 0.398601399 0.379300105 21.73229519
1.8461 0.8851 0.398601399 0.379300105 21.73229519
1.8603 0.8908 0.401408451 0.381719969 21.87094317
1.8746 0.8965 0.398601399 0.379300105 21.73229519
1.8888 0.9023 0.408450704 0.387770172 22.21759427
1.9031 0.908 0.398601399 0.379300105 21.73229519
1.9173 0.9137 0.401408451 0.381719969 21.87094317
1.9315 0.9195 0.408450704 0.387770172 22.21759427
1.9457 0.9253 0.408450704 0.387770172 22.21759427
1.9599 0.931 0.401408451 0.381719969 21.87094317
1.9741 0.9368 0.408450704 0.387770172 22.21759427
1.9883 0.9426 0.408450704 0.387770172 22.21759427
2.0025 0.9484 0.408450704 0.387770172 22.21759427
2.0166 0.9543 0.418439716 0.396300932 22.70637084
2.0308 0.9601 0.408450704 0.387770172 22.21759427
2.0449 0.9659 0.411347518 0.390250283 22.35969418
2.0591 0.9718 0.415492958 0.39379062 22.56254053
2.0732 0.9776 0.411347518 0.390250283 22.35969418
2.0874 0.9835 0.415492958 0.39379062 22.56254053
2.1015 0.9893 0.411347518 0.390250283 22.35969418
2.1156 0.9952 0.418439716 0.396300932 22.70637084
2.1297 1.0011 0.418439716 0.396300932 22.70637084
2.1438 1.007 0.418439716 0.396300932 22.70637084
2.1579 1.0128 0.411347518 0.390250283 22.35969418
2.172 1.0187 0.418439716 0.396300932 22.70637084
2.1861 1.0247 0.425531915 0.402321098 23.05130092
2.2002 1.0306 0.418439716 0.396300932 22.70637084
2.2143 1.0365 0.418439716 0.396300932 22.70637084
2.2284 1.0424 0.418439716 0.396300932 22.70637084
2.2425 1.0483 0.418439716 0.396300932 22.70637084
2.2565 1.0543 0.428571429 0.404891786 23.19859051];

  1 Comment

sisir regmi
sisir regmi on 21 Apr 2019
How did you get the plot as you have just two arrays of x and y points

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 8 Jan 2019
Edited: Star Strider on 8 Jan 2019
I would use the gradient (link) function. Since the data need to be sampled regularly with respect to the independent variable (your data are not), you need to interpolate.
Try this:
data1 = linspace(min(data(:,1)), max(data(:,1)), size(data,1));
data2 = interp1(data(:,1),data(:,2),data1);
g = gradient(data2, diff(data1(1:2)));
figure
plot(data1, data2, data1, g)
grid
legend('data', 'gradient', 'Location','NW')
EDIT —
Data and calculated slope plot:
Calculate slope of a discrete points - 2019 01 08.png

  3 Comments

sisir regmi
sisir regmi on 21 Apr 2019
Can you please plot the x and y data finding the slopes of the points below? thank you
x=[0.06,.155,.180,.25,.28,.418,.554,1.737,1.822,1.823,1.824,2.029,2.062,2.073,2.075,2.091,2.108,2.11,2.157,3.12,3.26,3.37,3.44,3.54,3.56,3.58,3.92,4.08,4.17,4.19,4.36,4.38,4.39,4.45,4.456,4.44,4.46,4.35,4.55,4.69,5.138,5.28,5.40,5.54,5.67,5.78,5.80,5.81,5.900,5.9001,5.91,5.92,6.19,6.20,6.21,6.423,6.57,6.65,6.71,6.72,6.83,6.85,6.92,7.11,7.25]
y=[13.22,179.6,438.2,687.8,981.3,1152,931,91.24,80.37,37.61,12.09,70.55,96.34,125.6,73.59,57.15,88.75,80.1,111.6,258.1,40.66,42.7,60.61,236.8,239.8,212.1,128.4,46.09,40.58,47.62,90.86,174.5,244.2,316.9,482,614.4,775.3,923.3,782.3,486.9,75.65,35.83,31.34,35.01,28.84,36.18,31.63,16.6,11.6,4.97,11.38,10.54,9.34,13.16,21.86,28.17,73.86,148.5,469.1,763.6,914.6,301.9,192,118.5,79.65,49.06]
Image Analyst
Image Analyst on 21 Apr 2019
Your vectors are not the same length. But the instantaneous slopes would be
slopes = diff(y) ./ diff(x)
sisir regmi
sisir regmi on 21 Apr 2019
0.0600 13.22
0.1550 179.6
0.1800 438.2
0.2500 687.8
0.2800 981.3
0.4180 1152
0.5540 931
1.7370 91.24
1.8220 80.37
1.8230 37.61
1.8240 12.09
1.8250 70.55
2.0290 96.34
2.0620 125.6
2.0730 73.59
2.0750 57.15
2.0910 88.75
2.1080 80.1
2.1100 111.6
2.1570 258.1
3.1200 40.66
3.2600 42.7
3.3700 60.61
3.4400 236.8
3.5400 239.6
3.5600 212.1
3.5800 128.4
3.9200 46.09
4.0800 40.58
4.1700 47.02
4.1900 90.86
4.3600 174.5
4.3800 244.2
4.3900 316.9
4.4500 482
4.4560 614.4
4.4400 775.3
4.4600 923.3
4.3500 782.3
4.5500 486.9
4.6900 75.65
5.1380 35.83
5.2800 31.34
5.4000 35.01
5.5400 28.84
5.6700 36.18
5.7800 31.63
5.8000 16.6
5.8100 11.26
5.9000 4.97
5.9001 11.38
5.9100 10.54
5.9200 9.34
6.1900 13.16
6.2000 21.86
6.2100 28.86
6.4230 738.6
6.5700 148.5
6.6500 469.1
6.7100 763.6
6.7200 914.6
6.8300 301.9
6.8500 192
6.9200 118.5
7.1100 79.65
7.2500 49.06
now its the same length but when i give the command for slope it says matrix dimension must agree

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 8 Jan 2019
Try diff:
slopes = diff(out, 1)

  3 Comments

BeeTiaw
BeeTiaw on 8 Jan 2019
I don't understand your answer. Which column of out are you referring to?
Please see the plot attached in the question.
The function diff will only give you the delta between two neighboring points, thus not the slope of the curve.
Image Analyst
Image Analyst on 8 Jan 2019
It does it for all columns. The delta between two indexes IS the slope between two indexes (on the right side). Maybe the gradient function Star suggests does it on both sides and takes the average - I don't know, look it up.
BeeTiaw
BeeTiaw on 8 Jan 2019
I understand that, but it has nothing to do with slope of the curve. Please see the plot and the question.
It seems that you misunderstood the question here. But thanks for responding ;)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by