octave 无约束最小化函数fminunc
被调用的函数costFunction.m为:
function [jVal,gradient] = costFunction(theta) jVal = (theta(1)-5)^2 + (theta(2)-5)^2; gradient = zeros(2,1); gradient(1) = 2*(theta(1)-5); gradient(2) = 2*(theta(2)-5);
octave程序为:
options = optimset('GradObj','on','MaxIter','100') initialTheta=zeros(2,1) [optTheta,functionVal,exitFlag] = fminunc(@costFunction,initialTheta,options)
输出为:
options =
scalar structure containing the fields:
GradObj = on
MaxIter = 100
initialTheta =
0
0
optTheta =
5.0000
5.0000
functionVal = 1.5777e-030
exitFlag = 1
8月2日: 还是不太理解这个函数和回归,因为theta选取不好会出现很坏的结果,可能是非凹的? 尚不明白.