- 第一题
1 Simple Octave/MATLAB function
The first part of ex1.m gives you practice with Octave/MATLAB syntax and the homework submission process. In the file warmUpExercise.m, you will find the outline of an Octave/MATLAB function. Modify it to return a 5 x5 identity matrix by filling in the following code:
意思是说要写一个warmUpExercise函数 运行该函数会输出一个5*5的单位矩阵
在warmUpExercise函数中进行编写即可
A = eye(5)
- 第二题
2.1 Plotting the Data
进入plotData中输入以下代码即可
plot(x, y, 'rx', 'MarkerSize', 10); % Plot the data
ylabel('Profit in $10,000s'); % Set the y−axis label
xlabel('Population of City in 10,000s'); % Set the x−axis label
- 第三题
2.2.3 Computing the cost J(θ) 在computeCost.m,中计算代价函数。
由定义可以知道
代码为
predict = X * theta;
sqrError = (predict-y).^2;
J = 1/(2*m)*sum(sqrError);
- 第四题
Gradient descent
代码
group = X'*(X*theta-y);
theta = theta-(alpha/m)*group;
注意:在进行矩阵的乘法时 假设A(m*n)*B(k*q) m一定要等于q 否则会报错