필터 지우기
필터 지우기

how to plot residual and fitting curve

조회 수: 11 (최근 30일)
farfar
farfar 2017년 4월 19일
편집: Image Analyst 2017년 11월 14일
Hi I have two set of data (a,b).
a=[6.91 8.26 2.21 1.19 2.32 8.41 7.3 4.32 2 3.42 3.21 7.54 8.72 2.38 3.1 8.18 5.47 1.27 6.09 7.98 7.69 5.39 7.35 7.33 5.3];
b=[17.52 1.77 14.97 7.5 15.09 9.4 17.36 22.69 12.87 19.16 19.06 14.75 9.83 17.61 18.63 11.15 23.89 10.75 22.33 14.29 16.77 22.41 16.63 17.42 21.37];
and I plot the linear regression line for it. how can I plot residual and the least square quadratic regression line ? thanks
figure(1)
scatter(a,b)
hl = lsline;
B = [ones(size(hl.XData(:))), hl.XData(:)]\hl.YData(:);
Slope = B(2)
Intercept = B(1)

채택된 답변

Image Analyst
Image Analyst 2017년 4월 19일
This works well:
a=[6.91 8.26 2.21 1.19 2.32 8.41 7.3 4.32 2 3.42 3.21 7.54 8.72 2.38 3.1 8.18 5.47 1.27 6.09 7.98 7.69 5.39 7.35 7.33 5.3];
b=[17.52 1.77 14.97 7.5 15.09 9.4 17.36 22.69 12.87 19.16 19.06 14.75 9.83 17.61 18.63 11.15 23.89 10.75 22.33 14.29 16.77 22.41 16.63 17.42 21.37];
% First need to sort a otherwise when we go to plot it, it will look like a mess!
[a, sortOrder] = sort(a, 'ascend');
b = b(sortOrder); % Need to sort b the same way.
% First compute the linear fit.
linearCoeffs = polyfit(a, b, 1);
Slope = linearCoeffs(2)
Intercept = linearCoeffs(1)
% Plot training data and fitted data.
subplot(2, 1, 1);
aFitted = a; % Evalutate the fit as the same x coordinates.
bFitted = polyval(linearCoeffs, aFitted);
plot(a, b, 'rd', 'MarkerSize', 10);
hold on;
plot(aFitted, bFitted, 'b-', 'LineWidth', 2);
grid on;
xlabel('a', 'FontSize', 20);
ylabel('b', 'FontSize', 20);
% Plot residuals as lines from actual data to fitted line.
for k = 1 : length(a)
yActual = b(k);
yFit = bFitted(k);
x = a(k);
line([x, x], [yFit, yActual], 'Color', 'm');
end
% Do the same for a quadratic fit.
quadraticCoeffs = polyfit(a, b, 2);
% Plot training data and fitted data.
subplot(2, 1, 2);
aFitted = a; % Evalutate the fit as the same x coordinates.
bFitted = polyval(quadraticCoeffs, aFitted);
plot(a, b, 'rd', 'MarkerSize', 10);
hold on;
plot(aFitted, bFitted, 'b-', 'LineWidth', 2);
grid on;
xlabel('a', 'FontSize', 20);
ylabel('b', 'FontSize', 20);
% Plot residuals as lines from actual data to fitted line.
for k = 1 : length(a)
yActual = b(k);
yFit = bFitted(k);
x = a(k);
line([x, x], [yFit, yActual], 'Color', 'm');
end
  댓글 수: 4
David Dalton
David Dalton 2017년 11월 14일
The equation of the fitted curve is a polynomial (first order) "polyfit(a, b, 1);" i.e. a linear fit... y=mx +c, where . where is has shown that the Intercept is 'c' and the Slope is m
Image Analyst
Image Analyst 2017년 11월 14일
편집: Image Analyst 2017년 11월 14일
bFitted = polyval(linearCoeffs, aFitted);
is essentially doing this:
bFitted = linearCoeffs(1) * aFitted + linearCoeffs(2);
and
bFitted = polyval(quadraticCoeffs, aFitted);
is essentially doing this:
bFitted = quadraticCoeffs(1) .* aFitted .^ 2 + quadraticCoeffs(2) .* aFitted + quadraticCoeffs(3);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by