how to put a vector in function

조회 수: 4 (최근 30일)
Maryamm Bodaghi
Maryamm Bodaghi 2014년 8월 15일
댓글: John D'Errico 2014년 8월 15일
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
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
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
Maryamm Bodaghi 2014년 8월 15일
thanks so much,yes it works.
how can I set constraints? sum(cc)=1 && 0<=cc<=1
and also,tolerance of delta?
could you please help me?
Star Strider
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Nonlinear Least Squares (Curve Fitting)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by