Asked by Giorgos Papakonstantinou
on 25 Jan 2013

I have a function which has many outputs. I would like to find the minimum of one of the outputs by optimizing the function. Since I have many outputs I suspect that the optimization will not be successful. So my function is of the form:

function [A, B, C]=myfun(a,b,c)

and I would like to find the minimum only of the output A. Then since I the optimized values for a,b,c I would like to call again the function to find the values also of B and C.

So I would like to do:

[Amin, Bopt, Copt]=myfun(aopt, bopt, copt)

Do you know how can implement it?

Answer by Matt J
on 25 Jan 2013

Edited by Matt J
on 25 Jan 2013

Any of the solvers can do this, e.g.

fun=@(x) myfun(x(1), x(2), x(3));

initialpoint=[aguess,bguess, cguess];

x = fminsearch(fun , initialpoint);

[Amin, Bopt, Copt]=fun(x);

Matt J
on 25 Jan 2013

Of course, you could have avoided the need to define

fun=@(x) myfun(x(1), x(2), x(3));

if you had defined myfun to accept one vector input [a,b,c] instead of 3 separate scalar inputs.

Matt J
on 25 Jan 2013

**Inside my fun there is only one output which I would like to optimize and depends on the vector x. The thing is that myfun is saved in separate script and has lots of outputs. Since I want the minimization of only one of the outputs I guess that the way I showed is not correct.**

As long as the first output of myfun is the value you are optimizing, and as long as you are not using the option 'GradObj'='on', it should be fine.

Matt J
on 25 Jan 2013

**Well I could place it first but that is not what I am looking for.**

Then wrap it in a function that does place it first.

Answer by Giorgos Papakonstantinou
on 25 Jan 2013

Basically I would like from another script to execute the optimization. So the function is saved in another script. When I do the optimization I am creating a function handle with the following way:

a=3; b=7; c=10; d=9; f=@(x)myfun(x, a, b, c, d); x0=[1, 2, 3]; A=zeros(1,3); A(1,1)=2; A(1,2)=0.5; b=-1; [xopt,fmin]=fmincon(p,x0,A,b)

I am passing parameters a,b,c,d. x is the variable vector. Inside my fun there is only one output which I would like to optimize and depends on the vector x. The thing is that myfun is saved in separate script and has lots of outputs. Since I want the minimization of only one of the outputs I guess that the way I showed is not correct.

Answer by Giorgos Papakonstantinou
on 25 Jan 2013

Well I could place it first but that is not what I am looking for. Anyway thank you Matt.

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 0 Comments