find the slope of a smooth curve at a point

5 views (last 30 days)
I want to find the slop of the curve at [maxdepth01 , maxload01] and draw the slope tangent to the zero axis .
s = load('matlab needed.mat')
s = struct with fields:
A: [204196×1 double] B: [204196×1 double] maxdepth01: 1.9545e+03 maxload01: 4.5054e+05
B = s.B;
A = s.A;
maxdepth01 = s.maxdepth01;
maxload01 = s.maxload01;
plot(B , A , "r--")
hold on
plot(maxdepth01 ,maxload01 ,"*")
  2 Comments
Mathieu NOE
Mathieu NOE on 14 Dec 2022
hello
so we have 2 data sets , i see one is the raw data (needed.mat) and one is smoothed ('matlab needed.mat')
still it's not clear what tangent / slope you want at the [maxdepth01 , maxload01] point
this is an angular point so we can either speak of the slope on the LHS (horizontal section) or after this point (vertical portion)
soheil zare
soheil zare on 14 Dec 2022
Edited: Image Analyst on 14 Dec 2022
I have corrected the data. Overall I want the slope at the maximum load which is shown with a star like the picture below and the slope shown by a red line.

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 14 Dec 2022
hello
I tried to do the entire process from the raw data to the drawing of the tangent line
your amount of data is quite significant, I coud decimate this by a factor 100 without mpact on the result (just FYI)
the tangent line is drawn with xx, yy data
load('needed.mat')
samples = numel(depth01);
% decimation and smoothing
decim_factor = 100;
ind = 1:decim_factor:samples;
x = depth01(ind);
y = load01(ind);
xs = smoothdata(x,'gaussian',25);
ys = smoothdata(y,'gaussian',25);
% maxdepth01 ,maxload01 point detection = point of maximal distance to origin
[d_squared,ind] = max(xs.^2+ys.^2);
maxdepth01 = xs(ind);
maxload01 = ys(ind);
% slection of 33% of reamining samples after the maxdepth01 ,maxload01 point
ind2 = ind + (0:(numel(x)-ind)/20);
x2 = xs(ind2);
y2 = ys(ind2);
% Fit a polynomial p of degree "degree" to the (x,y) data:
degree = 1;
p = polyfit(x2,y2,degree);
% Evaluate the fitted polynomial p and plot the tangent line:
% find x coordinate crossing y = 0
x0 = -p(2)/p(1);
xx = linspace(x0,maxdepth01,25);
yy = polyval(p,xx);
eqn = poly_equation(flip(p)); % polynomial equation (string)
figure(1);
plot(depth01 , load01 , "r*", xs, ys,'k')
hold on
plot(maxdepth01, maxload01, "c*", 'LineWidth', 2, 'MarkerSize', 40)
plot(x2,y2,'db',xx,yy,'--')
legend('raw data','data downsampled & smoothed','peak point','polyfit data' , eqn)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function eqn = poly_equation(a_hat)
eqn = " y = "+a_hat(1);
for i = 2:(length(a_hat))
if sign(a_hat(i))>0
str = " + ";
else
str = " ";
end
if i == 2
% eqn = eqn+" + "+a_hat(i)+"*x";
eqn = eqn+str+a_hat(i)+"*x";
else
% eqn = eqn+" + "+a_hat(i)+"*x^"+(i-1)+" ";
eqn = eqn+str+a_hat(i)+"*x^"+(i-1)+" ";
end
end
eqn = eqn+" ";
end
  4 Comments
soheil zare
soheil zare on 20 Dec 2022
hello.thanks for your answer .
can you help me to write a power function for the smoothed data after loadmax(unloading / descending ) part?
and also what should I do to select for example 10% of the descending after passing 5% from the max instead of choosing 33% after loadmax
thanks
Mathieu NOE
Mathieu NOE on 20 Dec 2022
ok
the first part of the question is clear
but after it gets complicated especially at the end of a heavy work day
"10% of the descending after passing 5% from the max instead of choosing 33% after loadmax"
can you make a sketch where all those values are in the graph ? I am not sure to understand if we speak from x, y values and if we start from the max load point or from the end point ???

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 14 Dec 2022
Try this:
s = load('matlab needed.mat')
s = struct with fields:
A: [204196×1 double] B: [204196×1 double] maxdepth01: 1.9545e+03 maxload01: 4.5054e+05
B = s.B;
A = s.A;
maxdepth01 = s.maxdepth01;
maxload01 = s.maxload01;
subplot(2, 1, 1);
plot(B, A, "r--", 'LineWidth', 2);
hold on
plot(maxdepth01, maxload01, "r*", 'LineWidth', 2, 'MarkerSize', 20)
grid on;
fontSize = 12;
xlabel('B Depth', 'FontSize',fontSize);
ylabel('A Load', 'FontSize',fontSize);
title('Original Data', 'FontSize',fontSize);
% Find index of maxdepth01 on the x axis
[m, indexOfPeak] = min(abs(B - maxdepth01))
m = 0.1007
indexOfPeak = 130220
plot(B(1:indexOfPeak), A(1:indexOfPeak), "b-", 'LineWidth', 2);
% Find the angles from the peak point to the last point
angles = atan2d(A(indexOfPeak:end-1) - A(end), B(indexOfPeak:end-1) - B(end));
subplot(2, 1, 2);
plot(angles, 'b-', 'LineWidth', 2);
grid on;
title('Angle from last point to peak point', 'FontSize',fontSize);
xlabel('Index', 'FontSize',fontSize);
ylabel('Angle', 'FontSize',fontSize);
% It appears that the angle right at the peak point
% is the steepest of all the angles, so just report that.
angleAtPeak = max(angles)
angleAtPeak = 89.9384
% Get the slope there at the peak.
slope = tand(angleAtPeak)
slope = 930.0585

Categories

Find more on Polynomials in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!