Least square fit (regression analysis)

조회 수: 2 (최근 30일)
Abraham
Abraham 2018년 11월 18일
답변: Bruno Luong 2018년 11월 18일
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

채택된 답변

Bruno Luong
Bruno Luong 2018년 11월 18일
The y vector must be column
r = inv(A1'*A1)*(A1'*y(:));

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by