Optimization of one output of a vectorfunction using fminunc

Hi!
Last time I got a very helpful idea. Now I defined a vectorfunction which has several variables x1,x2,x3,u1,u2 and also several outputs y1,y2,y3,...,yN. Now I would like to optimize only one output, i.e. I optimize f.ex. only y3. In my case y3 is a function that I get from y1,y2... .
I tried to find the answer, but mostly fminunc were used to optimize y or y1,y2,y3,..... What is the command in the fminunc solver to optimize only one output?
Thanks

 Accepted Answer

Inside of your objective function, only return that output:
function y3 = myobjfun(x)
% do whatever
y3 = something;
end

5 Comments

Hi! My problem is that I need the other outputs to calculate it. For example my output is like:
if true
y1=cumsum(A .* B );
y2=cumsum(C .* B );
y3=y1+y2;
end
Lets say the function is called [y1,y2,y3]=fun(x,a,b,c). x,a,b,c each are vectors. How I can get the value of x for which the y3 is max or min?
Thanks
As Sean has written, only return that output. Inside your function, y1 and y2 can of course be created.
function y3=myobjfun(x)
y1=...
y2=...
y3=y1+y2;
end
Hi! I changed now the objectivefunction to y3. Zhen I run the fminunc in the other script, I get a failure, which I dont understand: My fminunc code is
if true
V_OC(1)=1130;
V_OC(2)=750;
FoF(1)=0.5;
FoF(2)=0.5;
strnum=1;
x0=[1;1;1;1;1;1;1;1;1;1];
f6=efficiency(x,V_OC,FoF,strnum);
[x,fval]=fminunc(@(x)efficiency(x,V_OC,FoF,strnum),x0);
end
The failure I get is: Error using fminunc (line 332) User supplied objective function must return a scalar value.
What does this mean?
Your efficiency function obviously returns an array. It must be a scalar though. Maybe you actually want to optimize w.r.t. norm(efficiency)?
Hi!
I solved the problem. Thanks!!!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!