How to plot an optimization in logarithmic scale?

조회 수: 4 (최근 30일)
Ba Ba Black Sheep!
Ba Ba Black Sheep! 2017년 1월 3일
댓글: Obinna 2025년 9월 23일
The following code is expected to plot the output of Rosenbrock's function against number of iterations ( for the sake of the problem, don't be concerned with the quality of the source code).
function value = banana(x0)
a = x0(1);
b = x0(2);
x = x0(3);
y = x0(4);
value = (1 - x + a)^2 + 100 * (y - b * (x - a)^2)^2;
end
a = int8(4 * rand()) / 2;
b = int8(4 * rand()) / 2;
[x1, y1] = random(a, b);
[x2, y2] = random(a, b);
[x3, y3] = random(a, b);
[x4, y4] = random(a, b);
x = [x1; x2; x3; x4];
y = [y1; y2; y3; y4];
save values.mat;
for i = 1
x0 = [a, b, x(i), y(i)];
options = optimset('PlotFcns', { @optimplotfval });
[solution_point, fval,exitflag,output] = fminsearch(@banana,double(x0),options);
old_x=[x(i),y(i)];
new_x_1 = linspace(-2,2,51);
new_x_2 = linspace(-2,2,51);
for j=1:51
if(new_x_1(j)>= old_x(1))
new_x_1(j)=old_x(1);
break;
end
end
for j=1:51
if(new_x_2(j)>= old_x(2))
new_x_2(j)=old_x(2);
break;
end
end
end
My questions are,
(1) Is this plot really showing the output of Rosenbrock's function against number of iterations?
(2) The plot shows a sudden slump in the output of the function. I need to show this plot in logarithmic form, so that a smooth curve is plotted. How can I do that?

채택된 답변

Walter Roberson
Walter Roberson 2017년 1월 3일
For the log plot:
edit optimplotfval
now Save As into your own directory as optimplotlogfval.m . Close the original optimplotfval.m (so you do not accidentally modify it.) Then in the optimplotlogfval.m version, alter the "function" statement to use optimplotlogfval instead of optimplotfval as the name. Then go into the plotscalar function there and for the iteration == 0 case, after the ylabel() call, add
set(gca, 'YScale', 'log');
Make the same change for iteration == 0 in the plotvector function.
Saving all of that, go back to your code and change
options = optimset('PlotFcns', { @optimplotfval });
to
options = optimset('PlotFcns', { @optimplotlogfval });
  댓글 수: 1
Obinna
Obinna 2025년 9월 23일
Would you still recommend to do this now? plotscalar seems to have changed from that to "thePlot", so I don't know if these intructions work for 2025 MATLAB. Would it be possible to give another instructional on how to do it currently? Thank you

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by