create best fit line for this data: hs = [0 .5 .75 1 1.1 1.7 2 2.5 3.1 3.6 4 4.6 5.1 5.2 5.8 6.1 6.4 6.5]; grade = [30 35 38 42 47 50 55 58 61 68 77 80 83 84 89 94 92 98]; in matlab

main.m
% Data
hs = [0, 0.5, 0.75, 1, 1.1, 1.7, 2, 2.5, 3.1, 3.6, 4, 4.6, 5.1, 5.2, 5.8, 6.1, 6.4, 6.5];  
grade = [30, 35, 38, 42, 47, 50, 55, 58, 61, 68, 77, 80, 83, 84, 89, 94, 92, 98];  

% Fit a polynomial regression line
p = polyfit(hs, grade, 1);  % linear regression (degree 1 polynomial)
f = polyval(p, hs);

% Plot
scatter(hs, grade, 'b'); % scatter plot
hold on
plot(hs, f, 'r'); % fitted line
hold off

% Display the equation of the best fit line
fprintf('The best fit line equation is: y = %.2fx + %.2f\n', p(1), p(2));
525 chars
17 lines

This code fits a linear regression line to the given data points and plots the data points along with the best fit line. It also displays the equation of the best fit line.

related categories

gistlibby LogSnag