%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Datei:        bsp_grundlagen.m
%
% Beschreibung: Vorlesungsbeispiele zur FHA-Vorlesung 
%               MATLAB Grundlagen
%
% Datum:        18.03.07
% Autor:        Ulrich Wohlfarth
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

echo on;
format compact;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Starten von MATLAB

(40^3 + 3*2e3) / 7

variable_1 = 25; variable_2 = 10;
variable_1

a = variable_1 + variable_2, A = variable_1 / variable_2

1 / 0           % inf: Unendlich
0 / 0           % NaN: Nicht definiert

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Vektoren und Matrizen

clear

mein_vektor = [1 2 3]
meine_matrix = [mein_vektor; 5 6 7]

lang = 1:8
tief = 10:-2:0

meine_matrix(2, 3)
meine_matrix(2, :)
meine_matrix(end)
meine_matrix(end,:)
M = meine_matrix ; 
M (:,end+1) = [ 10 ; 11 ]

noch_laenger = linspace(1, 19, 10)
hoch_hinaus = logspace(1, 2, 5)
zwo_drei = ones(2, 3)

zufall = rand(3,2)

abs(i)
abs(1+j)

prod(mein_vektor)
prod(meine_matrix)
prod(prod(meine_matrix))

A = [1 2 ; 3 4]
B = [1/1 1/2 ; 1/3 1/4]
A*B
A.*B
size(A)
length(A)
A'

rank(A)
det(A)
inv(A)
eig(A)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Strukturen

meine_struktur = struct ('daten', meine_matrix, 'groesse', [2 3]);
meine_struktur(2).daten = meine_matrix.^(-1);
meine_struktur(2).daten

komponist = struct('name','Johann Sebastian Bach')
datum.Tag = 21 ;
datum.Monat = 'März' ;
datum.Jahr = 1685 ;
komponist.geboren = datum ;

komponist(2).name = 'Wolfgang Amadeus Mozart' ;
komponist(2).geboren.Tag = 27 ;
komponist(2).geboren.Monat = 'Januar' ;
komponist(2).geboren.Jahr = 1756 ;
komponist(2)
komponist(2).geboren

komponist

% Cell Arrays
zelle   = cell(2,3)

zelle{1,1} = 'Zelle (1,1) ist ein Text' ;
zelle{1,2} = 10 ;
zelle{1,3} = [1 2 ; 3 4] ;
zelle{2,1} = komponist(1) ;
zelle{2,2} = komponist(2) ;
zelle{2,3} = date ;

zelle
zelle{2,3}
zelle{2,1:2}            
zelle{2,2}.geboren.Monat


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Verwalten von Variablen

length(meine_matrix)

size(meine_matrix)

clear lang tief noch_laenger hoch_hinaus mein_vektor  ...
    datum zufall ans
whos

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Vergleichsoperationen und logische Operatoren

test = 1;
test ~= 1
(4>5-2 & 4<=5) | ~test

a = [ 0 0 1 1 ]' ;
b = [ 0 1 0 1 ]' ;

logtab = [ a b a&b a|b xor(a,b) ~a ~(a&b) ~(a|b)]

%% Logical Indexing

a = -2:7
a (a < 0) = 0
a = a (logical (mod (a, 3)))

% Alternative: ~ wandelt in logical um, [] löscht Element
% a (mod (a, 3)) = []


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Verzweigungen und Schleifen

test = 5;
if test<=2; a=2, elseif test<=5; a=5, else a=10, end;

switch test case 2; a=2, case {3 4 5}; a=5, otherwise a=10, end;

for k=1:5, k^2; end;
n = 1;
while 1, n=n+1; m=n^2, if m>10, break; end; end;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB-Scripts und -Funktionen

test = [ 2 4 3 ] ;
[A,G] = mittelwerte(test)

test = [ 2 4 3 ] ;
[M1,M2] = mittelwerte2(test,'g')

global schalter
schalter = 'g';
test = [ 2 4 3 ] ;
[M1,M2] = mittelwerte3(test)

f1 = inline('x.^2+x-1', 'x')        % Inline Object Funktion
test = [ 2 4 3 ] ;
f1(test)

fh = @det ;                         % function_handle auf @det erzeugen
fh_info = functions(fh)             % Info zu function_handle fh abfragen

a=1 ; b=2 ; c=3 ; d=4 ;
feval (fh,[a b ; c d])              % Ausführen des function_handles fh
%syms a b c d                        % Definition in der Math Symbolic Toolbox
%feval (fh,[a b ; c d])              % Ausführen des function_handles fh

