Setting up properly the fminunc function

2 ビュー (過去 30 日間)
ektor
ektor 2019 年 5 月 27 日
コメント済み: ektor 2019 年 5 月 28 日
Dear all,
I am trying to maximize this function
T=1000;
z=randn(T,1);
u=randn(T,1);
k1=0.01;
k2=0.01;
options=optimset('LargeScale','off','display','off','TolFun',0.0001,'TolX',0.0001,...
'GradObj','off', 'Hessian','off','DerivativeCheck','off');
for t=1:T
[x,fval,exitflag,output,G_sum,H]=fminunc('funct',u(t),options,...
z,k1, k2,t, u,T);
end
where
function LL= funct(x,z,k1, k2,t, u,T)
if t==1
u=[x; u(2:T) ];
elseif t==T
u=[u(1:T-1);x ];
else
u=[u(1:t-1);x;u(t+1:T) ];
end
e2=(z-u).^2;
kk= - 0.5*x^2/k2;
LL = -( -.5/k1*sum(e2(t:T))+kk) ;
end
However, I am not sure if the function is properly set up in terms of 'x'. As you can see 'x' changes position within 'u'.
I suspect that an alternative approach would be
function LL= funct(x,z,k1, k2,t, u,T)
u(t)=x;
e2=(z-u).^2;
kk= - 0.5*x^2/k2;
LL = -( -.5/k1*sum(e2(t:T))+kk );
end
Which of the two is more efficient? Is there any alternative solution?
Thanks in advance.
  3 件のコメント
ektor
ektor 2019 年 5 月 27 日
編集済み: ektor 2019 年 5 月 27 日
Dear Walter,
Thank you for this link.
I am not sure how to do that.
It should be something like:
function y = funn(z,k1, k2,t, u,T, valueofx)
function LL= funct(x)
u(t)=x;
e2=(z-u).^2;
kk= - 0.5*x^2/k2;
LL = -( -.5/k1*sum(e2(t:T))+kk );
end
end
ektor
ektor 2019 年 5 月 27 日
Any ideas, please?

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

採用された回答

Walter Roberson
Walter Roberson 2019 年 5 月 27 日
T=1000;
z=randn(T,1);
u=randn(T,1);
k1=0.01;
k2=0.01;
options = optimset('LargeScale','off','display','off','TolFun',0.0001,'TolX',0.0001,...
'GradObj','off', 'Hessian','off','DerivativeCheck','off');
x = zeros(1, T); fval = zeros(1,T); exitflag = zeros(1,T);
output = cell(1,T); G_sum = cell(1,T); H = cell(1,T);
for t = 1:T
[x(t), fval(t), exitflag(t) ,output{t}, G_sum{t}, H{t}] = fminunc(@(x) funct(x, z, k1, k2, t, u, T), u(t), options);
end
function LL = funct(x, z, k1, k2, t, u, T)
u(t) = x;
e2 = (z-u).^2;
kk = - 0.5*x^2/k2;
LL = -( -.5/k1*sum(e2(t:T))+kk );
end
If you only include e2(t:T) in your sum, then it is not clear to me why you are calculating e2 over the whole vector? Why not, for example,
for t = 1:T
[x(t), fval(t), exitflag(t) ,output{t}, G_sum{t}, H{t}] = fminunc(@(x) funct(x, z, k1, k2, u(t:end)), u(t), options);
end
function LL = funct(x, z, k1, k2, urest)
urest(1) = x;
e2 = (z-urest).^2;
kk = - 0.5*x^2/k2;
LL = -( -.5/k1*sum(e2)+kk );
end
  3 件のコメント
Walter Roberson
Walter Roberson 2019 年 5 月 28 日
The first of those calculates for all t values, throwing away all of the intermediate results and keeping only the last result. It would not leave you with an x for t = 1, an x for t = 2, and so on, only with an x for t = T.
ektor
ektor 2019 年 5 月 28 日
Thank you very much Walter!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by