Hi, here is my program
par0 = [0.005276, 0.5017, 88.6357];% initial values
fun_objetivo = @(par,pfrac)FunObjetivo(par);
options = optimset('MaxFunEvals',3000,'Display','iter','PlotFcns',@optimplotfval);
%We use here the Nelder-Mead method
par_optimos = fminsearch(fun_objetivo, par0, options);
I need help with some comands to create a 3d plot, what I want is a 3d plot with "par_optimos(1)" as X, "par_optimos(2)" as Y and "min f(x)" as Z , so I can see how two of the values of my vector "par_optimos" are optimized in each iteration.

 Akzeptierte Antwort

options = optimset('MaxFunEvals',3000,'Display','iter','PlotFcns',@plot_12z);
and
function stop = plot_12z(X,OPTIMVALUES,STATE)
persistent h
stop = false;
switch STATE
case 'init'
if isempty(h) || ~isvalid(h)
fig = figure();
ax = axes(fig);
h = animatedline();
end
case 'done'
if ~isempty(h) && isvalid(h)
delete(ancestor(h), 'figure');
end
case 'iter'
if isvalid(h)
addpoints(h, X(1), X(2), OPTIMVALUES.fval);
end
case 'interupt'
%no new data
end
end

16 Kommentare

I tried it, but for some reason, it appears error:
%Error using matlab.graphics.axis.Axes/set
%The UIContextMenu's parent figure is not the same as the
%parent figure of this object
%Error in callAllOptimPlotFcns (line 144)
%set(handle,'UIContextMenu', cmenu);
%Error in fminsearch>callOutputAndPlotFcns (line 485)
%stop =
%callAllOptimPlotFcns(plotfcns,xOutputfcn,optimValues,state,varargin{:})
%|| stop;
%Error in fminsearch (line 208)
%[xOutputfcn, optimValues, stop] =
%callOutputAndPlotFcns(outputfcn,plotfcns,v(:,1),xOutputfcn,'init',itercount,
%...
%Error in Main_optim (line 7)
%par_optimos = fminsearch(fun_objetivo, par0, options);
Can you help me please . This is something really new for me.
Interesting, it looks like it creates a figure itself. So,
function stop = plot_12z(X,OPTIMVALUES,STATE)
persistent h
stop = false;
switch STATE
case 'init'
if isempty(h) || ~isvalid(h)
%calling function will have made a figure the current figure
%and will have called subplot
ax = gca;
h = animatedline(ax);
end
case 'done'
if ~isempty(h) && isvalid(h)
delete(h);
end
case 'iter'
if isvalid(h)
addpoints(h, X(1), X(2), OPTIMVALUES.fval);
end
case 'interupt'
%no new data
end
end
Jorge Armando Vazquez
Jorge Armando Vazquez am 11 Mai 2021
Bearbeitet: Jorge Armando Vazquez am 11 Mai 2021
the error disappeared, but it keeps showing me a plot in 2d and I need one in 3d.
Try adding
view(3)
after the assignment to h in the 'init' section.
Do you know how to make 3d plot with mesh plot function, with the same axis. I'd appreciate it.
What would be mesh'd ? You have a series of par_optimos(1) and par_optimos(2) values, and corresponding function values, but they would be scattered.
When there are at least 3 points, you could potentially do a triangulation and mesh plot of that, such as is shown at https://www.mathworks.com/help/matlab/ref/delaunaytriangulation.html
what I need is a 3d plot but with a format similar of the meshgrid:
because I want to graph all the values that were between p0 and par_optimos, where "x" will be the values to get to par_optimos (1), "y" for the values of par_optimos (2) and z the error that occurred in each iteration, showing it with the meshgrid format. yeah I know sounds complicated but that's the way I was asked
?? For every minimization step, you are expected to graph for "all" of the values between p0 and par_optimos(1:2) ??
Or are you instead expected to run a minimization step without graphing in order to determine the location of the minima, and then graph for "all" of the values between p0 and the location of the minima ?
Or are you instead just expected to plot over a fixed range of x and y and highlight the minima observed over that range ?
I think the second option sounds more reasonable
For certainty, please indicate whether "second option" is "Or are you instead expected to run a minimization step without graphing" or "just expected to plot over a fixed range of x and y" ? The last of those is the easiest. Especially if the objective function is vectorized.
run a minimization step without graphing in order to determine the location of the minima, and then graph for "all" of the values between p0 and the location of the minima
Okay, in that case do not set a PlotFcn in the options.
Then, after the
par_optimos = fminsearch(fun_objetivo, par0, options);
have something like
N = 100;
xvec = linspace(par0(1), par_optimos(1), N);
yvec = linspace(par0(2), par_optimos(2), N);
p3 = par_optimos(3);
[X, Y] = meshgrid(xvec, yvec);
Z = arrayfun(@(x,y) fun_objectivo([x, y, p3]), X, Y);
surf(X, Y, Z, 'edgecolor', 'none');
xlabel('para_optimos(1)')
ylabel('para_optimos(2)');
zlabel('objective');
Note that this does not optimize at each point: it uses the same third optimization parameter as at the optimum it found.
Look very good, just that this error appears:
Undefined function or variable 'fun_objectivo'.
Error in Main_optim>@(x,y)fun_objectivo([x,y,p3])
Error in Main_optim (line 15)
Z = arrayfun(@(x,y) fun_objectivo([x, y, p3]), X, Y);
Z = arrayfun(@(x,y) fun_objetivo([x, y, p3]), X, Y);
I have been trying to run the program many times, and there appears no error but the program doesn´t stop running so I dont get the final plot, you think maybe if is by the way where for every minimization step, graph for "all" of the values between p0 and par_optimos(1:2) may works.
I do not see your code for FunObjetivo ?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by