I am trying to create a script where I plot data and I need to find the slope of the most linear segment of the data/graph. Any ideas on how I can do that for a script whose data will vary for each trial.

댓글 수: 3

Are you refering to the vertical line at x=0?
Define "most linear". What segment is most linear in this plot below?
plot(round(sin(-4:.05:4)))
axis padded
When I say "most linear" I mean the initial segment that appears to be vertical but in reality just has a tiny slope. After the value "60" on the y-axis you can see how the line changes from "linear" to more parabolic, that's what I mean by "most linear". I'd like to find the range of points that have a constant slope.
Thanks
Adam Danz
Adam Danz 2021년 8월 30일
I'd compute the slope using gradient as Star Strider recommended. If you plot the results you'll see a relatively straight line at some high value and will be precipitously fall off at the point where the original data starts to call off. To quantitatively determine the endpoint of the 'straight' line, you could take the 2nd derivative (gradient of the gradient) to locate the first spike that crosses a threhold you set.

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

 채택된 답변

Star Strider
Star Strider 2021년 8월 30일
One reasonably straightforward way to determine the slope of a curve is to use the gradient function.
For example, the gradient of the hyperbolic tangent function:
x = linspace(-4, 4, 500);
f = tanh(x);
dfdx = gradient(f) ./ gradient(x);
figure
plot(x, f)
hold on
plot(x, dfdx)
hold off
grid
legend('Function','Derivative', 'Location','best')
Then use the gradient result to determine what parts of the curve have the characteristics you’re interested in.
.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Spline Postprocessing에 대해 자세히 알아보기

질문:

2021년 8월 30일

댓글:

2021년 8월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by