fitting line for the first part of the graph
Afficher commentaires plus anciens
I have the following experimental data
x=[0.000421940928270042;0.000420168067226891;0.000418410041841004;0.000416666666666667;0.000414937759336100;0.000413223140495868;0.000411522633744856;0.000409836065573771;0.000408163265306122;0.000406504065040650;0.000404858299595142;0.000403225806451613;0.000401606425702811;0.000400000000000000;0.000398406374501992;0.000396825396825397;0.000395256916996047;0.000393700787401575;0.000392156862745098;0.000390625000000000;0.000389105058365759;0.000387596899224806;0.000386100386100386;0.000384615384615385;0.000383141762452107;0.000381679389312977;0.000380228136882129;0.000378787878787879;0.000377358490566038;0.000375939849624060;0.000374531835205993;0.000373134328358209;0.000371747211895911;0.000370370370370370;0.000369003690036900;0.000367647058823529;0.000366300366300366;0.000364963503649635;0.000363636363636364;0.000362318840579710;0.000361010830324910;0.000359712230215827;0.000358422939068100];
y=[-1.38899994939224;-1.35871395793660;-1.38242344659456;-1.40608139919123;-1.38422552774949;-1.37773526527217;-1.39487365118433;-1.36518337279495;-1.39978507595054;-1.38781689227391;-1.38382984300519;-1.41419979409050;-1.33826384663955;-1.35089262006801;-1.26352834469030;-1.33052407235525;-1.26786443834638;-1.23386998433884;-1.21555999623804;-1.16188856192494;-1.06920032290667;-1.01962852114161;-1.00407928991600;-0.919072639604953;-0.991469702775368;-0.815561305166114;-0.699234124812569;-0.638147300677634;-0.621360634010523;-0.573695070265892;-0.562308245326998;-0.445366441266917;-0.230576299726422;-0.220506986714244;-0.0720667385181811;-0.0396451201248960;-0.00437232408536387;0.232858731915111;0.238100819014742;0.396828802505368;0.551746643862013;0.626113326683903;0.764840522864362];
plot(x,y)
I am plotting Y(x)
The first part of this plot can be fitted using a line. Any suggestions how to do this.
Thanks in advance for your help.
Réponse acceptée
Plus de réponses (1)
Adam Danz
le 22 Oct 2019
This requires that the x values are in ascending order which is why we're soring them below. See comments for details.
% Sort x and y values so that x are in ascending order
[xSort, xIdx] = sort(x);
ySort = y(xIdx);
% find linear change point
chgPoint = find(ischange(ySort,'linear','MaxNumChanges',1,'SamplePoints',xSort));
% Fit line segment prior to change point
coef = polyfit(xSort(1:chgPoint),ySort(1:chgPoint),1);
% Plot results
figure();
plot(xSort,ySort, 'k-')
hold on
xline(xSort(chgPoint),'m-')
refline(coef)
xlim([min(x),max(x)])
ylim([min(y),max(y)])
legend({'data','ChangePoint','lin fit'})
title(sprintf('y = %.1fx + %.1f', coef))

6 commentaires
Mohammed Qahosh
le 23 Oct 2019
Adam Danz
le 23 Oct 2019
xline() is the function that draws the vertical magenta line in my plot. That function was introduced in r2018b so you're probably working with an earlier function.
Just remove that line - it only shows where the change detection occurs. You can replace it with this line.
plot(xSort(chgPoint),ySort(chgPoint), 'm*')
Mohammed Qahosh
le 23 Oct 2019
Adam Danz
le 23 Oct 2019
Glad I could help!
Moses Njovana
le 4 Juil 2023
Quick one @Adam Danz. Please kindly advise if there's a way to limit the refline line plot to a certain region of the graph?
Adam Danz
le 5 Juil 2023
Refline extends to the current axes limits so you could temporarily set xlim and ylim, call refline and then return the original xlim and ylim values. However, a better approach would simply be to compute the two end points at the specified bounds and plot the line using plot().
Catégories
En savoir plus sur 2-D and 3-D Plots dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!