MATLAB: How to find the local slope of an array of data

slope

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.

Best Answer

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');