Borrar filtros
Borrar filtros

Simple minimization algorithm in a while loop?

2 visualizaciones (últimos 30 días)
Norris
Norris el 10 de Abr. de 2015
Comentada: Norris el 30 de Abr. de 2015
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 comentarios
Norris
Norris el 10 de Abr. de 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 el 10 de Abr. de 2015
Editada: Adam el 10 de Abr. de 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 ?

Iniciar sesión para comentar.

Respuesta aceptada

Adam
Adam el 10 de Abr. de 2015
Editada: Adam el 10 de Abr. de 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 comentarios
Adam
Adam el 10 de Abr. de 2015
Editada: Adam el 10 de Abr. de 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 el 30 de Abr. de 2015
Sorry for the late reply.
Thanks for your suggestion. My blackbox function is a flow model which requires a number of input parameters.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by