help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: fminsearch plot tools


From: Pantxo
Subject: Re: fminsearch plot tools
Date: Thu, 20 Dec 2018 03:30:34 -0600 (CST)

gciriani wrote
> Thanks Kai for showing me the code. I tried it and it does plot some.
> However, it's only the individual points, as if each iteration was a
> separate series, so every point is disconnected, and one doesn't see the
> chronology of fminsearch.
> 
> I tried a different way on my own, by using global variables within the
> function to be minimized. That way after running the minimization I can
> manipulate the data in different way to visualize what's going on. I think
> I
> read somewhere that using global variables in a function is deprecated,
> but
> at least I got some clue about how to resolve my problem. 
> 
> 
> 
> -----
> Giovanni Ciriani - Windows 10, Octave 4.2.1, configured for
> x86_64-w64-mingw32
> --
> Sent from:
> http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html

Global variables are not deprecated, they are simply not recommended since
they often lead to hardly debugable programs.
You may want to use "persistent" variables which is the equivalent of
"static" variables in C. For example

## draw_fcn.m
function  ret = draw_fcn (x, optimval = [], state = [], doplot = false)
  persistent xx = [];
  if (doplot)
    plot (xx(1,:), xx(2,:))
  else
    xx = [xx x];
  endif
  ret = false;
endfunction

## Your program
fcn = @(x) (x(1)-5).^2 + (x(2)-8).^4;
x0 = [0;0];
[xmin, fval] = fminsearch (fcn, x0, optimset ("OutputFcn", @draw_fcn));

## Now plot the data
draw_fcn ([], [], [], 1);

## Clear persistent data
clear draw_fcn

Pantxo



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html



reply via email to

[Prev in Thread] Current Thread [Next in Thread]