![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/174040/image.png)
how to plot residual and fitting curve
14 views (last 30 days)
Show older comments
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)
0 Comments
Accepted Answer
Image Analyst
on 19 Apr 2017
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/174040/image.png)
4 Comments
David Dalton
on 14 Nov 2017
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
on 14 Nov 2017
Edited: Image Analyst
on 14 Nov 2017
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);
More Answers (0)
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox 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!