function gradient = meanSquaredGradient(x, y, parameter) % The gradient of the mean squared error % % Arguments: x, vector of the input values % y, vector of the corresponding measured output values % parameter, vector containing slope and intercept % as the 1st and 2nd element % % Returns: the gradient as a vector with two elements h = 1e-5; % stepsize for derivatives mse = meanSquaredError(x, y, parameter); partial_m = (meanSquaredError(x, y, [parameter(1)+h, parameter(2)]) - mse)/h; partial_b = (meanSquaredError(x, y, [parameter(1), parameter(2)+h]) - mse)/h; gradient = [partial_m, partial_b]; end