slope of non linear

6 views (last 30 days)
Frederik Reese
Frederik Reese on 11 Aug 2022
Edited: Bruno Luong on 24 Aug 2022
Hi, I have this plot in blue and I calculate the mean of the slope of the last 19 points befor the black line. And than I dram a constant line with that slope in red. The slope looks fine, but is my coding / mathematical thinking right to get the mean slope?
I use the mean of the second diff(...,2) of the last 19 y values to get the mean dy and than I divide it with the dx.
Am I right in using the second diff?
Here is my code:
for i= 1:size(FRI_HQ10000_nonan,1) %nur FRI- Punkte, bei denen auch Überlauf stattfindet und im Abschnitt endet
X(i,:)= [1:10055];
if isnan(HQ10000_Zeit_Flutende_nonan(i,:))== 1 | HQ10000_Zeit_Flutende_nonan(i,:)==0
HQ10000_slope(i,:)= nan;
else
% end slope calculation on last 20/ 19 Werte
HQ10000_dx_mean(i,:) = mean(diff(X(i,:))); % Average or mean value of array; Differences and approximate derivatives
FRI_HQ10000_end (i,:) = FRI_HQ10000_nonan(i,(HQ10000_Zeit_Flutende_nonan(i,:)-20):HQ10000_Zeit_Flutende_nonan(i,:)-1); % last 20 samples
HQ10000_dy_mean(i,:) = mean(diff(FRI_HQ10000_end(i,:),[],2),2); % diff on 2nd dimension , mean on 2nd dimension
HQ10000_slope(i,:) = HQ10000_dy_mean(i,1)./HQ10000_dx_mean(i,1); % by definition
end
end
Any comments?
Thanks
  2 Comments
Jan
Jan on 11 Aug 2022
The mean of the gradient of a noisy curve can be dominated by noise.
It is more stable to extimate a line through the points and use its slope. See polyfit(x, 1)
Bruno Luong
Bruno Luong on 24 Aug 2022
Edited: Bruno Luong on 24 Aug 2022
"Any comments?"
You compute somewhat
mean(diff(dy))/mean(diff(dx))
is equal to
(y(end)-y(1))/(x(end)-x(1))
So you actually compute the slope of the line that connects the two end points.

Sign in to comment.

Answers (3)

Rohit
Rohit on 24 Aug 2022
Hi,
As suggested above you can use the “polyfit” function to predict the line fitted for the last 19 points before the black line. You can refer to the below documentation link along with MATLAB Answer link which might prove helpful.
In case of any further doubts, you can contact MathWorks Technical Support.

John D'Errico
John D'Errico on 24 Aug 2022
Are you right to use a second order difference? NO. That is not the slope, but something proportional to the SECOND derivative. However, it looks like you are computing a difference on the second dimension. That would be ok. However, even then, I would make a different choice.
I might compute the slope, using gradient, better than diff. But now take the median of the slope estimates generate at those 19 points. This will reduce any noise in the estimation due to junk in the curve. Or, you could use a trimmed mean, where you take the mean of the central 50% of the slopes generated.

Bruno Luong
Bruno Luong on 24 Aug 2022
One of the most robust estimation of slope (in 2D) is using SVD
% Fake data
x = 10*rand(1,19);
y = 2 + 3*x;
x = x + 0.1*randn(size(x));
y = y + 0.1*randn(size(y));
% Estimate slope dy/dx
xy=[x(:) y(:)];
svd(xy-mean(xy,1),0);
[~,~,V]=svd(xy-mean(xy,1),0);
estimateslope = V(2,1)/V(1,1)
estimateslope = 3.0092

Categories

Find more on Mathematics 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!