Locating point on x-axis where the slope is decreasing

Dear all,
I used the following code to plot the angle over time:
figure
hold on
grid on
plot(Time, theta,'linewidth',1.5)
xlabel('Time [s]','FontWeight','bold')
ylabel('\theta [deg]','FontWeight','bold')
xlim([0 round(Time(end))])
legend('Opening angle','Location','northeast')
title('Opening angle')
The resulting graph is shown below. I want to index at what time the slope is decreasing. How can I do that?
The variables Time and theta to plot this graph can be found in the data.mat file.

 Accepted Answer

A reasonably robust way is to use the ischange function:
D = load('data.mat');
Time = D.Time;
theta = D.theta;
[TF,m,b] = ischange(theta, 'linear', 'Threshold',5);
chgpts = [find(TF); m(TF); b(TF)].';
chgidx = find(chgpts(:,2)<0,1);
figure
plot(Time, theta,'linewidth',1.5)
hold on
plot(Time(chgpts(chgidx(:,1))), theta(chgpts(chgidx(:,1))), 'xr')
hold off
grid on
xlabel('Time [s]','FontWeight','bold')
ylabel('\theta [deg]','FontWeight','bold')
xlim([0 round(Time(end))])
legend('Opening angle','Location','northeast')
title('Opening angle')
producing:
The ‘Time’ value is then:
ChangePointTime = Time(chgpts(chgidx(:,1)));
that here is 6.01 seconds.

4 Comments

Thank you this solution worked. However, I don't understand the threshold of 5. What does that mean?
And is this also possible to execute for locating the point at the end of the slope (around 11.5 seconds)?
My pleasure!
However, I don't understand the threshold of 5. What does that mean?
The 'Threshold',5 is the threshold value that worked. Like much in data analysis, this is arrived at through experimentation.
‘And is this also possible to execute for locating the point at the end of the slope (around 11.5 seconds)?
Yes. Revising the code slightly to detect it and demonstrate both values:
D = load('data.mat');
Time = D.Time;
theta = D.theta;
[TF,m,b] = ischange(theta, 'linear', 'Threshold',5);
chgpts = [find(TF); m(TF); b(TF)].';
chgidx1 = find(chgpts(:,2)<0,1);
[~,chgidx2] = min(chgpts(:,3));
figure
plot(Time, theta,'linewidth',1.5)
hold on
plot(Time(chgpts(chgidx2(:,1))), theta(chgpts(chgidx2(:,1))), '+r')
plot(Time(chgpts(chgidx1(:,1))), theta(chgpts(chgidx1(:,1))), 'xr')
hold off
grid on
xlabel('Time [s]','FontWeight','bold')
ylabel('\theta [deg]','FontWeight','bold')
xlim([0 round(Time(end))])
legend('Opening angle','Location','northeast')
title('Opening angle')
ChangePointTime(1) = Time(chgpts(chgidx1(:,1)))
ChangePointTime(1) = Time(chgpts(chgidx2(:,1)))
FirstChange = [Time(chgpts(chgidx1(:,1))), theta(chgpts(chgidx1(:,1)))];
LastChange = [Time(chgpts(chgidx2(:,1))), theta(chgpts(chgidx2(:,1)))];
text(FirstChange(1), FirstChange(2), sprintf('\\uparrow\n(%6.2f, %6.2f)',FirstChange), 'HorizontalAlignment','right', 'VerticalAlignment','top')
text(LastChange(1), LastChange(2), sprintf('(%6.2f, %6.2f)\n\\downarrow',LastChange), 'HorizontalAlignment','left', 'VerticalAlignment','bottom')
producing:
That should do what you want.
The actual values are in ‘FirstChange’ and ‘LastChange’. This code may need to be revised slightly to work with other data sets, however it should get you started.
Thank you so much for the code and explanation!

Sign in to comment.

More Answers (1)

Bascially you want to get the index when the change in theta becomes negative:
dtheta = diff(theta);
index = find(dtheta<0,1);%the 1 refers to the first entry where the statement is true

Categories

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!