
How do I find the local slope of an array of data?
31 views (last 30 days)
Show older comments
I have an array of data which is linear initially and then changes to a curve (e.g. a stress-strain curve for a material). I created a tangent to the linear part and I want to detect when the slope of the "real" curve, i.e. data array, changes more than 10% from the slope of the fitted tangent line. I know that the curve will always have a slope that is smaller than the fitted line.
thanks in advance.
0 Comments
Accepted Answer
Image Analyst
on 8 Mar 2016
One way is to get a spline fit, then use diff() to get the slopes. Here is the code:
% Demo to show spline interpolation.
% Clean up / initialize
clc;
close all;
clear all;
workspace; % Display workspace panel.
% Create the original knot points.
lengthX = 10;
x = 1:lengthX;
y = rand (lengthX,1);
% Plot it and show how the line has sharp bends.
plot(x, y, '-sr', 'LineWidth', 2);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Use splines to interpolate a smoother curve,
% with 10 times as many points,
% that goes exactly through the same data points.
samplingRateIncrease = 10;
newXSamplePoints = linspace(1, lengthX, lengthX * samplingRateIncrease);
smoothedY = spline(x, y, newXSamplePoints);
% Plot smoothedY and show how the line is
% smooth, and has no sharp bends.
hold on; % Don't destroy the first curve we plotted.
plot(newXSamplePoints, smoothedY, '-ob');
title('Spline Interpolation Demo', 'FontSize', 20);
legend('Original Points', 'Spline Points');
% Mathworks Demo code from their Help
% x = 0:10;
% y = sin(x);
% xx = 0:.25:10;
% yy = spline(x,y,xx);
% plot(x,y,'o',xx,yy)
slopes = [0, diff(smoothedY)];
plot(newXSamplePoints, slopes, 'k-', 'LineWidth', 3);
% Draw x axis
line(xlim, [0,0], 'Color', 'k', 'LineWidth', 2);
grid on;
legend('Original Points', 'Spline Points', 'Slope');

4 Comments
Mashud Rana
on 19 Feb 2018
Edited: Image Analyst
on 19 Feb 2018
In the mathworks demo code example at the end, If the x values are in unequal interval (let say x=[1 3.4 5.7 6 7.2 10 15 22 30 50]), then should the equation for calculating slopes look like:
slopes = [0, diff(smoothedY)./diff(xx)];
or it is still same as below:
slopes = [0, diff(smoothedY)];
Lastly, why should we need 10 times extra points when fitting spline to data?
Image Analyst
on 19 Feb 2018
It would be the first one I believe. But you might want to get the average slope on each side of the point and average them, or if you can get the cubic coefficients from the spline function at each interval, then you can get the exact analytical slope at any point using the derivative (recall your basic calculus).
I just took 10 so that there would be some markers on the curve. The curve will be the same regardless of how many points you pick to evaluate it at. In other words, if you pick 1 or 5 or 10 or 100, it doesn't matter - they will all fall on the curve.
More Answers (0)
See Also
Categories
Find more on Spline Postprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!