how to put a line by code in regression
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!