while using FMINCON can we pass matrices as arguments while using calling objective function saved as seperate file

4 views (last 30 days)
I'm using FMINCON . I saved my objective function in a seperate file. can i pass matrices as arguments while calling the objective function from main program.? if yes can i access the individual elements of the matrix?
call from main program is given below
[x,fval]=fmincon(@(x)fun(x,i,EC,PVpow,Pabrmp,Pplc,Psm,V1ref,k),x0,A,b,Aeq,beq,lb,ub,@(x)nonlcon(x,i,SOC1,PVpow,Pabrmp,Pplc,Psm,V1ref,k));
here PVpow,Pabrmp,Psm,Pplc are matrices

Accepted Answer

John D'Errico
John D'Errico on 20 Feb 2017
Yes. You can pass in any arguments you wish, matrices or not.
If you can pass them into a function, why would you think you cannot access individual elements of those matrices? Of course you can.
It seems apparent that you hd a problem, because otherwise, this question makes little sense. But if you got an error, then show it.
The function handle that you show:
@(x)fun(x,i,EC,PVpow,Pabrmp,Pplc,Psm,V1ref,k),
encapsulates the current value of those variables from the caller workspace, passing them into fun. fun can use those arguments in any way you wish.
  3 Comments
Krishna prasad K
Krishna prasad K on 20 Feb 2017
thanks again for your time sorry for that screen shot :)
as per your instruction i used curl brackets to access the elements of the cell array. then the following error is shown
Cell contents reference from a non-cell array object.
Error in fun (line 2)
f=EC{i+1}*{1/3}*{Pabrmp{i+1}+Psm{i+1}+Pplc{i+1}-PVpow{i+1}-x{k-i}-x{k-2*i+72}}+.055*(1/3)*x{k-2*i+72} +
EC{i+2}*{1/3}*{Pabrmp{i+2}+Psm{i+2}+Pplc{i+2}-PVpow{i+2}-x{k-i+1}-x{k+1-2*i+72}}+.055*(1/3)
Error in
@(x)fun(x,i,EC,PVpow,Pabrmp,Pplc,Psm,V1ref,k)
Error in fmincon (line 601)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in predprog (line 50)
[x,fval]=fmincon(@(x)fun(x,i,EC,PVpow,Pabrmp,Pplc,Psm,V1ref,k),x0,A,b,Aeq,beq,lb,ub,@(x)nonlcon(x,i,SOC1,PVpow,Pabrmp,Pplc,Psm,V1ref,k));
Caused by: Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.
>>

Sign in to comment.

More Answers (1)

Krishna prasad K
Krishna prasad K on 20 Feb 2017
thanks again for your time sorry for that screen shot :)
as per your instruction i used curl brackets to access the elements of the cell array. then the following error is shown
Cell contents reference from a non-cell array object.
Error in fun (line 2)
f=EC{i+1}*{1/3}*{Pabrmp{i+1}+Psm{i+1}+Pplc{i+1}-PVpow{i+1}-x{k-i}-x{k-2*i+72}}+.055*(1/3)*x{k-2*i+72} +
EC{i+2}*{1/3}*{Pabrmp{i+2}+Psm{i+2}+Pplc{i+2}-PVpow{i+2}-x{k-i+1}-x{k+1-2*i+72}}+.055*(1/3)
Error in
@(x)fun(x,i,EC,PVpow,Pabrmp,Pplc,Psm,V1ref,k)
Error in fmincon (line 601)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in predprog (line 50)
[x,fval]=fmincon(@(x)fun(x,i,EC,PVpow,Pabrmp,Pplc,Psm,V1ref,k),x0,A,b,Aeq,beq,lb,ub,@(x)nonlcon(x,i,SOC1,PVpow,Pabrmp,Pplc,Psm,V1ref,k));
Caused by: Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.
>>

Community Treasure Hunt

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

Start Hunting!