create a linear regression that finds r and r^2 in matlab using a function in matlab

main.m
% Example data
x = [1:5];
y = [2 3 5 4 6];

% Getting the coefficients for the linear regression
coefficients = polyfit(x, y, 1);
m = coefficients(1);
b = coefficients(2);

% Calculating the predicted values
y_pred = m*x + b;

% Calculating R and R^2
r = corrcoef(x, y);
R = r(1,2);
R_squared = R^2;

disp(['R value is: ', num2str(R)]);
disp(['R^2 value is: ', num2str(R_squared)]);
383 chars
20 lines

gistlibby LogSnag