fitting line for the first part of the graph

7 次查看(过去 30 天)
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.

采纳的回答

Joe Vinciguerra
Joe Vinciguerra 2019-10-22
编辑:Joe Vinciguerra 2019-10-22
% let's say you want to fit the first 10 elements
ROI = (1:10);
% I'm just sorting your data by X so it plots from left to right.
% This isn't necessary but satisfies my OCD.
A = sortrows([x y],1);
x = A(:,1);
y = A(:,2);
% Extract your region of interest.
xROI = x(ROI);
yROI = y(ROI);
% perform a linear fit using a polynomial of 1 degree.
[p, S] = polyfit(xROI,yROI,1); % p are your coefficients. S is your error (if you are interested)
fitROI = polyval(p,xROI); % now take the fit and evaluate it at x values of interest
% Let's see what it looks like
figure; hold on; % create a figure and don't overwrite it
plot(x,y,'b') % plot your original data in blue
plot(xROI,yROI,'g') % plot your region of interest in green
plot(xROI,fitROI,'r') % plot to fitted line in red
  3 个评论
Adam Danz
Adam Danz 2019-10-23
Note that this approach arbitrarily chooses what section of the line to fit. Instead, you could use a method that finds where the slope changes significantly. That's the approach in the other answer here.
Mohammed Qahosh
Mohammed Qahosh 2019-10-23
Adam Danz Yes, actually I hope two ways now. Thank you both for your help :))

请先登录,再进行评论。

更多回答(1 个)

Adam Danz
Adam Danz 2019-10-22
You can use ischange() with the linear method to find the point where the slope changes.
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))
191022 170111-Figure 1.png
  6 个评论
Moses Njovana
Moses Njovana 2023-7-4
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
Adam Danz 2023-7-5
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().

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by