Polyfit function is returning a partial line of best fit

1 次查看(过去 30 天)
When using the polyfit function to add a line of best fit for my graohs, it is returning an extremely small line of best fit. Sample code and information below
MATLAB Version: R2022b for academic use
code:
[xData, yData] = prepareCurveData(sbchl, sbb555);
ft = fittype('poly1'); %defines
[sbchl_vs_sbb555_fitresult, sbchl_vs_sbb555_gof] = fit(xData, yData, ft);
[xData, yData] = prepareCurveData(gichl, gib555);
ft = fittype('poly1'); %defines
[gichl_vs_gib555_fitresult, gichl_vs_gib555_gof] = fit(xData, yData, ft);
subplot(3,2,4);
plot(sbchl_vs_sbb555_fitresult);
hold on;
plot(sbchl,sbb555,'o');
hold on;
plot(gichl_vs_gib555_fitresult,'k');
hold on;
plot(gichl,gib555,'ok');
xlabel('Chl');
ylabel('b555');
In the subplot the line of best fit fills about 5% of the data area, if pulled to a normal figure it is covering about 50% of the figure area

采纳的回答

William Rose
William Rose 2023-2-22
Since you did not include a .mat file with the data being fitted (sbchl, sbb555, gichl, gib555), others cannot run your code.
I suggest you try plotting the fit last - after plotting the actual data. The help says
plot(cfit) plots the cfit object over the domain of the current axes, if any. If there are no current axes, and fun is an output from the fit function, the plot is over the domain of the fitted data.
Therefore if you plot the fit last, it may use a larger fraction of the current axes domain.
FYI, you can include the code as code, by highlighting it, then clicking on the "code" icon at the top of the message window. Then it is runnable in the window (if you include the necessary data):
[xData, yData] = prepareCurveData(sbchl, sbb555);
and so on.
Good luck.
  2 个评论
Gregory Lang
Gregory Lang 2023-2-22
Thank you, sorry I'm in class right now and trying to patch code at the same time. I'll try to work through what you suggested
William Rose
William Rose 2023-2-22
@Gregory Lang, you are welcome. @Voss and by @Oguz Kaan Hancioglu have also provided excellent answers which I'm sure you will find helpful.

请先登录,再进行评论。

更多回答(2 个)

Voss
Voss 2023-2-22
编辑:Voss 2023-2-22
Rather than calling plot with the fitobject returned from fit, you can use coeffvalues to get the coefficients of the fitobject and then use the coefficients to plot a line across whatever domain you want.
% made up data
sbchl = 1:10;
sbb555 = rand(1,10);
gichl = 1:10;
gib555 = rand(1,10);
x_plot = [0 10]; % domain to plot fitted lines over
ft = fittype('poly1'); %defines
% [xData, yData] = prepareCurveData(sbchl, sbb555);
xData = sbchl.';
yData = sbb555.';
[sbchl_vs_sbb555_fitresult, sbchl_vs_sbb555_gof] = fit(xData, yData, ft);
% [xData, yData] = prepareCurveData(gichl, gib555);
xData = gichl.';
yData = gib555.';
[gichl_vs_gib555_fitresult, gichl_vs_gib555_gof] = fit(xData, yData, ft);
subplot(3,2,4);
hold on;
p = coeffvalues(sbchl_vs_sbb555_fitresult);
plot(x_plot, x_plot.*p(1)+p(2), 'r');
plot(sbchl,sbb555,'ro');
p = coeffvalues(gichl_vs_gib555_fitresult);
plot(x_plot, x_plot.*p(1)+p(2), 'k');
plot(gichl,gib555,'ok');
% create a legend yourself, if you still want a legend
% legend('fitted curve','data','fitted curve','data')
xlabel('Chl');
ylabel('b555');

Oguz Kaan Hancioglu
Your code works as expected. There is no solution for curve fitting that covers all sample points. Therefore, you can only fit the curve with errors. By modifying the curve type or order, you can decrease the error.
Subplot and plot changes the figure view.
sbchl = 1:10;
sbb555 = sbchl.^2;
gichl = 1:10;
gib555 = sbchl.^2;
[xData, yData] = prepareCurveData(sbchl, sbb555);
ft = fittype('poly1'); %defines
[sbchl_vs_sbb555_fitresult, sbchl_vs_sbb555_gof] = fit(xData, yData, ft);
[xData, yData] = prepareCurveData(gichl, gib555);
ft = fittype('poly1'); %defines
[gichl_vs_gib555_fitresult, gichl_vs_gib555_gof] = fit(xData, yData, ft);
%subplot(3,2,4);
plot(sbchl_vs_sbb555_fitresult);
hold on;
plot(sbchl,sbb555,'o');
hold on;
plot(gichl_vs_gib555_fitresult,'k');
hold on;
plot(gichl,gib555,'ok');
xlabel('Chl');
ylabel('b555');
legend off;

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by