how to put a vector in function
1 view (last 30 days)
Show older comments
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 Comment
John D'Errico
on 15 Aug 2014
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.
Answers (1)
Star Strider
on 15 Aug 2014
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 Comments
Star Strider
on 15 Aug 2014
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.
See Also
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!