function varargout = form(varargin)
% FORM M-file for form.fig
%      FORM, by itself, creates a new FORM or raises the existing
%      singleton*.
%
%      H = FORM returns the handle to a new FORM or the handle to
%      the existing singleton*.
%
%      FORM('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in FORM.M with the given input arguments.
%
%      FORM('Property','Value',...) creates a new FORM or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before form_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to form_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help form

% Last Modified by GUIDE v2.5 19-Feb-2008 09:49:31

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @form_OpeningFcn, ...
                   'gui_OutputFcn',  @form_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before form is made visible.
function form_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to form (see VARARGIN)

% Choose default command line output for form
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes form wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = form_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in nd2D.
function nd2D_Callback(hObject, eventdata, handles)
% hObject    handle to nd2D (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global x1;
global x2;
global f;

%addpath([cd 'C:\Documents and Settings\sam37\moje\Matlab\Skomam\NewtonNDzakl']);
addpath([cd '\NewtonNDzakl']);

plot(0,0);

x1 = -3:0.1:3;
x2 = -3:0.1:3;
for i=1:length(x1)
    for j=1:length(x2)
        f(j,i) = feval('funkce',[x1(i) x2(j)]);
    end
end

mesh(x1,x2,f);
text(3,5,['y =  3*(1-x(1))^2*exp(-(x(1)^2) - (x(2)+1)^2) \newline - 10*(x(1)/5 - x(1)^3 - x(2)^5)*exp(-x(1)^2-x(2)^2) \newline - 1/3*exp(-(x(1)+1)^2 - x(2)^2);'],'BackgroundColor',[1 1 .6]);
% x1 = 0.23,  x2 = -1.63, fmin = -6.5511


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

plot(0,0);
drawnow;


% --- Executes on button press in nd2D.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to nd2D (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

        



% --- Executes on button press in min1D.
function min1D_Callback(hObject, eventdata, handles)
% hObject    handle to min1D (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Newton('sin','cos','sin',pi,1e-3)

hold off
% --- Executes on button press in nd1D.
function nd1D_Callback(hObject, eventdata, handles)
% hObject    handle to nd1D (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

plot(0,0);
axis([0 2*pi -1 1]);
text(2*pi/2,0.5,['{\bf Newtonova metoda 1D} \newline  \newline {\bf while} abs(xn-x) >= \epsilon  \newline  x=xn; fx=fxn;\newline   xn = x - f ''/f '''';\newline   fxn=sin(xn);\newline    it = it + 1;\newline {\bf end}'],'BackgroundColor',[1 1 .6]);

hold on
x = 0:2*pi/100:2*pi;
y = sin(x);
plot(x,y);
plot(pi,sin(pi),'+m');
text(pi+0.05,sin(pi),'x0'); 
grid off





% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)

% ***************  Kontury *************** 

% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global x1;
global x2;
global f;


[c,h] = contour(x1,x2,f,25); 
clabel(c);  %clabel(c,h);
text(0.2,-2,[' x1 = 0.23,  x2 = -1.63, fmin = -6.5511'],'BackgroundColor',[1 1 .7]);
xlabel('x1');
ylabel('x2');




% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% ***************  Nacti data *************** 
% hObject    handle to pushbutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global x1;
global x2;
global f;
global x0;

x0 = [-1,0];

hold on    
contour(x1,x2,f,25); 
    
    plot(x0(1),x0(2),'+m');
    text(x0(1),x0(2),'x0'); 
    axis([-3 3 -3 3]);

    text(1,1.5,['{\bf Newtonova metoda 2D} \newline \newline {\bf while} abs(grad)>\epsilon  \newline   it=it+1; \newline  h=-Hess\\grad;  \newline  xold = x; \newline  x=x+h; \newline  grad=feval(df,x);  \newline  Hess=feval(ddf,x);  \newline  {\bf end;}'],'BackgroundColor',[1 1 .6]);




% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% ***************  Najdi minimum *************** 
% hObject    handle to pushbutton9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global x1;
global x2;
global f;
global x0;

[y,it] = newtonNDzakl('dfunkce','ddfunkce',x0',1e-3)
hold off


% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
% ***************  amotor *************** 

% hObject    handle to pushbutton10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
addpath([cd '\amotor']);
plot(0,0);
start;



% --- Executes on button press in pushbutton11.
function pushbutton11_Callback(hObject, eventdata, handles)

% ***** MCRS minimum *******************

% hObject    handle to pushbutton11 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

addpath([cd '\mcrs2D']);
hold on
[iterr,xmin,fmin] = mcrs2D(2, [-3;-3], [3;3] ,'random', 'validate', 'fitness', 100000, 1e-3);
text(-2.5,-2.5,['MCRS minimum: Iter: ' num2str(iterr) ' ,xmin= ' num2str(xmin) ' ,fmin= ' num2str(fmin)],'BackgroundColor',[1 0 0]);
hold off




% --- Executes on button press in pushbutton12.
function pushbutton12_Callback(hObject, eventdata, handles)

% ***** SCGA minimum *******************

% hObject    handle to pushbutton12 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

addpath([cd '\Hybrid2D']);
hold on
[xmin, fmin,fcount] = SCGA(2,'fitness',[3 3],[-3 -3]);
text(-2.5,-2.9,['SCGA minimum: Iter: ' num2str(fcount) ' ,xmin= ' num2str(xmin(1)) ' ,' num2str(xmin(2)) ' ,fmin= ' num2str(fmin)],'BackgroundColor',[0 1 0]);
axis([-3 3 -3 3]);
hold off


% --- Executes on button press in pushbutton13.
function pushbutton13_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton13 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton14.
function pushbutton14_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton14 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton15.
function pushbutton15_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton15 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton16.
function pushbutton16_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton16 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


