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

Not z but y.
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
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.
using breakpoints now.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

태그

질문:

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