How to find the linear slope of a set of data points on a graph
38 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am taking some data from a test and I need to know how to find the slope equation and R^2 of the linear section of the data. I also need to plot this linear line on the same plot as the plot of the data points. If you look at the picture you will see initally there is a linear slope before the data curves. This is the part that I need to plot a linear line to, the from there get the equation for that line and the R^2 value. I also included my code for this plot.
T=[0
0.250000000000000
1.14000000000000
2.33000000000000
3.58000000000000
4.76000000000000
5.15000000000000
5.58000000000000
5.94000000000000
6.13000000000000
6.44000000000000
6.69000000000000
6.88000000000000
6.92000000000000
7.06000000000000
7.04000000000000
7.23000000000000
7.13000000000000
7.39000000000000
7.29000000000000
7.59000000000000
7.60000000000000
7.69000000000000
7.73000000000000
7.91000000000000
7.94000000000000
8.14000000000000
8.19000000000000
8.27000000000000
8.29000000000000
8.44000000000000
8.73000000000000
8.58000000000000
8.75000000000000
8.80000000000000
8.85000000000000
9.03000000000000
9.20000000000000
9.35000000000000
9.49000000000000
9.65000000000000
9.59000000000000
9.69000000000000
9.69000000000000
9.89000000000000
10.1900000000000
10.2900000000000
10.6900000000000
10.7200000000000
10.7900000000000
10.8300000000000
11.2500000000000];
AT=[0
0.00628318530717959
0.0230383461263252
0.0411897703470662
0.0605629250442032
0.0839503370209273
0.0944223125328932
0.110304808726042
0.129677963423179
0.144687794990330
0.168773338667852
0.182037840983009
0.199840199353351
0.217118958948095
0.240680903850018
0.253770873239976
0.272969495011913
0.283965069299477
0.305781684949407
0.327772833524535
0.357617963733638
0.392873614623924
0.427082067963012
0.459894257900506
0.500734962397173
0.544542726622231
0.606501915068030
0.638965039155124
0.679631210726592
0.703542221478914
0.741590399172391
0.795695605984215
0.828856861772107
0.869348500418376
0.915076126820627
0.971799327510443
1.06709430466933
1.18211150237576
1.26693450402268
1.34948857764202
1.44094383044652
1.52611589794384
1.62943938966191
1.71862571443882
1.87622894589390
2.04936560769174
2.23768663398193
2.42059713959094
2.56406320410487
2.76145994250543
3.12117230134146
3.44650167391320];
L=0.07845;
J=0.1332*10^-9;
TL=T*L;
ATJ=AT*J;
plot(ATJ,TL,Marker=".")
0 commentaires
Réponse acceptée
Star Strider
le 2 Août 2023
Modifié(e) : Star Strider
le 2 Août 2023
Perhaps this —
T=[0
0.250000000000000
1.14000000000000
2.33000000000000
3.58000000000000
4.76000000000000
5.15000000000000
5.58000000000000
5.94000000000000
6.13000000000000
6.44000000000000
6.69000000000000
6.88000000000000
6.92000000000000
7.06000000000000
7.04000000000000
7.23000000000000
7.13000000000000
7.39000000000000
7.29000000000000
7.59000000000000
7.60000000000000
7.69000000000000
7.73000000000000
7.91000000000000
7.94000000000000
8.14000000000000
8.19000000000000
8.27000000000000
8.29000000000000
8.44000000000000
8.73000000000000
8.58000000000000
8.75000000000000
8.80000000000000
8.85000000000000
9.03000000000000
9.20000000000000
9.35000000000000
9.49000000000000
9.65000000000000
9.59000000000000
9.69000000000000
9.69000000000000
9.89000000000000
10.1900000000000
10.2900000000000
10.6900000000000
10.7200000000000
10.7900000000000
10.8300000000000
11.2500000000000];
AT=[0
0.00628318530717959
0.0230383461263252
0.0411897703470662
0.0605629250442032
0.0839503370209273
0.0944223125328932
0.110304808726042
0.129677963423179
0.144687794990330
0.168773338667852
0.182037840983009
0.199840199353351
0.217118958948095
0.240680903850018
0.253770873239976
0.272969495011913
0.283965069299477
0.305781684949407
0.327772833524535
0.357617963733638
0.392873614623924
0.427082067963012
0.459894257900506
0.500734962397173
0.544542726622231
0.606501915068030
0.638965039155124
0.679631210726592
0.703542221478914
0.741590399172391
0.795695605984215
0.828856861772107
0.869348500418376
0.915076126820627
0.971799327510443
1.06709430466933
1.18211150237576
1.26693450402268
1.34948857764202
1.44094383044652
1.52611589794384
1.62943938966191
1.71862571443882
1.87622894589390
2.04936560769174
2.23768663398193
2.42059713959094
2.56406320410487
2.76145994250543
3.12117230134146
3.44650167391320];
L=0.07845;
J=0.1332*10^-9;
TL=T*L;
ATJ=AT*J;
[Lv,m,b] = ischange(TL, 'linear', 'Threshold',0.01); % Change Points
Chs = nnz(Lv)
idxv = 1:find(Lv);
mdl = fitlm(ATJ(idxv), TL(idxv))
Slope = mdl.Coefficients.Estimate(2)
RSq = mdl.Rsquared.Ordinary
RSqAdj = mdl.Rsquared.Adjusted
[y,yci] = predict(mdl, ATJ(idxv));
figure
plot(ATJ,TL,Marker=".")
hold on
plot(ATJ(Lv),TL(Lv),"r+")
plot(ATJ(idxv), y, '-r')
plot(ATJ(idxv), yci, '--r')
hold off
EDIT — (2 Aug 2023 at 18:55)
Added the ‘Slope’ variable, returning that value from the regression results.
.
2 commentaires
Star Strider
le 2 Août 2023
That depends on the data, although it should if the data are similar. It might be necessary to return more change points from ischange and then choose the most optimal of them otherwise. Return more by reducing the value of 'Threshold'.
The independent variable coordinate of the value (if you need to calculate it) would be ‘ATJ(idxv(end))’ and the dependent variable value accordingly would be ‘TL(idxv(end))’.
Plus de réponses (2)
John D'Errico
le 2 Août 2023
There is NO linear section. It is a curve. Viewed over a short enough segment, any curve will appear linear.
T=[0
0.250000000000000
1.14000000000000
2.33000000000000
3.58000000000000
4.76000000000000
5.15000000000000
5.58000000000000
5.94000000000000
6.13000000000000
6.44000000000000
6.69000000000000
6.88000000000000
6.92000000000000
7.06000000000000
7.04000000000000
7.23000000000000
7.13000000000000
7.39000000000000
7.29000000000000
7.59000000000000
7.60000000000000
7.69000000000000
7.73000000000000
7.91000000000000
7.94000000000000
8.14000000000000
8.19000000000000
8.27000000000000
8.29000000000000
8.44000000000000
8.73000000000000
8.58000000000000
8.75000000000000
8.80000000000000
8.85000000000000
9.03000000000000
9.20000000000000
9.35000000000000
9.49000000000000
9.65000000000000
9.59000000000000
9.69000000000000
9.69000000000000
9.89000000000000
10.1900000000000
10.2900000000000
10.6900000000000
10.7200000000000
10.7900000000000
10.8300000000000
11.2500000000000];
AT=[0
0.00628318530717959
0.0230383461263252
0.0411897703470662
0.0605629250442032
0.0839503370209273
0.0944223125328932
0.110304808726042
0.129677963423179
0.144687794990330
0.168773338667852
0.182037840983009
0.199840199353351
0.217118958948095
0.240680903850018
0.253770873239976
0.272969495011913
0.283965069299477
0.305781684949407
0.327772833524535
0.357617963733638
0.392873614623924
0.427082067963012
0.459894257900506
0.500734962397173
0.544542726622231
0.606501915068030
0.638965039155124
0.679631210726592
0.703542221478914
0.741590399172391
0.795695605984215
0.828856861772107
0.869348500418376
0.915076126820627
0.971799327510443
1.06709430466933
1.18211150237576
1.26693450402268
1.34948857764202
1.44094383044652
1.52611589794384
1.62943938966191
1.71862571443882
1.87622894589390
2.04936560769174
2.23768663398193
2.42059713959094
2.56406320410487
2.76145994250543
3.12117230134146
3.44650167391320];
L=0.07845;
J=0.1332*10^-9;
TL=T*L;
ATJ=AT*J;
First, TL is not an increasing set. As such, that will make things worse. So if we sort the data...
[TLS,idx] = sort(TL);
ATJS = ATJ(idx);
plot(ATJS,TLS)
Now we can compute the local slope of the curve along the entire length of the curve.
G = gradient(TLS,ATJS);
plot(ATJS,G)
If there were a linear portion near the beginning of the curve, you would see the slope is locally CONSTANT there. Does that happen? NO!!!! The slope is everywhere changing, and changing rapidly near the beginning of the curve.
You are allowing your eyes to trick you, thinking the beginning of the curve has a linear portion. It is not so. Not at all.
0 commentaires
Steven Lord
le 2 Août 2023
T=[0
0.250000000000000
1.14000000000000
2.33000000000000
3.58000000000000
4.76000000000000
5.15000000000000
5.58000000000000
5.94000000000000
6.13000000000000
6.44000000000000
6.69000000000000
6.88000000000000
6.92000000000000
7.06000000000000
7.04000000000000
7.23000000000000
7.13000000000000
7.39000000000000
7.29000000000000
7.59000000000000
7.60000000000000
7.69000000000000
7.73000000000000
7.91000000000000
7.94000000000000
8.14000000000000
8.19000000000000
8.27000000000000
8.29000000000000
8.44000000000000
8.73000000000000
8.58000000000000
8.75000000000000
8.80000000000000
8.85000000000000
9.03000000000000
9.20000000000000
9.35000000000000
9.49000000000000
9.65000000000000
9.59000000000000
9.69000000000000
9.69000000000000
9.89000000000000
10.1900000000000
10.2900000000000
10.6900000000000
10.7200000000000
10.7900000000000
10.8300000000000
11.2500000000000];
AT=[0
0.00628318530717959
0.0230383461263252
0.0411897703470662
0.0605629250442032
0.0839503370209273
0.0944223125328932
0.110304808726042
0.129677963423179
0.144687794990330
0.168773338667852
0.182037840983009
0.199840199353351
0.217118958948095
0.240680903850018
0.253770873239976
0.272969495011913
0.283965069299477
0.305781684949407
0.327772833524535
0.357617963733638
0.392873614623924
0.427082067963012
0.459894257900506
0.500734962397173
0.544542726622231
0.606501915068030
0.638965039155124
0.679631210726592
0.703542221478914
0.741590399172391
0.795695605984215
0.828856861772107
0.869348500418376
0.915076126820627
0.971799327510443
1.06709430466933
1.18211150237576
1.26693450402268
1.34948857764202
1.44094383044652
1.52611589794384
1.62943938966191
1.71862571443882
1.87622894589390
2.04936560769174
2.23768663398193
2.42059713959094
2.56406320410487
2.76145994250543
3.12117230134146
3.44650167391320];
Let's see if there's a point on the curve where it has an "abrupt" change in slope & intercept. You might also find the second and third outputs from ischange useful.
changeLocs = ischange(T, 'linear', 'SamplePoints', AT);
plot(AT, T, 'o-', 'MarkerIndices', find(changeLocs))
0 commentaires
Voir également
Catégories
En savoir plus sur Descriptive Statistics dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!