Maximising a function that i've created

조회 수: 1 (최근 30일)
Groat
Groat 2015년 2월 14일
댓글: Stephen23 2015년 2월 15일
So I have created a function, f(quantity), and I would like to minimise this with respect to quantity.
Error in Maximisation (line 15):
x = fminbnd(findvalue(quantity), 2, 10)
Any help on why my approach is wrong?
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2015년 2월 14일
Sam - please provide the full error message and describe your inputs. What does your function quantity do?
Groat
Groat 2015년 2월 15일
Hi Geoff, the whole error message:
Error using fcnchk (line 106)
FUN must be a function, a valid string expression, or an inline function object.
Error in fminbnd (line 181)
funfcn = fcnchk(funfcn,length(varargin));
Error in Maximisation (line 15)
x = fminbnd(findvalue(quantity),2,10)
so quantity is a variable and findvalue(quantity) outputs a single number (after doing some calculations using the quantity variable - I have a summation in there, and some multiplication). Do you need to see the findvalue function as well?
Thanks for the help.

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

답변 (1개)

Stephen23
Stephen23 2015년 2월 15일
편집: Stephen23 2015년 2월 15일
It looks like you are not actually providing a function to fminbnd , as its syntax requires.
If findvalue is your function, then findvalue(quantity) is probably returning a value, which you then supply to fminbnd in the line x = fminbnd(findvalue(quantity),...).
However the documentation of fminbnd states that it requires a function handle, so you need to supply it with a function handle . It might be sufficient if you try this:
x = fminbnd(findvalue, 2, 10)
Or perhaps
x = fminbnd(@findvalue, 2, 10)
depending on how you have defined your function findvalue.
PS: Also note that maximizing a function is the same as minimizing its negative.
  댓글 수: 3
Groat
Groat 2015년 2월 15일
Although I don't think the minimisation is actually working. I've done a for loop (manually calculating the maximum) to test, and with a lot of ranges the fminbnd result is wrong.
Is this a result of my function, or is there a better optimisation method to use?
Stephen23
Stephen23 2015년 2월 15일
The @ creates a function handle .
"Is this a result of my function, or is there a better optimisation method to use?": it could be both. Not all functions have a minimum or it is not easy to locate, and there are different minimization tools that each perform well on different kind of functions.
Without knowing anything more about your function, it is impossible to say why it is not finding a minimum. As a start I would suggest that you plot the function to see if it even has a minimum.

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by