Skip to content

Commit d7e8054

Browse files
committed
Added gradient descent
1 parent 6ee40b4 commit d7e8054

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
% This function demonstrates gradient descent in case of linear regression with one variable.
2+
3+
% Theta is a column vector with two elements which this function returns after modifying it.
4+
5+
% This function receives the feature vector x, vector of actual target variables Y, Theta
6+
7+
% containing initial values of theta_0 and theta_1, learning rate Alpha, number of iterations
8+
9+
% noi.
10+
11+
function Theta = gradientdescent(x, Y, Theta, Alpha, noi)
12+
13+
n = length(Y); % Number of training examples.
14+
15+
for i = 1:noi
16+
17+
theta_1 = Theta(1) - Alpha * (1 / n) * sum(((x * Theta) - Y) .* x(:, 1)); % Temporary variable to simultaneously update theta_0 but i have used 1 to avoid confusion since indexing in MATLAB/Octave starts from 1.
18+
19+
theta_2 = Theta(2) - Alpha * (1 / n) * sum(((x * Theta) - Y) .* x(:, 2)); % Temporary variable to simultaneously update theta_1.
20+
21+
Theta(1) = theta_1; % Assigning first temporary value to update first actual value simultaneously.
22+
23+
Theta(2) = theta_2; % Assigning second temporary value to update second actual value simultaneously.
24+
25+
end
26+
27+
end

0 commit comments

Comments
 (0)