Matlab Einführung

30
Matlab Einführung Tobias Wunner 16. Oktober 2006

description

Matlab Einführung. Tobias Wunner 16. Oktober 2006. Matlab eine Einführung. Vorteile Interpreter und interaktive Befehlseingabe Schnelles Implementieren von wissenschaftlichen Methoden Gutes Hilfesystem. >> lookfor 'sum' TRACE Sum of diagonal elements. CUMSUM Cumulative sum of elements. - PowerPoint PPT Presentation

Transcript of Matlab Einführung

Page 1: Matlab Einführung

Matlab Einführung

Tobias Wunner

16. Oktober 2006

Page 2: Matlab Einführung

Matlab eine Einführung

• Vorteile– Interpreter und interaktive Befehlseingabe

• Schnelles Implementieren von wissenschaftlichen Methoden

• Gutes Hilfesystem

>> lookfor 'sum'TRACE Sum of diagonal elements.CUMSUM Cumulative sum of elements.SUM Sum of elements....

Page 3: Matlab Einführung

Matlab eine Einführung

• Vorteile– Interpreter und interaktive Befehlseingabe

• Schnelles Arbeiten zum Entwickeln von Programmen

• Gutes Hilfesystem

>> help sumSUM Sum of elements.S = SUM(X) is the sum of the elements of the vector X. If X is a matrix, S is a row vector with the sum over each...

Page 4: Matlab Einführung

Matlab eine Einführung

• Vorteile– Modulorientiert / Offen

• Sämtliche Matlabskripte sind einsehbar

>> edit sum

Page 5: Matlab Einführung

Matlab eine Einführung

• Vorteile– Erweiterbar

• C oder FORTRAN Code durch mex Bibliotheken

Page 6: Matlab Einführung

Matlab eine Einführung• Vorteile

– Profiler ab Version 7!• Hilft beim Auffinden von

Ressourcen-Verbrauchenden Programmteile

Page 7: Matlab Einführung

Matlab eine Einführung• Vorteile

– Umfangreiche Softwarepakete vorhanden• z.B.Toolboxes

– Image Processing Toolbox– Neural Network Toolbox

• www.mathworks.com/matlabcentral– Hier findet ihr alles was ihr nicht findet… :)

Page 8: Matlab Einführung

Matlab eine Einführung

• Nachteile– Nicht Objektorientiert (kein JAVA)– Effiziente Programme nur durch

Vektorisierung

>> x=1:5X = 1 2 3 4 5>> y=sqrt(1:5);

for x=1:5 y = sqrt(x);end

statt

„Umdenken“

Page 9: Matlab Einführung

Matlab eine Einführung

• Nachteile– Fast Alles ist erlaubt!

• Keine Variablendeklaration nötig• Variablenneubelegungen

(Typänderung)

• Funktionen mit gleichen Ein- und Ausgabeparametern

>> x=2>> x=‘super‘

function x = myfun(x) ... x = x*2;

Page 10: Matlab Einführung

Matlab eine Einführung• Nachteile

– Fast Alles ist erlaubt!• Sogar Build-In Matlab Funktionen können als

Variablen genutzt werden

=> ACHTUNG tötet eingebaute Funktionen!!!>> sum = 1:5;>> sum([0 0 1 1 0]) 3 4 % intuitiv erwartet 1+2+3+4+5=15>> who % zeigt alle variablen im speicher

an ans sum>> tmp = sum % sichern von variable sum>> clear sum % löschen von variablesum>> sum(tmp) % richtig! 15

Page 11: Matlab Einführung

Matlab eine Einführung

• MATLAB starten– UNIX

• z.B. Remote via Putty

– Windows

andromeda@ option matlabandromeda@ matlab –nojvm -nodisplay

Page 12: Matlab Einführung

Matlab eine Einführung

• Handwerkzeug zum starten– Arbeitsverzeichnis

>> pwd>> ls>> cd projekt1

– Variablen im Speicher>> who

– Variablen löschen/speichern >> clear y >> save dateiname >> load dateiname– History

Cursor

Page 13: Matlab Einführung

Matlab eine Einführung

• Matrizen– Beliebige Matrizen

>> [1 3 5 7;2 4 6 8]

– Spezielle Matrizen>> eye(3)>> ones(2,4)>> zeros(1,3)

– Zufallszahlen >> rand(3) >> rand(100,100)

Page 14: Matlab Einführung

Matlab eine Einführung

• Matrizen indizieren– Dimension

>> x=rand(3,4) % M-by-N Matrix>> size(x,1) % M = Zeilen>> size(x,2) % N = Spalten

– Alle Elemente als Liste>> x(:)

– k-tes bis Letztes Element

>> y(k:end)

Page 15: Matlab Einführung

Matlab eine Einführung

• Matrizen indizieren– Mit Logik

>> x=2:7 2 3 4 5 6 7>> x>4 0 0 0 1 1 1

– Indizes ausgeben>> find(x>3) 5 6 7

Page 16: Matlab Einführung

Matlab eine Einführung

• Vergleichsoperatoren– kleiner/größer <,>– gleich/ungleich ==,~=– größergleich >=

• Logische Operatoren– Und &– Oder |– Nicht ~

Page 17: Matlab Einführung

Matlab eine Einführung

• Matrizen sortieren/umformen– sortieren

>> x=6:-1:1 6 5 4 3 2 1>> sort(x) 1 2 3 4 5 6

– umformen>> reshape(x,2,3) 1 3 5 2 4 6

Page 18: Matlab Einführung

Matlab eine Einführung

• Matrix Algebra– Kronecker Produkt

>> kron([1 1 1],[1 2 5]) 1 1 1 2 2 2 5 5 5

– Weiter nützliche Matrixoperationen >> help matfun

Page 19: Matlab Einführung

Matlab eine Einführung

• Komponentenweise Operationen– Inneres Produkt

>> x=[1 -1]‘ (x ist hier>> x‘*x Spaltenvektor!) 2

– Äußeres Produkt

>> x*x‘ 1 -1 -1 1

Page 20: Matlab Einführung

Matlab eine Einführung

• Vektormanipulationen– Eintragsweise Operationen

>> [1 2 3].*[1 10 100] 1 20 300>> [10 20 30]./[5 20 60] 2.0000 1.0000 0.5000

>> [2 4 8].^2 4 16 64

Page 21: Matlab Einführung

Matlab eine Einführung

• Programmierung– Schleifen

for i=[1 4 2 1] sprinft(´Round%d\n´,i);end

while BEDINGUNG ...end

Page 22: Matlab Einführung

Matlab eine Einführung

• Programmierung– Abbruch von Schleifen

for i=1:10 ... if (error<.1) break; % bricht Schleife ab endend

Page 23: Matlab Einführung

Matlab eine Einführung

• Programmierung– Bedingungen

if BEDINGUNG ...end

if BEDINGUNG ...else ...end

Page 24: Matlab Einführung

Matlab eine Einführung

• Datentypen– Arrays

matrix = [‘Peter‘,‘Hans‘] PeterHansmatrix = [‘Peter‘;‘Hans‘] geht nicht!

– Cells cell = {‘Peter‘,‘Hans‘} m = char(cell) Peter Hans

Lösung

>> m(2,:) Hans>> cell{2} Hans

Page 25: Matlab Einführung

Matlab eine Einführung

• Datentypen– Cells -> beliebiege Datentypen cell2 = {12,cell,m} cell2{3}

Peter Hans

– Ähnliche Idee bei Struct struct1.a = cell2; struct1.b = rand(10,10); struct1.a{2}{3}(2,:) % Zugriff auf Hans

Page 26: Matlab Einführung

Matlab eine Einführung

• Ein-/Ausgabe

disp(‘Hello World‘); % Einfache Ausgabesprinft(‘Zahl=%d\n‘,x); % Formatierte

Ausgabe

x = input(‘Zahl eingeben:‘,x); % Einfache EingabeS = ‘1 2 3‘;x = sscanf(S,‘%f‘); % Formatierte Eingabe

Page 27: Matlab Einführung

Matlab eine Einführung

• Grafische Ausgabe von Daten– neues Ausgabefenster öffnen

figure

– Daten in einem 2D Bild ausgebenx=rand(10,10);x2=x*x‘;figure, imagesc(x2), colorbar

sc = scaled image Skala

Kovarianzmatrix

Page 28: Matlab Einführung

Matlab eine Einführung

• Grafische Ausgabe von Daten– Funktionen Plotten

plot(XVALUES,YVALUES)xval = 0:.1:30yval = sin(3.*cos(xval))plot(xval,yval,‘r‘)

– Mehrere Funktionen yval2 = sin(xval) plot(xval,yval,xval,yval2)oder plot(xval,yval,‘:b‘) hold on plot(xval,yval2,‘r‘)

Page 29: Matlab Einführung

Matlab eine Einführung

• Grafische Ausgabe von Daten– 3D Plot von Oberflächen (surfaces)

x = imread(...);surf(x);shading flat;

Page 30: Matlab Einführung

Matlab eine Einführung