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

9 views (last 30 days)
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?

Answers (1)

Neha Talnikar
Neha Talnikar on 22 Jul 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

Products

Community Treasure Hunt

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

Start Hunting!