function mydem(action);
%Function minimization demonstration.
%Steepest Descent Algorithm
%Spring 00 Neural Nets Class Demo
%S. Mandayam, ECE Dept., Rowan University
%
%Adapted from
%BANDEM Banana function minimization demonstration.
%   Adapted for Expo by Ned Gulley, 6-21-93
%   Copyright (c) 1990-98 by The MathWorks, Inc.
%   $Revision: 5.8 $  $Date: 1997/11/29 01:22:57 $
%          
if nargin<1,
    action='initialize';
end;

if strcmp(action,'initialize'),
    figNumber=figure( ...
        'Name','Steepest Descent Demo', ...
        'NumberTitle','off', ...
        'Visible','off');
    axes( ...
        'Units','normalized', ...
        'Visible','off', ...
        'Position',[0.05 0.32 0.70 0.75]);

    %===================================
    % Set up the Comment Window
    top=0.27;
    left=0.05;
    right=0.75;
    bottom=0.05;
    labelHt=0.05;
    spacing=0.005;
    promptStr=str2mat(' ',...
               ' Press the buttons at the right to see examples of', ...
               ' Optimization Toolbox commands.');
    % First, the MiniCommand Window frame
    frmBorder=0.02;
    frmPos=[left-frmBorder bottom-frmBorder ...
        (right-left)+2*frmBorder (top-bottom)+2*frmBorder];
    uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'Position',frmPos, ...
        'BackgroundColor',[0.5 0.5 0.5]);
    % Then the text label
    labelPos=[left top-labelHt (right-left) labelHt];
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'Position',labelPos, ...
        'BackgroundColor',[0.5 0.5 0.5], ...
        'ForegroundColor',[1 1 1], ...
        'String','Comment Window');
    % Then the editable text field
    txtPos=[left bottom (right-left) top-bottom-labelHt-spacing];
    txtHndl=uicontrol( ...
        'Style','edit', ...
        'HorizontalAlignment','left', ...
        'Units','normalized', ...
        'Max',10, ...
        'BackgroundColor',[1 1 1], ...
        'Position',txtPos, ...
        'Callback','bandem(''eval'')', ...
        'String',promptStr);
    % Save this handle for future use
    set(gcf,'UserData',txtHndl);

    %====================================
    % Information for all buttons
    labelColor=[0.8 0.8 0.8];
    top=0.95;
    left=0.80;
    btnWid=0.15;
    btnHt=0.08;
    % Spacing between the button and the next command's label
    spacing=0.02;

    %====================================
    % The CONSOLE frame
    frmBorder=0.02;
    yPos=0.05-frmBorder;
    frmPos=[left-frmBorder yPos btnWid+2*frmBorder 0.9+2*frmBorder];
    uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'Position',frmPos, ...
        'BackgroundColor',[0.5 0.5 0.5]);
    
    %====================================
    %====================================
    % The STEEPEST DESCENT button
    btnNumber=3;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Steepest';
    callbackStr='mydem(''demobutton'')';
    
    % Generic popup button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
        'UserData',btnNumber);

    %====================================
      % The INFO button
    uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'Position',[left bottom+btnHt+spacing btnWid btnHt], ...
        'String','Info', ...
        'Callback','mydem(''info'')');

    %====================================
    % The CLOSE button
    uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'Position',[left bottom btnWid btnHt], ...
        'String','Close', ...
        'Callback','close(gcf)');

    % Now uncover the figure
    set(figNumber,'Visible','on');

elseif strcmp(action,'demobutton'),
    method=get(gco,'UserData');
    txtHndl=get(gcf,'UserData');

    % Plotting the function
    cla reset;
    axis off;
    x=0:0.2:12;
    y=besselj(5,(20-x));
    % draw the plot
    surfHndl=plot(x,y);
    axis on;
    hold on;
    drawnow
    
    % Uncomment the next line for random initial conditions
    x=rand*5;
    %x=2;
    plot(x,myfun(x),'ko', ...
        'MarkerSize',15, ...
        'LineWidth',2, ...
        'EraseMode','none');

    text(x,myfun(x),'   Begin', ...
        'Color',[0 0 0], ...
        'EraseMode','none');


    OPTIONS=0;
    % if you want cubic interpolation line search, set OPTIONS(7)=1
    % otherwise mixed polynomial line search is used.
    % 
    %OPTIONS(7)=1;
    % Turn off all messages, even if no convergence.
    OPTIONS(1)=-1;
    
   if method==3,
        str= ...
        [' Steepest Descent                '  
         ' (Unconstrained minimization)    '];
        set(txtHndl,'String',str);
        OPTIONS(6)=2;
        OPTIONS(10)=200;
        OPTIONS(14)=300;
        f='[besselj(5,(20-x));onestep(x)]';
        str2=' [x, options] = fminu(f,x,OPTIONS);';
        [x, options] = fminu(f,x,OPTIONS);
 
     
end;
drawnow
            plot(x,myfun(x),'ko', ...
        'MarkerSize',15, ...
        'LineWidth',2, ...
        'EraseMode','none');
    text(x,myfun(x),'   End', ...
        'Color',[0 0 0], ...
        'EraseMode','none');

    funEvals=sprintf(' Number of function evaluations: %g', options(10));
    str=get(txtHndl,'String');
    str=str2mat(str,' ',str2,' ',funEvals);
    set(txtHndl,'String',str);

elseif strcmp(action,'info'),
    helpwin(mfilename);
end;    % if strcmp(action, ...
