Simple minimization algorithm in a while loop?

11 visualizzazioni (ultimi 30 giorni)
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 Commenti
Norris
Norris il 10 Apr 2015
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 il 10 Apr 2015
Modificato: Adam il 10 Apr 2015
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 ?

Accedi per commentare.

Risposta accettata

Adam
Adam il 10 Apr 2015
Modificato: Adam il 10 Apr 2015
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 Commenti
Adam
Adam il 10 Apr 2015
Modificato: Adam il 10 Apr 2015
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 il 30 Apr 2015
Sorry for the late reply.
Thanks for your suggestion. My blackbox function is a flow model which requires a number of input parameters.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by