function varargout = eigenstuff_GUI(varargin)
% EIGENSTUFF_GUI M-file for eigenstuff_GUI.fig
%      EIGENSTUFF_GUI, by itself, creates a new EIGENSTUFF_GUI or raises the existing
%      singleton*.
%
%      H = EIGENSTUFF_GUI returns the handle to a new EIGENSTUFF_GUI or the handle to
%      the existing singleton*.
%
%      EIGENSTUFF_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in EIGENSTUFF_GUI.M with the given input arguments.
%
%      EIGENSTUFF_GUI('Property','Value',...) creates a new EIGENSTUFF_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before eigenstuff_GUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to eigenstuff_GUI_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 eigenstuff_GUI

% Last Modified by GUIDE v2.5 28-Aug-2012 09:50:59

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @eigenstuff_GUI_OpeningFcn, ...
                   'gui_OutputFcn',  @eigenstuff_GUI_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 eigenstuff_GUI is made visible.
function eigenstuff_GUI_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 eigenstuff_GUI (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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

% Presets
global A
global tol1
global tol2
global n_iter
global check_pow
global check_qr
global l
global d

A = sprandsym(10,0.3);
set(handles.edit1,'String','sprandsym(10,0.3)');
d = eig(A);
set(handles.listbox1,'String',num2str(d));
tol1 = 10^(-3); %Tol for power
tol2 = 10^(-3); %Tol for inverse
set(handles.tol_pow,'String',tol1);
set(handles.tol_inv,'String',tol2);
n_iter = 20; %No iter for QR
set(handles.n_it_qr,'String',n_iter);
check_pow = 1; % Normal power
check_qr = 1; % Normal QR
l = 1;
set(handles.l_inv,'String',l);
ploteigen(n_iter)


% Create Legend

ax=handles.axes3;
set(ax,'Visible','off');
set(ax,'NextPlot','add');
axis(ax,'off');

he=plot(ax,0,0,'r:','LineWidth',2.5);
hp=plot(ax,0,0,'b -d');
hi=plot(ax,0,0,'k -s');
hqr=plot(ax,0,0,'-o','color',[34, 139, 34]/256,'linewidth',1.2);
%cellfun(@(h)(set(h,'Visible','off')),{he,hp,hi,hqr});
titles={'Exact eigenvalues','Power method','Inverse iteration',...
    'QR iteration'};
leg=legend(ax,titles);
set(leg,'Box','off');
set(leg,'Color','none');


% --- Outputs from this function are returned to the command line.
function varargout = eigenstuff_GUI_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;



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

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double
global A
global d
global n_iter
try

    str = get(hObject,'String');
    A = str2num(str);
    assert(  ndims(A) == 2 && size(A,1)==size(A,2) && size(A,1) > 1 && ...
        size(A,1) < 25 && ~any(isnan(A(:))) && ...
        all(isreal(A(:))) && ~any(isinf(A(:))),...
    'Invalid matrix specified. Must be square with 1 < size < 25');
    d = eig(A);
    assert(all(isreal(d)),'It is required that A have real eigenvalues.');
    set(handles.listbox1,'String',num2str(d));
    axes(handles.axes1)
    cla
    ploteigen(n_iter)
    set(handles.start_button,'Enable','on');
catch err
    set(handles.start_button,'Enable','off');
    waitfor(msgbox(err.message,'Error','error','modal'));    
end

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox1


% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


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

% Hint: get(hObject,'Value') returns toggle state of radiobutton4


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

% Hint: get(hObject,'Value') returns toggle state of radiobutton5



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

% Hints: get(hObject,'String') returns contents of tol_inv as text
%        str2double(get(hObject,'String')) returns contents of tol_inv as a double
global tol2
try
    tol2 = str2double(get(hObject,'String'));
    assert(~isnan(tol2) && isreal(tol2) && ~isinf(tol2) ...
        && tol2 > 0 && tol2>eps,...
    'Invalid tolerance for inverse iteration! (Is it too small?)');
    set(handles.start_button,'Enable','on');
catch err
    set(handles.start_button,'Enable','off');
    waitfor(msgbox(err.message,'Error','error','modal'));    
end
    


% --- Executes during object creation, after setting all properties.
function tol_inv_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tol_inv (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


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

% Hint: get(hObject,'Value') returns toggle state of radiobutton2


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

% Hint: get(hObject,'Value') returns toggle state of radiobutton3


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

% Hints: get(hObject,'String') returns contents of tol_pow as text
%        str2double(get(hObject,'String')) returns contents of tol_pow as a double
global tol1
try
    tol1 = str2double(get(hObject,'String'));
    assert(~isnan(tol1) && isreal(tol1) && ~isinf(tol1) ...
    && tol1>eps,...
        'Invalid tolerance for power iteration! (Is it too small?)');
    set(handles.start_button,'Enable','on');
catch err
    set(handles.start_button,'Enable','off');
    waitfor(msgbox(err.message,'Error','error','modal'));    
end



% --- Executes during object creation, after setting all properties.
function tol_pow_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tol_pow (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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

% Hints: get(hObject,'String') returns contents of l_inv as text
%        str2double(get(hObject,'String')) returns contents of l_inv as a double
global l
try
    str = get(hObject,'String');
    l = str2double(str);
    assert(~isnan(l) && isreal(l) && ~isinf(l),...
        'Invalid shift for inverse iteration!');
    set(handles.start_button,'Enable','on');
catch err
    set(handles.start_button,'Enable','off');
    waitfor(msgbox(err.message,'Error','error','modal'));    
end
    

% --- Executes during object creation, after setting all properties.
function l_inv_CreateFcn(hObject, eventdata, handles)
% hObject    handle to l_inv (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


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

% Hint: get(hObject,'Value') returns toggle state of radiobutton8
global check_qr
check_qr = 1; % Normal QR method

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

% Hint: get(hObject,'Value') returns toggle state of radiobutton9
global check_qr
check_qr = 2; % QR method with shifts

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

% Hints: get(hObject,'String') returns contents of n_it_qr as text
%        str2double(get(hObject,'String')) returns contents of n_it_qr as a double

global n_iter
try
    n_iter = str2double(get(hObject,'String'));
    assert(~isnan(n_iter) && isreal(n_iter) && ~isinf(n_iter) ...
        && ceil(n_iter)==n_iter && n_iter>0 && n_iter <= 1000,...
        'Invalid number of iterations for QR! (Max 1000)');
    set(handles.start_button,'Enable','on');
catch err
    set(handles.start_button,'Enable','off');
    waitfor(msgbox(err.message,'Error','error','modal'));    
end
    



% --- Executes during object creation, after setting all properties.
function n_it_qr_CreateFcn(hObject, eventdata, handles)
% hObject    handle to n_it_qr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end





%%%%%%%% Methods %%%%%%%%%%%%%%%%%%

function[lambda, eigenv] = power_meth(A,tol)
% Single-staged power method
% We are guaranteed that this terminates if A has evalues of different
% magnitudes.
global d
mineig = min(d);
[n,~] = size(A);

% Presets
accept = 0;
v = ones(n,1);
v = v/norm(v);

b = [];
k = 1;
while accept == 0
    u = A*v;
    f = @(x) (norm(u - x*v));
%     lambda = fminsearch(@(x) f(x),0); %Or alternatively can use raileigh quotient
    lambda = (v'*u)/(v'*v); %Raileigh quotient
    b(k) = lambda;
    k = k+1;
    % See if we can accept lambda as the eigenvalue
    if f(lambda) <= tol;
        accept = 1;
        eigenv = u;
    end
    
    v = u/norm(u);
end
plot([1:k-1],b,'b -d');
plot([k-1,k-1],[lambda,mineig],'b:')
ploteigen(k-1)

function[lambda,eigenv] = power_meth_shifts(A,tol)
% Power method with shifts
global s_pow
global d
mineig = min(d);

[n,~] = size(A);

% Presets
accept = 0;
v = ones(n,1);
v = v/norm(v);
w = v; % This is x_tilde_k in Arieh's notes.

b = [];
k = 1;
while accept == 0
    s = s_pow; % shift, e.g A(1,1) if want smallest evalue, A(n,n) for largest evalue.
    u = (A - s*eye(n))*v;
    
    f = @(x) (norm(u - x*v));
    lambda = (v'*u)/(v'*v); %Raileigh quotient
 %   lambda = fminsearch(@(x) f(x),0);
    b(k) = lambda + s;
    k = k+1;
    % See if we can accept lambda as the eigenvalue
    if f(lambda) <= tol;
        accept = 1;
        lambda = lambda + s;
        eigenv = w;
    end
    w = A*v; % This is x_tilde_k in Arieh's notes.
    v = u/norm(u);
end
plot([1:k-1],b,'b -d');
plot([k-1,k-1],[lambda,mineig],'b:')
ploteigen(k-1)

function[lambda, eigenv] = inverse_meth(A,tol,l)
% Inverse iteration method
% l is an estimate of an eigenvalue of A
global d
mineig = min(d);

[n,~] = size(A);

% Presets
accept = 0;
x_k = ones(n,1);
b = [];
k = 1;

while accept == 0
    x_kplus1 = (A - l*eye(n))\x_k;
    
    f = @(z) (norm(x_k - z*x_kplus1));

    nu = fminsearch(@(z) f(z),0);
    b(k) = l + nu;
    k = k+1;
    % See if we can accept lambda as the eigenvalue
    if f(nu) <= tol*norm(x_kplus1);
        accept = 1;
        lambda = l + nu;
        eigenv = x_kplus1/norm(x_kplus1);
    end
    
    x_kplus1 = x_kplus1/norm(x_kplus1);
    x_k = x_kplus1;
end
plot([1:k-1],b,'k -s');
plot([k-1,k-1],[lambda,mineig],'k:')
ploteigen(k-1)

function[lambda] = QR_alg(A, n_iter)
%n_iter is the number of iterations.
global d
mineig = min(d);

b = [];
for i = 1:n_iter;
    [Q,R] = qr(A);
    A = R*Q;
    lambda = A(1,1);
    b(i) = lambda;
end 
plot([1:n_iter],b,'-o','color',[34, 139, 34]/256,'linewidth',1.2);
plot([n_iter,n_iter],[lambda,mineig],':','color',[34, 139, 34]/256)
ploteigen(n_iter)

function[lambda] = QR_shifts(A,n_iter)
global s_qr
global d
mineig = min(d);

% QR method with shifts
[n,~] = size(A);
s = s_qr; %Shift
b = [];
for i = 1:n_iter;
    [Q,R] = qr(A - s*eye(n));
    A = R*Q + s*eye(n);
    s = A(n,n);
    b(i) = s;
    lambda = s;
end
plot([1:n_iter],b,'-o','color',[34, 139, 34]/256,'linewidth',1.2);
plot([n_iter,n_iter],[lambda,mineig],':','color',[34, 139, 34]/256)
ploteigen(n_iter)


% --- Executes during object creation, after setting all properties.
function panel_pow_CreateFcn(hObject, eventdata, handles)
% hObject    handle to panel_pow (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function panel_inv_CreateFcn(hObject, eventdata, handles)
% hObject    handle to panel_inv (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function panel_QR_CreateFcn(hObject, eventdata, handles)
% hObject    handle to panel_QR (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes on button press in start_button.
function start_button_Callback(hObject, eventdata, handles)
% hObject    handle to start_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global A
global tol1
global tol2
global n_iter
global l
global check_pow
global check_qr

cla
hold on
axes(handles.axes1)
if check_pow == 1
    [~, ~] = power_meth(A,tol1);
elseif check_pow == 2
    [~, ~] = power_meth_shifts(A,tol1);
end

[~,~] = inverse_meth(A,tol2,l);

if check_qr == 1
    [~] = QR_alg(A,n_iter);
elseif check_qr == 2
    [~] = QR_shifts(A,n_iter);
end
axis image
axis square
 


% --- Executes when selected object is changed in panel_pow.
function panel_pow_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in panel_pow 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)
global check_pow

h = get(eventdata.NewValue,'string');
switch h
    case 'Normal'
        check_pow = 1;
    case 'With shifts'
        check_pow = 2;
end
sh_pow_Callback(handles.sh_pow,eventdata,handles);


% --- Executes when selected object is changed in panel_QR.
function panel_QR_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in panel_QR 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)
global check_qr

h = get(eventdata.NewValue,'string');
switch h
    case 'Normal'
        check_qr = 1;
    case 'With shifts'
        check_qr = 2;
end
sh_pow_Callback(handles.sh_qr,eventdata,handles);



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

% Hints: get(hObject,'String') returns contents of sh_qr as text
%        str2double(get(hObject,'String')) returns contents of sh_qr as a double
    global s_qr
try
    str = get(hObject,'string');
    s_qr = str2double(str);
    assert(~isnan(s_qr) && isreal(s_qr) && ~isinf(s_qr),...
        'Invalid shift for QR iteration!');
    set(handles.start_button,'Enable','on');
catch err
    set(handles.start_button,'Enable','off');
    waitfor(msgbox(err.message,'Error','error','modal'));    
end

% --- Executes during object creation, after setting all properties.
function sh_qr_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sh_qr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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

% Hints: get(hObject,'String') returns contents of sh_pow as text
%        str2double(get(hObject,'String')) returns contents of sh_pow as a
%        double
global s_pow
try
    str = get(hObject,'string');
    s_pow = str2double(str);
    disp(s_pow);
    assert(~isnan(s_pow) && isreal(s_pow) && ~isinf(s_pow),...
        'Invalid shift for power iteration!');
    set(handles.start_button,'Enable','on');
catch err
    set(handles.start_button,'Enable','off');
    waitfor(msgbox(err.message,'Error','error','modal'));    
end

% --- Executes during object creation, after setting all properties.
function sh_pow_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sh_pow (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function axes3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate axes3

function[] = ploteigen(k)
global d
hold on
x_eig = [1:k];
for r = 1:length(d);
    y_eig = d(r)*ones(1,length(x_eig));
    disp(d(r));
    plot( x_eig,y_eig,'r:','linewidth',2.5);
end
set(gca,'Yminortick','on','xminortick','on'); 
xlabel('Iterations');
ylabel('Eigenvalues');
axis([1 k min(d)-0.1 max(d)+0.1]);
%axis image
axis square


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