- Matlab Starten - Fenster erklären:

13.04.2015
Matlab-Einfuehrung
1
- Matlab Starten
- Fenster erklären:
- Command Window
- Workspace
- Current Folder
- Datentypen und Variablen:
- Typen: Zahlen (meist komplexe double)
>> 1
>> 1/2
>> 0.1
>> 1i
>> 1i^2
- Text (Strings): Einfache Anführungszeichen
>> 'Hallo Welt'
- Booleans
>> true
>> false
- Vektoren
>> x = [ 1; 2; 3 ];
>> x2 = [ 1; 2; 1 ];
>> x3 = 1:10
>> x4 = 1:2:10
>> x5 = linspace(0, 2*pi)
>> x6 = linspace(0, 2*pi, 11)
- Matrizen: eye, zeros, ones, feste Matrizen
>> A = [ 1, 2, 3;
4, 5, 6;
7, 8, 9 ]
>> B = eye(3)
>> C = ones(4, 3)
>> D = zeros(3, 4)
>> E = rand(3, 3)
>> F = diag([ 1, 2, 3 ])
>> G = []
- Auf Workspace den zeigen
- Rechenoperationen:
- Summe
- Differenz
- Produkt
>> A*x
>> A*A
>> A*B
>> A*C(?)
>> A*D, C*A
- Division (Achtung: was passiert bei Matrizen?)
- Lösen von LGS
>> y = (A+B)\x, (A+B)*y
>> y = A\x, A*y
/home/sniper/svn/uebung/Numerik1/SS15/ueb/Matlab-Einfuehrung
13.04.2015
-
-
-
-
-
Matlab-Einfuehrung
2
>> z = A\x2, A*z
- Potenz (A^2, C^2(?))
- Punktweise Operationen
- Transponieren
>> A.'
>> A*C.'
- Elementweise Funktionen (sin, cos, exp, sqrt, log, ...)
- abs, max, min, round
- norm
Logische Operationen:
- Vergleiche
>> 1 == 0
>> 1 ~= 0
>> 1 > 5
>> 1 <= 5
>> x > 1
>> x2 > 1
>> all(x > 1)
>> all(x > 0)
>> any(x > 1)
>> any(x > 3)
- Achtung:
>> all(A > 0)
>> all(all(A > 0))
Indizieren
>> A(1, 1)
>> A(:, 1)
>> A(1. :)
>> A([ 1, 2 ], :)
>> A(1:2, 1:2)
>> A(1, 1) = 17
>> A(1, :) = [ 10, 11, 12 ]
>> x(x > 1)
>> A(A > 3)
>> x(x > 1) = 0
Größeninformationen, Zusammenfassende Operationen
>> size(A), size(x)
>> length(x)
>> length(A), length(C), length(D)
>> isempty(A)
>> isempty(F)
Anzeigebefehle
- A, B
- disp(A);
- disp('Hallo Welt');
- fprintf('Keine Leerzeile, aber die Zahl %d', 5);
Plotten
>> plot(x6, sin(x6))
/home/sniper/svn/uebung/Numerik1/SS15/ueb/Matlab-Einfuehrung
13.04.2015
Matlab-Einfuehrung
3
>> plot(x5, sin(x5))
- Zweiter Plot in das gleiche Bild
>> hold on;
>> plot(x5, cos(x5), 'r--');
- Plot Titel
>> title('Trigonometrische Funktionen');
- Achsenbeschriftungen
>> xlabel('x');
>> ylabel('y');
- Legenden
>> legend('sin', 'cos');
- Fehler erzeugen
>> error('Du bist zu dumm.');
- Immer Fehlerausgaben von Matlab beachten
>> A(4, 1)
- Matlab Skripte
- Sammlung von Matlab Befehlen, die hintereinander ausgeführt
werden
- Auf "neue Datei" im oben klicken
- Editor einbetten
- "disp('Ich bin skript.m')" als Inhalt
- Datei speichern (z.B. skript.m)
- Auf ausfüren klicken
>> skript
- (ohne .m!)
- Kontrollstrukturen
- if-Abfrage
-> if A(1) > 1
disp('Groß!');
elseif A(1) == 1
disp('Eins.')
else
disp('Klein')
end
>> skript
>> A(1) = 0; skript
>> A(1) = 2; skript
- for-Schleife
-> for kk = 1:10
disp(kk);
end
>> skript
-> for kk = x
>> skript
- while-Schleife
-> x = 10;
while x > 0
x = x - 1
/home/sniper/svn/uebung/Numerik1/SS15/ueb/Matlab-Einfuehrung
13.04.2015
Matlab-Einfuehrung
4
end
- Kein do ... while
- Matlab Funktionen
- Eigener Workspace!
- Ein- und Ausgabeargumente
- Neue Datei
-> function [ x, y ] = funktion(a, b)
disp(a)
end
- Speichern unter ...
>> funktion()
>> funktion(5)
>> funktion(5, 10)
-> disp(b)
>> funktion(5)
>> funktion(5, 10)
>> [ x, y ] = funktion(5, 10)
-> x = a + b;
y = a - b;
>> [ x, y ] = funktion(5, 10)
>> [ a, b ] = funktion(5, 10)
>> [ a, b ] = funktion(x, y)
- Haltepunk in Zeile 1 setzen
>> [ a, b ] = funktion(x, y)
- Zeigt anderen Workspace!
- Programm Schritt für Schritt durchlaufen lassen
/home/sniper/svn/uebung/Numerik1/SS15/ueb/Matlab-Einfuehrung