slope of non linear

조회 수: 14 (최근 30일)
Frederik Reese
Frederik Reese 2022년 8월 11일
편집: Bruno Luong 2022년 8월 24일
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
Jan
Jan 2022년 8월 11일
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 2022년 8월 24일
편집: Bruno Luong 2022년 8월 24일
"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.

댓글을 달려면 로그인하십시오.

답변 (3개)

Rohit
Rohit 2022년 8월 24일
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 2022년 8월 24일
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 2022년 8월 24일
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

카테고리

Help CenterFile Exchange에서 Smoothing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by