Finding the slope after drawing a trend/linear fit

5 views (last 30 days)
I have an excel data. I want to find the slope after fitting a linear fit on it. In the attached plot, I manually drawn the fit line in black, I need the matlab code for it. So that the program will do it. I've attached my data too.

Accepted Answer

Star Strider
Star Strider on 16 Nov 2023
Alternatively —
files = dir('*.xlsx');
T1 = readtable(files.name)
T1 = 5001×2 table
T x _______ ________ 0 0.04851 4e-06 0.051774 8e-06 0.050142 1.2e-05 0.04851 1.6e-05 0.04851 2e-05 0.050142 2.4e-05 0.050142 2.8e-05 0.050142 3.2e-05 0.050142 3.6e-05 0.050142 4e-05 0.050142 4.4e-05 0.051774 4.8e-05 0.04851 5.2e-05 0.051774 5.6e-05 0.051774 6e-05 0.050142
VN = T1.Properties.VariableNames;
T = T1.T;
x = T1.x;
[xmax,idx] = max(x);
idxrng = 1:idx;
DM = [T(idxrng) ones(size(x(idxrng)))]; % Design Matrix
B = DM \ x(idxrng) % Another Option Is 'polyfit'
B = 2×1
84.3962 0.0217
FitLine = DM * B; % Use 'polyval' With 'polyfit'
xr(1) = 0.004;
yr(1) = [xr 1] * B;
yr(2) = 0.2;
xr(2) = (yr(2)-B(2))/B(1);
figure
plot(T,x)
hold on
plot(T(idxrng), FitLine, '-k', 'LineWidth',3)
plot([1 1]*xr(1), [yr(1) yr(2)], '-r')
plot([xr(1) xr(2)], [1 1]*yr(2), '-r')
hold off
grid
xlabel(VN{1})
ylabel(VN{2})
text(xr(1), yr(2), sprintf('\\uparrow\nSlope = %.2f', B(1)), 'Horiz','left', 'Vert','top')
.
  4 Comments
Reji G
Reji G on 17 Nov 2023
I've updated excel sheet with required data. T is common. Plot T&w, T&x, T&y, T&z. I want to draw a fit in increasing direction in the positive half cycles(As shown in the previous diagram) and the slope between each fit lines drawn for T&w, T&x, T&y, T&z.
Star Strider
Star Strider on 17 Nov 2023
If you want regression lines for the outset of each variable, this works —
files = dir('*.xlsx');
T1 = readtable(files.name)
T1 = 5001×5 table
T w x y z _______ ________ _________ __________ __________ 0 0.04851 -0.047776 -0.066858 0.0019753 4e-06 0.051774 -0.04288 -0.063591 0.00034241 8e-06 0.050142 -0.046144 -0.057056 0.0019753 1.2e-05 0.04851 -0.047776 -0.052155 0.00034241 1.6e-05 0.04851 -0.046144 -0.047254 0.0019753 2e-05 0.050142 -0.044512 -0.042353 0.0036082 2.4e-05 0.050142 -0.046144 -0.035819 0.0019753 2.8e-05 0.050142 -0.046144 -0.027651 0.0019753 3.2e-05 0.050142 -0.047776 -0.02275 0.0019753 3.6e-05 0.050142 -0.044512 -0.019483 0.00034241 4e-05 0.050142 -0.044512 -0.012948 0.00034241 4.4e-05 0.051774 -0.046144 -0.0047799 0.0019753 4.8e-05 0.04851 -0.046144 -0.0015127 0.0036082 5.2e-05 0.051774 -0.047776 0.0033882 0.0019753 5.6e-05 0.051774 -0.046144 0.0099227 0.00034241 6e-05 0.050142 -0.044512 0.018091 0.0052411
VN = T1.Properties.VariableNames;
T = T1.T;
figure
tiledlayout(2,2)
for k = 1:size(T1,2)-1
nexttile
v = T1{:,k+1};
plot(T, v)
grid
title(VN{k+1})
[vmax,idx] = max(v);
idxrng = 1:idx;
DM = [T(idxrng) ones(size(T(idxrng)))]; % Design Matrix
B = DM \ v(idxrng); % Another Option Is 'polyfit'
FitLine = DM * B; % Use 'polyval' With 'polyfit'
hold on
plot(T(idxrng), FitLine, '-k', 'LineWidth',3)
hold off
xlabel(VN{1})
ylabel(VN{k+1})
text(mean(xlim), 0.9*max(ylim), sprintf('Regression Line Slope = %.3f',B(1)), 'Horiz','center', 'Vert','top')
end
The regression lines do not connect the first and first maximum points in all variables because they are fitting all the data between those points. If you want something else, plese be specific.
.

Sign in to comment.

More Answers (0)

Categories

Find more on Curve Fitting Toolbox in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!