Least square fit (regression analysis)

2 views (last 30 days)
I have a x and y data, trying to find the the a and b coefficients using least square fit.
this is my code
My r transpose need to be in the format:
% define coordinates
x = [10 20 30 40 50 60 70];
y = [0.765 0.995 1.248 1.524 1.695 2.132 2.463];
plot(x,y,'*');
axis([0 80 0 2]);
hold on
A1 = zeros(1);
for i = 1:7
A1(i,1) = x(i)^2;
A1(i,2) = x(i);
A1(i,3) = 1.0;
end
r = inv(A1'*A1)*(A1'*y);
t = (0:0.1:7);
y1 = r(1)*t.^2 + r(2)*t + r(3);
ploy(t,y1,'r');
Error message
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in
the first matrix matches the number of rows in the second matrix. To perform
elementwise multiplication, use '.*'.
I did this: inv(A1'*A1)*A1'*y
But got an error message
Any possible fix is highly appreciated. Thanks

Accepted Answer

Bruno Luong
Bruno Luong on 18 Nov 2018
The y vector must be column
r = inv(A1'*A1)*(A1'*y(:));

More Answers (0)

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!