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

% Last Modified by GUIDE v2.5 07-Jun-2011 18:43:14

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Lk_OpeningFcn, ...
                   'gui_OutputFcn',  @Lk_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 Lk is made visible.
function Lk_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 Lk (see VARARGIN)
axis(handles.axes1,[-10. 10. -5. 5.]);
% Choose default command line output for Lk
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = Lk_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)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%Anfangswerte/Anfangsbedingunen
t(1)=0;
z1(1)=0;
z2(1)=0;
z3(1)=0;
z4(1)=0;

%Dauer der Simulation und Inkrement berechnung

tend=10;
n=400;
dt=(tend-t(1))/n;



%Berechnung der einzelnen Punkten
 for i=1 : n+1 
     
               %Runge Kuttaverfahren
              k(1,1) = P1(t(i),z1(i),z2(i),z3(i),z4(i));
              k(2,1) = P2(t(i),z1(i),z2(i),z3(i),z4(i));
              k(3,1) = P3(t(i),z1(i),z2(i),z3(i),z4(i));
              k(4,1) = P4(t(i),z1(i),z2(i),z3(i),z4(i));
              
    
              k(1,2) = P1(t(i)+dt/2,z1(i)+dt/2*k(1,1),z2(i)+dt/2*k(2,1),z3(i)+dt/2*k(3,1),z4(i)+dt/2*k(4,1));
              k(2,2) = P2(t(i)+dt/2,z1(i)+dt/2*k(1,1),z2(i)+dt/2*k(2,1),z3(i)+dt/2*k(3,1),z4(i)+dt/2*k(4,1));
              k(3,2) = P3(t(i)+dt/2,z1(i)+dt/2*k(1,1),z2(i)+dt/2*k(2,1),z3(i)+dt/2*k(3,1),z4(i)+dt/2*k(4,1));
              k(4,2) = P4(t(i)+dt/2,z1(i)+dt/2*k(1,1),z2(i)+dt/2*k(2,1),z3(i)+dt/2*k(3,1),z4(i)+dt/2*k(4,1));
    
              k(1,3) = P1(t(i)+dt/2,z1(i)+dt/2*k(1,2),z2(i)+dt/2*k(2,2),z3(i)+dt/2*k(3,2),z4(i)+dt/2*k(4,2));
              k(2,3) = P2(t(i)+dt/2,z1(i)+dt/2*k(1,2),z2(i)+dt/2*k(2,2),z3(i)+dt/2*k(3,2),z4(i)+dt/2*k(4,2));
              k(3,3) = P3(t(i)+dt/2,z1(i)+dt/2*k(1,2),z2(i)+dt/2*k(2,2),z3(i)+dt/2*k(3,2),z4(i)+dt/2*k(4,2));
              k(4,3) = P4(t(i)+dt/2,z1(i)+dt/2*k(1,2),z2(i)+dt/2*k(2,2),z3(i)+dt/2*k(3,2),z4(i)+dt/2*k(4,2));
    
              k(1,4) = P1(t(i)+dt,z1(i)+dt*k(1,3),z2(i)+dt*k(2,3),z3(i)+dt*k(3,3),z4(i)+dt*k(4,3));
              k(2,4) = P2(t(i)+dt,z1(i)+dt*k(1,3),z2(i)+dt*k(2,3),z3(i)+dt*k(3,3),z4(i)+dt*k(4,3));
              k(3,4) = P3(t(i)+dt,z1(i)+dt*k(1,3),z2(i)+dt*k(2,3),z3(i)+dt*k(3,3),z4(i)+dt*k(4,3));
              k(4,4) = P4(t(i)+dt,z1(i)+dt*k(1,3),z2(i)+dt*k(2,3),z3(i)+dt*k(3,3),z4(i)+dt*k(4,3));
              
     
              z1(i+1)= z1(i)+1/6*(k(1,1)+2*k(1,2)+2*k(1,3)+k(1,4))*dt;     %Weg x
              z2(i+1)= z2(i)+1/6*(k(2,1)+2*k(2,2)+2*k(2,3)+k(2,4))*dt;     %Geschwindigkeit v
              z3(i+1)= z3(i)+1/6*(k(3,1)+2*k(3,2)+2*k(3,3)+k(3,4))*dt;     %Winkel phi
              z4(i+1)= z4(i)+1/6*(k(4,1)+2*k(4,2)+2*k(4,3)+k(4,4))*dt;     %Winkelgeschiwindigkeit omega
 
 
              t(i+1)=t(i)+dt;       %Zeit inkrementation
    
 end
 
 
       %Schleife für die Bildausgabe
%__________________________________________________________________________
       for i = 1 : n
           
           %Länge des Stabes
           lS=4;
           
           %Koordinaten bestimmung im Axes
       x = [z1(i),z1(i)+lS*sin(z3(i))] ;    % X Werte der Laufkatze mit Pendel
       y = [0,-lS*cos(z3(i))] ;             % Y Werte der Laufkatze mit Pendel
       h=plot(handles.axes1,x,y,'-ob');      % Darstellung der Linien Farbe
       %Grafik einlesen
       st=imread('Bild.jpg');
       axis(handles.axes1)     %Position im Axes-Objekt
       axes('Position',[z1(i),0,0.3,0.3]);
       image(st);  %Grafik einfügen
       
       set(h,'MarkerFaceColor','blue')
       set(h,'LineWidth',2.5)              % Linien stärke
       axis(handles.axes1,[-10. 10. -5. 5.]);  %Axes dimensionierung
              
       %erste Versuche Grafiken einzubauen
       % Grafik1  einfügen
         %axis(handles.axes1);            % Auswahl des entsprechenden Axes-Objekts
         %G1=imread('Bild.jpg','jpg');   % Einlesen der Grafik
         %image (G1); axis image;           % Grafik ausgeben, Grafik entzerren
         %axis off                          % Koordinatenachsen ausblenden

       
       %Bild Ausgabe
       Bild = getframe;
       end
       
       
       %Differentialgleichungen für die Runge-Kutta berechnung
%==========================================================================
  
     
%P1
 function wert1 = P1(t,z1,z2,z3,z4)
%function
wert1 = z2;
%__________________________________________________________________________

%P2
function wert2 = P2(t,z1,z2,z3,z4)
%function selbst
%Variablen
mk=100; Jl=400; ls=4; F0=2000; ml=500; c=200000; dt=1; a=5; g=9.81;

if (t<dt) 
     Ft = F0; 
else
     Ft = 0;
end

if (z1<a)
     ct = 0; 
else
     ct = c;
end

a12=ml*ls*cos(z3);
a11=mk+ml;
a22=ml*ls^2+Jl;
b1=ml*ls*z4^2*sin(z3)-ct*(z1-a)+Ft;
b2=-ml*g*ls*sin(z3);
D=a11*a22-a12^2;

wert2=(a22*b1-a12*b2)/D;
%__________________________________________________________________________
% P3
  function wert3 = P3(t,z1,z2,z3,z4)
% funktion 
  wert3 = z4;
%__________________________________________________________________________

  % P4
  function wert4 = P4(t,z1,z2,z3,z4)
% funktion 
  mk=100; Jl=400; ls=4; F0=2000; ml=500; c=200000; dt=1; a=5; g=9.81;
   
  if (t<dt) 
     Ft = F0; 
else
     Ft = 0;
  end

if (z1<a)
     ct = 0; 
else
     ct = c;
end

a12=ml*ls*cos(z3);
a11=mk+ml;
a22=ml*ls^2+Jl;
b1=ml*ls*z4^2*sin(z3)-ct*(z1-a)+Ft;
b2=-ml*g*ls*sin(z3);
D=a11*a22-a12^2;

wert4 = (a11*b2-a12*b1)/D;
 %_________________________________________________________________________
 
