Error in plotting Cost Function as a function of iterations

16 次查看(过去 30 天)
Hello all. I've been trying to implement Linear Regression with 2 features using Gradient Descent. The Gradient Descent works well numerically leading to optimal values of the Weight matrix and continuously decreasing Cost function with increasing number of iterations. But when I try to plot the graph of J vs iterations, I don't get the desired graph. Instead, all values upto the secondlast value are zero and last value is the actual minimum value of the cost function. So how do i fix this? Here's my code:
For Gradient Descent
function [W ,J_list] = gradDescent(W, X, y, alpha, num_iters)
m = length(y);
n = length(W);
J_list = zeros(num_iters,1);A = J_list;
T0 = 0;T1 = 0;T2 = 0;
for i=1:num_iters
T0 = W(1) -alpha*(((X * W) -y)' * X(:, 1));
T1 = W(2) -alpha*(((X * W) -y)' * X(:, 2));
T2 = W(3) -alpha*(((X * W) -y)' * X(:, 3));
W(1) = T0;
W(2) = T1;
W(3) = T2;
J_list(num_iters) = costFunction(X, y, W);
fprintf('[%.0f %.0f %.0f %.0f] \n', [W(1) W(2) W(3) J_list(num_iters)]);
end
plot(1:numel(J_list), J_list, '-b', 'LineWidth', 2);
end
For the Cost Function:
function [J] = costFunction(X,y, W)
m = length(y); %The number of training examples
n = length(W);
J = 0;
J = J + 0.5*sum((X*W-y).*(X*W-y));
end
Here is Feature Normalization:
function [X_norm, mu, sigma] = featureNormalize(X)
X_norm = X;
mu = zeros(1, size(X, 2));
sigma = zeros(1, size(X, 2));
mu = mean(X,1);
sigma = std(X,1);
for i = 1:size(X,1)
X_norm(i,:) = (X_norm(i,:) - mu)./(sigma);
end
And here is my main code:
clc; clear all; close all;
data = load('data.txt');
X = data(:,1:2);
y = data(:,3);
m = length(y);
[X mu sigma] = featureNormalize(X);
X = [ones(m,1), X];
W = rand(size(X,2),1)*10;
J = costFunction(X,y,W);
iterations =500;
alpha = 0.00001;
[W,J_list] = gradDescent(W,X,y,alpha,iterations);
All help is appreciated. TIA.
  1 个评论
Koustubh Phalak
Koustubh Phalak 2018-8-20
I forgot to put up the plot and the actual numerical visualization of the cost function. So here are the values:

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2018-8-20
编辑:Stephen23 2018-8-20
You used the wrong index here:
J_list(num_iters) = = costFunction(...)
the index should be i.
  3 个评论
Stephen23
Stephen23 2018-8-21
编辑:Stephen23 2018-8-21
@Koustubh Phalak: I hope that it helps. Don't forget to accept the answer that best helped to solve your original question. Accepting answers is the easiest way to show your appreciation to the volunteers who help you on this forum.
Koustubh Phalak
Koustubh Phalak 2018-8-21
@Stephen Cobeldick Oops, I didn't know about this since I am new to this website :P

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by