MATLAB: Max and Min Slopes

min and max slopes

Hi,
I am constructing two segments of cubic hermite functions. How can find the maximun and mininmum slopes of the curve and plot them graphically in the order of magnitude?
% first segment
%Inputs:
%first point (0,0); second point(5,150); thid points(10,0)
%derivatives: g0=g1=g2=0
x0=0; x1=5; g0=0; g1=0;
y0= 0; y1= 150;
A1 = [x0^3,x0^2,x0,1;
3*x0^2,2*x0,1,0;
x1^3,x1^2,x1,1;
3*x1^2,2*x1,1,0];
C1 = A1\[y0,g0,y1,g1]'; % Solve for the four cubic Hermite coefficients
n = 1000;
X1 = linspace(x0,x1,n);
Y1 = C1'*[X1.^3;X1.^2;X1;ones(1,n)];
% second segment
x2=10; y2= 0; g1= 0; g2=0;
A2 = [x1^3,x1^2,x1,1;
3*x1^2,2*x1,1,0;
x2^3,x2^2,x2,1;
3*x2^2,2*x2,1,0];
C2 = A2\[y1,g1,y2,g2]';
X2 = linspace(x1,x2,n);
Y2 = C2'*[X2.^3;X2.^2;X2;ones(1,n)];
plot(X1,Y1);hold on; plot(X2,Y2);title('cubic hermite'); xlabel('x'); ylabel('y');

Best Answer

One approach:
x0=0; x1=5; g0=0; g1=0;
y0= 0; y1= 150;
A1 = [x0^3,x0^2,x0,1;
3*x0^2,2*x0,1,0;
x1^3,x1^2,x1,1;
3*x1^2,2*x1,1,0];
C1 = A1\[y0,g0,y1,g1]'; % Solve for the four cubic Hermite coefficients
n = 1000;
X1 = linspace(x0,x1,n);
Y1 = C1'*[X1.^3;X1.^2;X1;ones(1,n)];
% second segment
x2=10; y2= 0; g1= 0; g2=0;
A2 = [x1^3,x1^2,x1,1;
3*x1^2,2*x1,1,0;
x2^3,x2^2,x2,1;
3*x2^2,2*x2,1,0];
C2 = A2\[y1,g1,y2,g2]';
X2 = linspace(x1,x2,n);
Y2 = C2'*[X2.^3;X2.^2;X2;ones(1,n)];
dY1 = gradient(Y1, X1(2)-X1(1)); % Calculate Gradient

[Y1max,idxY1(1)] = max(dY1);
[Y1min,idxY1(2)] = min(dY1);
dY2 = gradient(Y2, X2(2)-X2(1)); % Calculate Gradient
[Y2max,idxY2(1)] = max(dY2);
[Y2min,idxY2(2)] = min(dY2);
plot(X1,Y1)
hold on
plot(X2,Y2)
plot(X1(idxY1(1)), Y1(idxY1(1)), '^')
plot(X1(idxY1(2)), Y1(idxY1(2)), 'v')
plot(X2(idxY2(1)), Y2(idxY2(1)), '^')
plot(X2(idxY2(2)), Y2(idxY2(2)), 'v')
hold off
title('cubic hermite')
xlabel('x')
ylabel('y');
This uses the gradient (link) function to calculate the numerical derivative of the vectors, then finds the maxima and minima.
Experiment to get the result you want.