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

조회 수: 2 (최근 30일)
alpedhuez
alpedhuez 2020년 8월 5일
댓글: alpedhuez 2020년 8월 5일
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일
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
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개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by