how to put a line by code in regression

2 ビュー (過去 30 日間)
Andy
Andy 2012 年 7 月 23 日
So after two days, I finally got part of my code to work for my linear regression assignment for work. Unfortunately, I don't know how to put a best-fitted linear regression line in MATLAB in the plot for linear regression. What is the code to do so?
Also, I used polyfit(x,y,1) and my output is
ans =
[-0.619 10.4846]
What is the code to write B0 and B1 on its own? I want my output to be like this:
b0 =
-0.619
b1 =
10.4846

採用された回答

Image Analyst
Image Analyst 2012 年 7 月 23 日
I'd do something like this (untested)
% Do the regression:
coeffs = polyfit(xTraining, yTraining, 1);
% Print coefficients to command window
coeffs(1)
coeffs(2)
% Fit some new x range (may be higher resolution than the training x if you want).
x = linspace(startingX, endingX, numberOfSamples);
% Do the fit
yFitted = polyval(coeffs, x);
% Plot the fit.
plot(x, yFitted, 'b-', 'LineWidth', 3);
hold on;
% Along with it, plot the original training data:
plot(xTraining, yTraining, 'rs');

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by