How to plot an optimization in logarithmic scale?
4 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
採用された回答
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
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 Center および File 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!