Minimize second output of a function with respect to a variable.

3 visualizaciones (últimos 30 días)
Peter Laslo
Peter Laslo el 21 de Jul. de 2014
Respondida: Neha Talnikar el 22 de Jul. de 2014
Say I have an function of several variables with vector output, say, [a,b]=myfun(x,y,z);
Now, say I want to create a function that passes the parameters y and z and minimizes myfun with respect to x, so that the first output is minimized. So I could do something such as this:
function c = myfun2(x,y)
x0=1;
c = fminunc(@(x)myfun(x,y,x),x0)
end
But what if want to create a function, say fun3, that passes x and y as in fun2 but so that the second output of myfun is minimized. How could I do that without redefining myfun (or creating a new function) in an m file?

Respuestas (1)

Neha Talnikar
Neha Talnikar el 22 de Jul. de 2014
“fminunc” expects the objective function to return a scalar.
To minimize the objective function with respect to the second output of the function “myfun”, you could write another function which calls the “myfun” and returns the second output only.
function out = secondArgument(x,y,z)
[~,out] = myfun(x,y,z);
end
This function could then be used for minimizing as follows:
function c = myfun2(y,z)
x0=1;
c = fminunc(@(x) secondArgument(x,y,z),x0)
end

Categorías

Más información sobre Get Started with Optimization Toolbox en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by