how to put a line by code in regression
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Image Analyst
on 23 Jul 2012
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 Comments
More Answers (0)
See Also
Categories
Find more on Linear and Nonlinear Regression 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!