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

2 次查看(过去 30 天)
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?

回答(1 个)

Neha Talnikar
Neha Talnikar 2014-7-22
“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

类别

Help CenterFile Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by