Polyfit function is returning a partial line of best fit

조회 수: 2 (최근 30일)
Gregory Lang
Gregory Lang 2023년 2월 22일
댓글: William Rose 2023년 2월 22일
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
Oguz Kaan Hancioglu 2023년 2월 22일
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에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by