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 Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by