While loop and optimization

조회 수: 9 (최근 30일)
Chetan Fadnis
Chetan Fadnis 2022년 8월 26일
댓글: Chetan Fadnis 2022년 8월 26일
I am trying to optimize certain values using golden search, inside while loop. To initialize I assign a value to a variable (say t=0.1) outside while loop. I calculate the another parameter using golden ratio method inside the while loop, check for the the condition if my objective is less than tolerance the loop breaks, else I have to update the value of (t) by say 0.05. The maximum value for t is 0.35. If I write it like, t=t+0.05; it will go into infinite looping case. How to stop this while loop, once t attains its maximum value?
t=0.1; %for this t calculate initial y1, y2, which are further updated using golden search
while 1
%code for golden search, giving values of y2 and y1.
if (abs(y2-y1)<=tolerance)
break
end
t=t+0.05;
end
I want t must not exceed 0.35 in any case.
  댓글 수: 2
Chunru
Chunru 2022년 8월 26일
Then you need "while t<0.35" instead of "while 1"
Chetan Fadnis
Chetan Fadnis 2022년 8월 26일
thank you.

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

채택된 답변

Karim
Karim 2022년 8월 26일
You can change the condition for the while loop to reflect this, see below for an example
t = 0.1;
t_max = 0.35;
while t <= t_max
%code for golden search, giving values of y2 and y1.
if abs(y2-y1) <= tolerance
break
end
t = t + 0.05;
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by