Curve Fitting using normal equations formulation of least squares

조회 수: 9 (최근 30일)
MATLABhelp
MATLABhelp 2019년 1월 23일
댓글: Dr. Bhavya 2024년 9월 18일
I am given an data table with values of x and y and supposed to approximate the relationship between x and y with a straight line y = a0 + a1*x. I must find the parameters using normal equations formulation of least squares. I have begun, but am unsure how to plot the curve?
My code is the following:
x = [20 40 60 80 100 120 140 160];
y = [13 22 30 36 40 43 45 46];
% Normal equations formulation of least squares
A = [ones(size(x)) x];
b = inv(A'*A)*(A'*y);

채택된 답변

Matt J
Matt J 2019년 1월 23일
x = [20 40 60 80 100 120 140 160].';
y = [13 22 30 36 40 43 45 46].';
% Normal equations formulation of least squares
A = [ones(size(x)) x];
yfit = A*((A'*A)\(A'*y));
plot(x,y,'*',x,yfit,'-')

추가 답변 (1개)

Stephan
Stephan 2019년 1월 23일
편집: Stephan 2019년 1월 23일
A = [20 40 60 80 100 120 140 160];
A = [A' ones(numel(A),1)];
b = [13 22 30 36 40 43 45 46]';
x = A\b;
fprintf('y = %.5f + %.5f * x\n', x(2),x(1))
test = x(1).*A+x(2);
scatter(A(:,1),b,'or')
hold on
plot(A,test)
hold off

카테고리

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