# How can one write a program to do this? (fminbnd)

2 views (last 30 days)
alpedhuez on 5 Aug 2020
Commented: alpedhuez on 5 Aug 2020
I define a function
function out = test(x)
y=x^2;
z=y+1;
out=z
end
Then I run
fminbnd(@(x) test(x),0,1)
This will return the value of x that maxes test(x). But I also want to output y at that optimal. How can one do this?

Alan Weiss on 5 Aug 2020
Edited: Alan Weiss on 5 Aug 2020
You can get the value of out = z simply by asking for it:
[xbest,out] = fminbnd(@(x) test(x),0,1)
Alan Weiss
MATLAB mathematical toolbox documentation

Show 1 older comment
Alan Weiss on 5 Aug 2020
I am not sure what you want. You can calculate y separately in a function. Your function does not output y.
Alan Weiss
MATLAB mathematical toolbox documentation
Steven Lord on 5 Aug 2020
Define your test function to return two outputs, out and y. fminbnd will only ever call your function with one output. After fminbnd returns the "best x" call test with that best x as the input and two outputs.
alpedhuez on 5 Aug 2020
using breakpoints now.