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];
[a, sortOrder] = sort(a, 'ascend');
b = b(sortOrder);
linearCoeffs = polyfit(a, b, 1);
Slope = linearCoeffs(2)
Intercept = linearCoeffs(1)
subplot(2, 1, 1);
aFitted = a;
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);
for k = 1 : length(a)
yActual = b(k);
yFit = bFitted(k);
x = a(k);
line([x, x], [yFit, yActual], 'Color', 'm');
end
quadraticCoeffs = polyfit(a, b, 2);
subplot(2, 1, 2);
aFitted = a;
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);
for k = 1 : length(a)
yActual = b(k);
yFit = bFitted(k);
x = a(k);
line([x, x], [yFit, yActual], 'Color', 'm');
end
Best Answer