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

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = lr_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)

global A;           %Globale Variable muss gesetzt werden um gui und 
                    %m files miteinander zu verknüpfen
%Berechnung des LR-Algorithmus mittels Shift-Verfahren
%Voraussetzung ist eine nxn Matrix, welche symetrisch ist
%Als Abbruchbedingung wird die Überschreitung des Fehlers angenommen.
%Dieser muss kleiner 1 sein. In unserem Fall haben wir den Fehler bei 1/4
%gewählt
n=size(A,1);        %Gibt Zeilenanzahl an
t=1;                %Initialisierungsschritt von t
l=zeros(n,n);       %Erzeugung einer leeren nxn Matrix
err=1;              %Initialisierungsschritt des Fehlers
while ( t==1 )||(err >= 1/5000)
    for j=1:1:n-1
        for i=j+1:1:n
            l(i,j)=A(i,j)/A(j,j);
            for k=j+1:1:n
                A(i,k)=A(i,k)-l(i,j)*A(j,k);
            end
        end
    end
    %Erzeugung der L Matrix, durch eine Einheitsmatrix. Einsetzen der
    %Elemente der Matrix l in die obere Dreicksmatrix
    L=eye(n);
    for j=1:1:n-1
        for i=j:1:n-1
            L(i+1,j)=l(i+1,j);
        end
    end
    %Besetzung der Matrix R mit den Werten aus dem neuen A und Austausch
    %der unteren Dreieckselemente mit 0
    R=A;
    for j=1:1:n-1
        for i=j:1:n-1
            R(i+1,j)=0;
        end
    end
    disp('-----------------')
    fprintf('Matrix Schritt: %d\n', t)
    disp('-----------------')
    A=R*L
    Aalt=L*R;
    t=t+1;
    %Neuberechnung des absoluten Fehlers der n,n-ten Elemente
    err=abs(1-(Aalt(n,n)/A(n,n)));
end

aquer=Aalt-Aalt(n,n)*eye(n);

for k=1:1:n-1
    for i=k+1:1:n
        aquer(i,k)=aquer(i,k)/aquer(k,k);
        for j=k+1:1:n
            aquer(i,j)=aquer(i,j)-aquer(i,k)*aquer(k,j);
        end
    end
end
lneu=eye(n);
rneu=aquer;

for j=1:1:n-1
    for i=j:1:n-1
        lneu(i+1,j)=aquer(i+1,j);
    end
end

for j=1:1:n-1
    for i=j:1:n-1
        rneu(i+1,j)=0;
    end
end
%Berechnung der neuen Matrix nach Shift-Verfahren
aneu=(rneu*lneu)+Aalt(n,n)*eye(n);

%Ausgabe der Eigenwerte mittels einer Tabelle, sowie die Anzahl der
%Schritte. Am Ende wird noch einmal die neue Matrix ausgegeben, in der die
%Eigenwerte auch in den Diagonalelementen abzulesen sind.
disp('----------')
disp('Eigenwerte:')
disp('----------')
for i=1:n
    fprintf('%03d\n',aneu(i,i))
end
disp('--------------------')
disp('Anzahl der Schritte ')
disp('--------------------')
disp(t-1)
disp('------------')
disp('Neue Matrix ')
disp('------------')
disp(aneu)

% --- 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)
disp('Daran arbeite ich gerade')


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