x = [100 1000 500 1800 180 140];
y = [100 1010 502 1777 183 141];
x_error = 5 * ones(size(x));
figure
hold on
% Scatter points
scatter(x, y, 70, 'filled')
% Horizontal error bars
for i = 1:length(x)
line([x(i)-x_error(i), x(i)+x_error(i)], [y(i), y(i)], ...
'LineWidth', 3, 'Color', 'r')
line([x(i)-x_error(i), x(i)-x_error(i)], [y(i)-15, y(i)+15], ...
'LineWidth', 3, 'Color', 'r')
line([x(i)+x_error(i), x(i)+x_error(i)], [y(i)-15, y(i)+15], ...
'LineWidth', 3, 'Color', 'r')
end
% Best-fit line
p = polyfit(x, y, 1);
xfit = linspace(min(x), max(x), 100);
yfit = polyval(p, xfit);
plot(xfit, yfit, 'k-', 'LineWidth', 2.5)
% Equation text on graph
equation = sprintf('y = %.4fx + %.4f', p(1), p(2));
text(300, 1600, equation, 'FontSize', 14, 'FontWeight', 'bold')
xlabel('Function Generator (Hz)')
ylabel('Oscilloscope (Hz)')
title('Oscilloscope Frequency vs Function Generator Frequency')
legend('Data points', 'X error bars', 'Line of best fit', 'Location',
'northwest')
grid on
hold off
1
Published with MATLAB® R2026a
2