Simple minimization algorithm in a while loop?

1 次查看(过去 30 天)
Hello,
I have a program (call it blackbox) and I give this program a value (zo) and it does a calculation and checks if the result (diff) is within a certain tolerance (tol).
What I would like to do is to have it minimize in as few iterations as possible. It would be ideal to have the steps increase/decrease dynamically. Anyone have suggestions?
Here is what I was thinking.
%input value to start
zo = 15;
% +/- tolerance
tol = 0.1;
% start while loop
diff = 1;
count = 0;
while abs(diff) > tol
% black box algorithm
diff = zo/2 - 2
% check if value is within the tolerance
% is there a better way to do this in less iterations?
if diff > tol
zo = zo - 0.1;
elseif diff < -tol
zo = zo + 0.1;
end
count = count +1
end
  2 个评论
Norris
Norris 2015-4-10
Edit: I realized the code was very poorly written and didnt really get at what I wanted to do. I hope its easier to understand now.
I want to be able to minimize the error with the least amount of iterations possible.
Adam
Adam 2015-4-10
编辑:Adam 2015-4-10
I assume those hard-coded 0.1 values should be ' tol ' instead? Or do you really want to always add/subtract -0.1 irrespective of if you change the value of tol ?

请先登录,再进行评论。

采纳的回答

Adam
Adam 2015-4-10
编辑:Adam 2015-4-10
zo = 5;
tol = 0.1;
diff = 100; % Doesn't matter so long as it is outside of tolerance
count = 0;
while abs( diff ) > tol
diff = blackbox( z0 );
z0 = z0 - sign( diff-tol ) * tol;
count = count + 1;
end
looks like it does what you want unless I am missing something. That is only based on a quick assessment of your code.
Obviously you can add in printing stuff out to command line if you wish.
  3 个评论
Adam
Adam 2015-4-10
编辑:Adam 2015-4-10
It's impossible really to advise on how best to minimise a blackbox function. I have no idea whether the function is well behaved or not. Changing z0 by even 0.1 could have a huge impact on results depending on the function.
Good optimisation algorithms are usually tailored towards the problem with some kind of knowledge of the domain.
e.g. if the output from your blackbox function is a nice parabola based on the input then it is trivial to minimise to within a tolerance. If the output of function is, to all intents and purposes, arbitrary relative to the input then that obviously isn't the case.
Norris
Norris 2015-4-30
Sorry for the late reply.
Thanks for your suggestion. My blackbox function is a flow model which requires a number of input parameters.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by