필터 지우기
필터 지우기

Simple minimization algorithm in a while loop?

조회 수: 2 (최근 30일)
Norris
Norris 2015년 4월 10일
댓글: Norris 2015년 4월 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에서 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