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

1 Ansicht (letzte 30 Tage)
alpedhuez
alpedhuez am 5 Aug. 2020
Kommentiert: alpedhuez am 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?

Akzeptierte Antwort

Alan Weiss
Alan Weiss am 5 Aug. 2020
Bearbeitet: Alan Weiss am 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
  4 Kommentare
Steven Lord
Steven Lord am 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
alpedhuez am 5 Aug. 2020
using breakpoints now.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by