how to put a vector in function
古いコメントを表示
hello all;
I want to write a function like below, and minimum it with "fminsearch" but I cant!
function F_m_p_cmp=fmp_cmp(cc)
mm=3
for ii=1:mm
for t=1:24
ex(ii,t)=cc(t)*(Uu(1,t,ii)+Uu(2,t,ii)+Uu(3,t,ii))+sum(Cc(:,t,ii))
end
end
expp=p*ex;
eexp=exp(expp);
F_m_p_cp=(1/p)*log10(sum(ex));
end
" cc" should be a 1 by 24 vector. could you please help me?
1 件のコメント
John D'Errico
2014 年 8 月 15 日
Note that you will never be happy with trying to solve a 24 variable problem with fminsearch. It simply is NOT designed for that many unknowns. Use a better optimizer, such as fmincon or fminunc.
回答 (1 件)
Star Strider
2014 年 8 月 15 日
You have to define ‘cc’ as a (1x24) vector as your initial parameter estimate vector ( x0 in the documentation ) to fminsearch.
You also have to pass Uu, Cc and p to your ‘fmp_cpm’ function. If Uu, Cc and p are already in your workspace, I would create an anonymous objective function to do that.
First, rewrite your function statement as:
function F_m_p_cmp=fmp_cmp(cc, Uu, Cc, p)
then create your objective function as:
objfcn = @(cc) fmp_cmp(cc, Uu, Cc, p);
so your call to fminsearch becomes:
cc0 = rand(1,24); % Choose whatever (1x24) vector for ‘cc’ you want
cc_est = fminsearch(objfcn, cc0);
No promises because I can’t run your code, but this should get you started.
6 件のコメント
Maryamm Bodaghi
2014 年 8 月 15 日
Star Strider
2014 年 8 月 15 日
First, you need to change this line:
function F_m_p_cmp=fmp_cmp(cc,Uu,Cc,m,p)
to:
function F_m_p_cmp=fmp_cmp(cc,Uu,Cc,mm,p)
note the change from m to mm, and then eliminate this line in that function:
mm=1
because it will replace the value of mm in the argument list.
The objective function I wrote then becomes:
objfcn = @(cc) fmp_cmp(cc, Uu, Cc, mm, p);
The rest of my code stays the same. Make these changes, run your code, and see if fminsearch produces acceptable results. The output of fminsearch should be your optimised ‘cc’ parameters.
I do not know what you are doing in the rest of your code. I want to get fminsearch working with it, since that is what you asked.
Maryamm Bodaghi
2014 年 8 月 15 日
Star Strider
2014 年 8 月 15 日
Change this line in your function:
F_m_p_cp=(1/p)*log10(sum(eexp));
to:
F_m_p_cmp=(1/p)*log10(sum(eexp));
Now run it and see if it works.
Maryamm Bodaghi
2014 年 8 月 15 日
Star Strider
2014 年 8 月 15 日
My pleasure!
You cannot set constraints with fminsearch. You can normalise them inside your fmp_cmp function with:
cc = cc/sum(cc);
as the first statement in the function, but this is not actually constraining them. I can’t help you with delta. I have no idea what you are doing.
If you want to optimise with constraints, you will have to use one of the Optimization Toolbox solvers such as fmincon.
カテゴリ
ヘルプ センター および File Exchange で Tuning Goals についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!