被调用的函数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选取不好会出现很坏的结果,可能是非凹的? 尚不明白.

相关文章:

  • 2021-07-18
  • 2021-12-15
  • 2022-01-22
  • 2022-12-23
  • 2021-11-24
  • 2021-08-07
  • 2022-12-23
  • 2021-06-17
猜你喜欢
  • 2021-06-20
  • 2022-12-23
  • 2022-12-23
  • 2021-10-26
  • 2021-10-19
  • 2022-01-01
相关资源
相似解决方案