How to find the slope of a linear area
조회 수: 104 (최근 30일)
이전 댓글 표시
how do i find the slope of the linear area, someone knows the code?
답변 (3개)
Hassaan
2023년 12월 25일
The slope of a line on a graph represents the rate of change of the y-variable with respect to the x-variable. For linear relationships, the slope is constant, and for curves, the slope can be determined at any given point using calculus, specifically the first derivative.
To find the slope of a curve in a specific region that appears linear, you can fit a straight line to that region using linear regression and then take the coefficient of the x-variable as the slope.
In MATLAB, you could use the polyfit function to fit a line to the data points in the region of interest. Generates dummy data, assumes a linear region, performs linear regression on that region, and plots the results.
% Clear workspace and figures
clear; close all; clc;
% Generate dummy data
x_data = linspace(-0.15, 0.15, 100);
y_data = 2 * x_data.^3 - 10 * x_data.^2 + 5 * x_data + randn(size(x_data)) * 0.01;
% Assume the linear region is between -0.05 and 0.05 on the x-axis
linear_region = x_data >= -0.05 & x_data <= 0.05;
% Perform linear regression on the linear region
p = polyfit(x_data(linear_region), y_data(linear_region), 1);
% The slope of the linear area
slope = p(1);
% Plot the original data
plot(x_data, y_data, 'b-', 'LineWidth', 1.5);
hold on; % Hold the current graph
% Plot the linear fit
x_fit = x_data(linear_region);
y_fit = polyval(p, x_fit);
plot(x_fit, y_fit, 'r-', 'LineWidth', 2);
% Annotate the slope on the plot
text(mean(x_fit), mean(y_fit), ['Slope: ' num2str(slope, '%.2f')], ...
'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center', 'FontSize', 12);
% Label the axes
xlabel('Sideslip Front Axis');
ylabel('Lateral Force Front Axis');
title('Dummy Data with Linear Fit');
legend('Original Data', 'Linear Fit', 'Location', 'northwest');
% Show the plot with the linear fit
hold off;
Replace x_data and y_data with the actual data points from your graph, and set x_start and x_end to define the linear region you're interested in. The polyfit function will then return the slope and intercept of the best-fit line through that region. The polyval function is used to evaluate the fitted polynomial at the given x-values, which can be plotted to visually confirm the fit.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
댓글 수: 0
John D'Errico
2023년 12월 25일
편집: John D'Errico
2023년 12월 25일
There is NO linear region in such a curve. Only a region which is moderately close to linear. It is a CURVE. Not a straight line.
And of course, we lack any data from you to give an example.
First, suppose your curve is a fairly smooth one? Now you should consider that the linear part of a sigmoidal curve like that is the one with the MAXIMUM slope. For example, I'll make up some data...
x = sort(rand(100,1)); % your data needs to be sorted to plot well, as well as do the next step
y = sin((x - 0.5)*pi);
plot(x,y,'-o')
So what is the linear part of that curve? Certainly there is none. But the most linear portion is the one with the maximum local slope. And how can we find that?
s = gradient(y,x);
And now, we can find the peak slope and the location simply enough.
[peakslope,peakloc] = findpeaks(s,x)
hold on
xline(peakloc)
So, as we would expect for this function, the maximum slope is quite approximately pi. And we found it at approximately x=0.5000. This is indeed the location on the curve where it is most perfectly linear.
If your curve has some degree of noise in it, then I would use other tools. But lacking any data, I cannot suggest the perfect tool. I might use a smoothing spline instead then find the point of maximum slope on the smoothing spline. And there are many other approaches I might take. I might even use polyfit, on a restricted region of the data, but then you need to choose the region.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!