Incorrect y intercept using fitlm

3 ビュー (過去 30 日間)
Sarvesh
Sarvesh 2025 年 2 月 28 日
コメント済み: Sarvesh 2025 年 2 月 28 日
Hi and thanks in advance.
I'm currently working on creating basic plots and obtaining the equation of the best-fit line using 'fitlm' in MATLAB. However, I've observed that the y-intercept of the equation doesn't seem to match the value displayed on the graph. The best-fit line visually crosses around approximately 1 on the y-axis, but the displayed value is approximately -0.21 from the equation. Is there a way to correct this discrepancy to match the displayed value, or is there something else happening that I am unaware of
thanks
% Example data (replace with your actual data)
xData = [1, 2, 3, 4, 5, 6, 7];
yData = [1.1, 1.5, 4, 3.8, 6, 6.2, 8];
% Fit a linear model to the data
mdl = fitlm(xData, yData);
% Plotting the data and the best fit line
figure;
scatter(xData, yData, 'filled', 'o'); % Scatter plot of data points
hold on;
plot(mdl); % Plotting the best fit line
hold off;
xlabel('X Data');
ylabel('Y Data');
title('Best Fit Line');
grid on;
% Display the equation of the line
disp(['Equation of the best fit line: y = ' num2str(mdl.Coefficients.Estimate(2)) 'x + ' num2str(mdl.Coefficients.Estimate(1))]);
  2 件のコメント
Stephen23
Stephen23 2025 年 2 月 28 日
編集済み: Stephen23 2025 年 2 月 28 日
" The best-fit line visually crosses around approximately 1 on the y-axis"
Yes, but at x=1, so it has nothing to do with the y-intercept (which is defined as being at x=0).
"...but the displayed value is approximately -0.21 from the equation."
Which seems about correct.
"Is there a way to correct this discrepancy to match the displayed value"
The only discrepancy that I can see is that you are confusing x=1 for x=0.
"or is there something else happening that I am unaware of"
Plot the line of best fit at x=0 and see what value it has.
Sarvesh
Sarvesh 2025 年 2 月 28 日
Silly overlook from my end! thanks @Stephen23

サインインしてコメントする。

採用された回答

Sam Chak
Sam Chak 2025 年 2 月 28 日
Extend the line, and you will see the y-intercept at .
% Example data (replace with your actual data)
xData = [1, 2, 3, 4, 5, 6, 7];
yData = [1.1, 1.5, 4, 3.8, 6, 6.2, 8];
% Fit a linear model to the data
mdl = fitlm(xData, yData)
mdl =
Linear regression model: y ~ 1 + x1 Estimated Coefficients: Estimate SE tStat pValue ________ _______ ________ __________ (Intercept) -0.21429 0.50341 -0.42567 0.68805 x1 1.1464 0.11257 10.185 0.00015658 Number of observations: 7, Error degrees of freedom: 5 Root Mean Squared Error: 0.596 R-squared: 0.954, Adjusted R-Squared: 0.945 F-statistic vs. constant model: 104, p-value = 0.000157
% Plotting the data and the best fit line
figure;
scatter(xData, yData, 'filled', 'o'); % Scatter plot of data points
hold on;
plot(mdl); % Plotting the best fit line
xlabel('X Data');
ylabel('Y Data');
title('Best Fit Line');
grid on;
%% ---- plot the extension ----
m = mdl.Coefficients.Estimate(2);
c = mdl.Coefficients.Estimate(1);
x = linspace(0, 1, 101);
y = m*x + c;
plot(x, y, '--')
xlim([0 7])
hold off;
% Display the equation of the line
disp(['Equation of the best fit line: y = ' num2str(mdl.Coefficients.Estimate(2)) 'x + ' num2str(mdl.Coefficients.Estimate(1))]);
Equation of the best fit line: y = 1.1464x + -0.21429
  1 件のコメント
Sarvesh
Sarvesh 2025 年 2 月 28 日
thanks @Sam Chak. just realized the mistake a while ago.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with Curve Fitting Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by