How can I find the coeficients alpha, beta of the simple linear regression, using "\" operator

1 view (last 30 days)
How can I find the coeficients alpha, beta for the X coordinates of the simple linear regression, using "\" operator?

Answers (1)

Torsten
Torsten on 10 Dec 2022
x = (0:0.1:1).';
y = 3*x + 4 + 0.01*randn(numel(x),1);
A = [x,ones(size(x))];
b = y;
coeffs = A\b;
coeffs(1)
ans = 3.0072
coeffs(2)
ans = 3.9919
  2 Comments
Torsten
Torsten on 11 Dec 2022
Edited: Torsten on 11 Dec 2022
You want to solve
x(1)*a + 1*b = y(1)
x(2)*a + 1*b = y(2)
...
x(end)*a + 1*b = y(end)
for a and b.
If you write this in matrix form M*[a;b] = v, you get the above M and v (I named them A and b), and you solve for [a;b] as
ab = M\v.

Sign in to comment.

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!