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

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
Alan Weiss 2020 年 8 月 5 日
編集済み: Alan Weiss 2020 年 8 月 5 日

0 投票

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 件のコメント

alpedhuez
alpedhuez 2020 年 8 月 5 日
Not z but y.
Alan Weiss
Alan Weiss 2020 年 8 月 5 日
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
Steven Lord 2020 年 8 月 5 日
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 2020 年 8 月 5 日
using breakpoints now.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

タグ

質問済み:

2020 年 8 月 5 日

コメント済み:

2020 年 8 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by