how to put a line by code in regression
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
Image Analyst
am 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Regression finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!