I got the wrong answer even my code is right. Please help!

6 次查看(过去 30 天)
--------------Main program---------------------------------
X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
theta = zeros(2, 1); % initialize fitting parameters
% Some gradient descent settings
iterations = 1500;
alpha = 0.01;
fprintf('\nTesting the cost function ...\n')
% compute and display initial cost
J = computeCost(X, y, theta);
fprintf('With theta = [0 ; 0]\nCost computed = %f\n', J);
fprintf('Expected cost value (approx) 32.07\n');
--------------Calculating function J---------------------------------
function J = computeCost(X, y, theta)
m = length(y);
h = X * theta;
J = (1/(2*m))*(sum(h-y).^2);
end
-------------------Here is my result----------------------------------------
Testing the cost function ...
With theta = [0 ; 0]
Cost computed = 1653.631660------------------> my result
Expected cost value (approx) 32.07 ----------------> this is the right answer
With theta = [-1 ; 2]
Cost computed = 4359.141958------------------->my result
Expected cost value (approx) 54.24----------------->this is the right answer
------------------------------------------------------------
  17 个评论
supun dassanayake
supun dassanayake 2020-4-17
编辑:Walter Roberson 2020-4-17
The X val are:-
X = [
1.00 6.11
1.00 5.53
1.00 8.52
1.00 7.00
1.00 5.86
1.00 8.38
1.00 7.48
1.00 8.58
1.00 6.49
1.00 5.05
1.00 5.71
1.00 14.16
1.00 5.73
1.00 8.41
1.00 5.64
1.00 5.38
1.00 6.37
1.00 5.13
1.00 6.43
1.00 7.07
1.00 6.19
1.00 20.27
1.00 5.49
1.00 6.33
1.00 5.56
1.00 18.95
1.00 12.83
1.00 10.96
1.00 13.18
1.00 22.20
1.00 5.25
1.00 6.59
1.00 9.25
1.00 5.89
1.00 8.21
1.00 7.93
1.00 8.10
1.00 5.61
1.00 12.84
1.00 6.35
1.00 5.41
1.00 6.88
1.00 11.71
1.00 5.77
1.00 7.82
1.00 7.09
1.00 5.07
1.00 5.80
1.00 11.70
1.00 5.54
1.00 7.54
1.00 5.31
1.00 7.42
1.00 7.60
1.00 6.33
1.00 6.36
1.00 6.27
1.00 5.64
1.00 9.31
1.00 9.45
1.00 8.83
1.00 5.18
1.00 21.28
1.00 14.91
1.00 18.96
1.00 7.22
1.00 8.30
1.00 10.24
1.00 5.50
1.00 20.34
1.00 10.14
1.00 7.33
1.00 6.01
1.00 7.23
1.00 5.03
1.00 6.55
1.00 7.54
1.00 5.04
1.00 10.27
1.00 5.11
1.00 5.73
1.00 5.19
1.00 6.36
1.00 9.77
1.00 6.52
1.00 8.52
1.00 9.18
1.00 6.00
1.00 5.52
1.00 5.06
1.00 5.71
1.00 7.64
1.00 5.87
1.00 5.31
1.00 8.29
1.00 13.39
1.00 5.44];
Y:-
Y = [
17.59
9.13
13.66
11.85
6.82
11.89
4.35
12.00
6.60
3.82
3.25
15.51
3.16
7.23
0.72
3.51
5.30
0.56
3.65
5.39
3.14
21.77
4.26
5.19
3.08
22.64
13.50
7.05
14.69
24.15
-1.22
6.00
12.13
1.85
6.54
4.56
4.12
3.39
10.12
5.50
0.56
3.91
5.39
2.44
6.73
1.05
5.13
1.84
8.00
1.02
6.75
1.84
4.29
5.00
1.42
-1.42
2.48
4.60
3.96
5.41
5.17
-0.74
17.93
12.05
17.05
4.89
5.74
7.78
1.02
20.99
6.68
4.03
1.28
3.34
-2.68
0.30
3.88
5.70
6.75
2.06
0.48
0.20
0.68
7.54
5.34
4.24
6.80
0.93
0.15
2.82
1.85
4.30
7.20
1.99
0.14
9.06
0.62];

请先登录,再进行评论。

回答(1 个)

supun dassanayake
supun dassanayake 2020-4-17
hai.
I am fasing the same problem.
Can you help me to solve this problem
please..

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by