为什么fminsearch和fminunc在进行优化时会给出不同的答案?

时间:2016-09-12 13:24:02

标签: matlab

这个问题可能与数学理论有关,或者可能没有,我还是想在这里问一下。我有一个TESTCase.m如下:

function ret=TESTCase(Input,k)

if k==1
    if Input(2)~=1
        Input(2)=1;
        ret=(Input(1)-2).^2+5.*Input(2).^2;
    end
else if k~=1
        ret=(Input(1)-2).^2+(Input(2)-Input(1)).^2;
    end
end

然后,我尝试使用fminunc和fminsearch如下:

TEST=@(x)TESTCase(x,1); fminsearch(TEST,[5,3]); fminunc(TEST,[5,3]);

我分别得到两个答案: 2.0000 3.9000(使用fminsearch)和2.0000 3.0000(使用fminunc)

我知道在我定义的函数下,这两个答案是正确的,但我想知道为什么在使用fminsearch时它会改变第二个值,但在使用fminunc时没有显示任何变化。这对我来说很奇怪。此外,使用fminsearch时,不会显示任何消息,但使用fminunc会显示以下消息:

Warning: Gradient must be provided for trust-region algorithm;
  using line-search algorithm instead. 

 In fminunc at 382 

Local minimum found.

Optimization completed because the size of the gradient is less than
the default value of the function tolerance.

<stopping criteria details>。有什么区别?我知道fminsearch是衍生免费而fminunc不是,但如果消息输出,则应该意味着fminunc在这种情况下也是衍生免费的;在这种情况下,它们应该是等同的。

1 个答案:

答案 0 :(得分:0)

fminuncfminsearch使用不同的衍生免费算法:fminunc使用某种simplex search方法,fminunc使用line search。 由于正确选择的下降方向Iteration Func-count f(x) Step-size optimality 0 3 14 6 1 6 9 0.166667 4 2 9 5 1 0 在两次迭代中找到最小值:

fminsearch

在这种情况下,单纯形法并不是最佳的。 Iteration Func-count min f(x) Procedure 0 1 14 1 3 14 initial simplex 2 5 11.25 expand 3 6 11.25 reflect 4 8 7.25 expand 5 9 7.25 reflect 6 11 5.25 reflect 7 12 5.25 reflect 8 14 5 contract outside 9 15 5 reflect 10 17 5 contract inside 11 19 5 contract inside 12 21 5 contract inside 13 23 5 contract inside 14 25 5 contract inside 15 27 5 contract inside 16 29 5 contract inside 17 31 5 contract inside 18 33 5 contract inside 19 35 5 contract inside 20 37 5 contract inside 21 39 5 contract inside 22 41 5 contract inside 23 43 5 contract inside 24 45 5 contract inside 25 47 5 contract inside 26 49 5 contract inside 27 51 5 contract inside 28 53 5 contract inside 29 55 5 contract inside 30 57 5 contract inside 31 59 5 contract inside 32 61 5 contract inside 33 63 5 contract inside 34 65 5 contract inside 35 69 5 shrink 36 73 5 shrink 37 77 5 shrink 38 81 5 shrink 39 85 5 shrink 40 89 5 shrink 41 93 5 shrink 42 97 5 shrink 43 101 5 shrink 44 105 5 shrink 45 109 5 shrink 的优化详情如下:

fminsearch

很难说为什么2.0000 3.9000的结果正是<?php if (!empty($errors)) { ?> <div class="errors"> <h5>Hay <?php echo count($errors); ?> Errores:</h5> <ul> <?php foreach ($errors as $field => $error) { ?> <li style="color:red;"><?php echo $error[0]; ?></li> <?php } ?> </ul> </div> <?php } echo $this->Form->create('Pagos'); $i = 0; ?> <div class="data"> <!-- venc.ref, venc.proveedor, venc.moneda, venc.vencimiento, venc.pago, venc.monto, venc.concepto, venc.id --> <table class="table"> <thead> <tr> <th>Referencia</th> <th>Proveedor</th> <th>Moneda</th> <th>vencimiento</th> <th>Status</th> <th>Monto</th> <th>Concepto</th> <th>Seleccione</th> </tr> </thead> <tbody> <?php foreach ($datos as $d) : ?> <tr> <td><?php echo $d['venc']['ref']; ?></td> <td><?php echo $d['venc']['proveedor']; ?></td> <td><?php echo $d['venc']['moneda']; ?></td> <td><?php echo implode('/', array_reverse(explode('-', $d['venc']['vencimiento']))); ?></td> <td><?php echo $d['venc']['pago']; ?></td> <td><?php echo number_format($d['venc']['monto'],2,',','.'); ?></td> <td><?php echo $d['venc']['concepto']; ?></td> <td><?php echo $this->Form->input('Pagos][.id', array('label' => false, 'type' => 'hidden', 'value' => $d['venc']['id'])); echo $this->Form->input('Pagos][.monto', array('label' => false, 'type' => 'hidden', 'value' => $d['venc']['monto'])); echo $this->Form->input('Pagos][.moneda_id', array('label' => false, 'type' => 'hidden', 'value' => (strlen($d['venc']['moneda'])>1)?2:1 )); echo $this->Form->input('Pagos][.selec', array('type' => 'checkbox', 'label' => false)); ?></td> </tr> <?php endforeach; ?> </tbody> </table> </div> <div class="row-form"> <div class="span2 offset3"> <?php echo $this->Js->submit('Pagar', array('class' => 'btn btn-info', 'update' => '#contentWrap', 'id' => 'pagar')); ?> </div> </div> <?php if (false != $saved) echo "<script> $('#dialogModal4').dialog('close'); </script>"; echo $this->Form->end(); echo $this->Js->writeBuffer(); //assuming this view is rendered without the default layout, make sure you write out the JS buffer at the bottom of the page ?> 。通往最低限度的方法并不容易......