Problem with fminunc, why does the parameters not change?

Hi,
may be is a very simple mistake but I cant see it. I have a simple model to try how fminunc works and the thing is I dont get different parameters from the initial ones.
this is my code:
function main2()
pars0=[0.2 0.3 0.4]';
objective=@(x)li2(x);
[x,fval,exitflag,output,grad,hessian]= fminunc(objective,pars0);
disp(x);
disp(fval);
disp(exitflag);
end
function [ilik]=li2(x);
mData=randn(50,1);
iT=size(mData,1);
lik=zeros(iT,1);
hxt=zeros(iT,1);
Yt=0;
xt=0;
for i = 1 : iT
Yt=mData(i,1);
F=x(3,1)*Yt^2+x(1,1)*Yt+x(2,1);
hxt(i,1)=F;
lik(i,1) = -(0.5)*iT*log(2*pi)-0.5*F;
end
ilik = -sum(lik);
end
Thanks a lot in advance.

 採用された回答

Matt J
Matt J 2014 年 5 月 7 日
編集済み: Matt J 2014 年 5 月 7 日

0 投票

That is not how your code behaves for me. I do see a small change between pars0 and the final solution:
>> x-pars0
ans =
1.0e-05 *
0.0872
-0.0150
0.1102
However, it makes no sense to have randn() statements in your objective function. Doing so means the objective function is not a deterministic function of the input x. How can the iterative minimization algorithm know what to minimize if the definition of the function is changing randomly with every iteration?

1 件のコメント

Matt J
Matt J 2014 年 5 月 7 日
編集済み: Matt J 2014 年 5 月 7 日
You probably meant to do something like the following. Notice that it keeps the random data fixed while the minimization is in progress. However, without constraints, the minimum is simply -Inf.
pars0=[0.2 0.3 0.4]';
mData=randn(50,1);
objective=@(x)li2(x,mData);
[x,fval,exitflag,output,grad,hessian]= fminunc(objective,pars0);
disp(x);
disp(fval);
disp(exitflag);
keyboard
function [ilik]=li2(x,mData)
iT=size(mData,1);
lik=zeros(iT,1);
hxt=zeros(iT,1);
Yt=0;
xt=0;
for i = 1 : iT
Yt=mData(i,1);
F=x(3,1)*Yt^2+x(1,1)*Yt+x(2,1);
hxt(i,1)=F;
lik(i,1) = -(0.5)*iT*log(2*pi)-0.5*F;
end
ilik = -sum(lik);

サインインしてコメントする。

その他の回答 (1 件)

Barbara
Barbara 2014 年 5 月 14 日

0 投票

Dear Matt J
Yes the mistake was that I put the random generator of the data inside the function. I see it now. Thanks!

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

タグ

質問済み:

2014 年 5 月 7 日

回答済み:

2014 年 5 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by