calculate a regression line of height and weight without using corrcoef in matlab

You can calculate the regression line of height and weight in Matlab by using the least squares method.

First, you need to load in the data for height and weight as arrays. Let's call them x and y, respectively.

main.m
x = [68, 71, 61, 69, 71, 58, 72, 73, 58, 74];
y = [165, 201, 140, 170, 192, 125, 195, 205, 115, 210];
102 chars
3 lines

Next, you need to calculate the mean of x and y.

main.m
x_mean = mean(x);
y_mean = mean(y);
36 chars
3 lines

Then, you can calculate the slope of the regression line (m) using the formula:

main.m
m = sum((x - x_mean) .* (y - y_mean)) / sum((x - x_mean).^2);
62 chars
2 lines

Finally, you can calculate the intercept of the regression line (b) using the formula:

main.m
b = y_mean - m * x_mean;
25 chars
2 lines

And the regression line is given by:

main.m
y_reg = m * x + b;
19 chars
2 lines

You can plot the original data and regression line to visualize the fit:

main.m
plot(x, y, 'o');
hold on;
plot(x, y_reg);
xlabel('Height');
ylabel('Weight');
legend('Data', 'Regression Line');
113 chars
7 lines

This will output a scatter plot with the regression line overlaid on top.

gistlibby LogSnag