regress(x,y) for least square regression of two variables x,y
Show older comments
when i use regress(x,y),I obtain only one answer :shouldn't i get two answers which are the slope and y-intercept ?
Accepted Answer
More Answers (1)
Image Analyst
on 2 Jun 2013
I don't have regress(). Please list what Product (toolbox) is is in below your question in the Product box.
You can use polyfit() which is in base MATLAB to get the slope and intercept in one line. Here's a full blown demo:
clc;
% Create training data:
x = [1 4 5 6]
y = [4 6 7 9]
% Do the regression.
coefficients = polyfit(x, y, 1); % 1 means a line
slope = coefficients(1)
intercept = coefficients(2)
% We're done!
% Plot training data:
plot(x, y, 'bo', 'LineWidth', 3);
hold on;
% Plot fit
numberOfPoints = 100;
fitX = linspace(min(x), max(x), numberOfPoints);
fitY = polyval(coefficients, fitX);
plot(fitX, fitY, 'r-', 'LineWidth', 3);
grid on;
xlabel('X', 'FontSize', 30);
ylabel('Y', 'FontSize', 30);
1 Comment
the cyclist
on 2 Jun 2013
I always forget that regress() is not in core MATLAB. I added the Statistics Toolbox product tag.
Categories
Find more on Linear Predictive Coding 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!