Polyfit function is returning a partial line of best fit
Show older comments
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
Accepted Answer
More Answers (2)
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');
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;
Categories
Find more on Descriptive Statistics and Visualization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
