Kurz: MATLAB (Matrix Laboratory, MathWorks, 1984) ist eine Umgebung für numerisches Rechnen, Datenanalyse, Simulation und Visualisierung. Grundidee: Alles ist eine Matrix.
Teil des Kurses MATLAB
Was ist MATLAB?
MATLAB (Matrix Laboratory, MathWorks, 1984) ist eine Umgebung für numerisches Rechnen, Datenanalyse, Simulation und Visualisierung. Grundidee: Alles ist eine Matrix. Ein einzelner Wert ist eine 1×1-Matrix, ein Vektor eine Zeile oder Spalte. Dadurch lassen sich mathematische Formeln fast so aufschreiben wie auf Papier.
Verbreitet in Ingenieurwesen, Regelungstechnik, Signalverarbeitung, Bildverarbeitung, Finanzmathematik und Forschung. Die kostenlose Alternative GNU Octave versteht fast denselben Code. Alle Beispiele dieses Kurses laufen in Octave; Abweichungen zu MATLAB nennen wir. (Python mit NumPy und Julia sind die verwandten freien Alternativen.)
Ausführen
octave # interaktive Konsole
octave --no-gui skript.m # Skript ausführen
octave --eval "disp(1+2)"In MATLAB nutzt du den Editor mit Taste F5 oder tippst den Dateinamen ohne .m in das Befehlsfenster. Nützliche Befehle: help sin, doc sin, who (Variablen), clear (löschen), clc (Anzeige leeren), close all.
Das erste Skript
hallo.m
disp('Hallo MATLAB!')
x = 5
y = x ^ 2; % Semikolon unterdrückt die Ausgabe
y
fprintf('x = %d, y = %d\n', x, y);
a = 3; b = 4;
c = sqrt(a^2 + b^2)Ausgabe:
Hallo MATLAB!
x = 5
y = 25
x = 5, y = 25
c = 5- Ohne Semikolon am Zeilenende zeigt MATLAB das Ergebnis an
dispgibt Werte aus,fprintfformatiert wieprintfin C- Kommentare beginnen mit
%(in Octave auch#) - Strings stehen in
'...'oder"..." ansspeichert das Ergebnis des letzten nicht zugewiesenen Ausdrucks
Rechnen
3 + 4 * 2
(3 + 4) * 2
2 ^ 10
mod(17, 5)
rem(-7, 3)
mod(-7, 3)
floor(3.7)
ceil(3.2)
round(2.5)
fix(-3.7)
abs(-5)
sqrt(2)
exp(1)
log(100)
log10(1000)
pi
1 / 3
format long
1 / 3
format short
1e3
5 / 0
-1 / 0
0 / 0
isnan(0 / 0)Ausgabe:
ans = 11
ans = 14
ans = 1024
ans = 2
ans = -1
ans = 2
ans = 3
ans = 4
ans = 3
ans = -3
ans = 5
ans = 1.4142
ans = 2.7183
ans = 4.6052
ans = 3
ans = 3.1416
ans = 0.3333
ans = 0.333333333333333
ans = 1000
ans = Inf
ans = -Inf
ans = NaN
ans = 1Inf, -Inf und NaN entstehen bei Division durch null bzw. ungültigen Rechnungen. Es gibt keine Ganzzahl-Typprobleme: Standard ist double.
Variablen und Typen
zahl = 42;
text = 'Hallo';
wahr = true;
c = 3 + 4i;
class(zahl)
class(text)
class(wahr)
class(int8(5))
class(single(1.5))
isnumeric(zahl)
ischar(text)
whos zahl
abs(c)
real(c)
imag(c)
int8(200)
uint8(-5)
5 / int32(2)Ausgabe:
ans = double
ans = char
ans = logical
ans = int8
ans = single
ans = 1
ans = 1
Variables visible from the current scope:
variables in scope: top scope
Attr Name Size Bytes Class
==== ==== ==== ===== =====
zahl 1x1 8 double
Total is 1 element using 8 bytes
ans = 5
ans = 3
ans = 4
ans = 127
ans = 0
ans = 3whos zeigt Größe und Typ. Ganzzahltypen (int8 … uint64) sättigen am Rand; komplexe Zahlen sind eingebaut (i oder j).
Merke
- MATLAB denkt in Matrizen; Skalare sind 1×1-Matrizen
- Ohne
;wird das Ergebnis angezeigt;dispundfprintfzur Ausgabe %leitet Kommentare ein- Rechnen wie üblich,
mod/rem/floor/roundfür Ganzzahlen - Octave ist die freie, weitgehend kompatible Alternative
Übungsaufgabe
Berechne die Hypotenuse eines Dreiecks mit den Katheten 5 und 12 und gib sie mit fprintf auf zwei Nachkommastellen aus.
Quiz zur Selbstkontrolle
Wofür steht der Name MATLAB?
- Matrix Laboratory (richtig)
- Math Laboratory
- Matrix Language Basic
- Master Lab
Was bewirkt ein Semikolon am Zeilenende?
- Die Ausgabe wird unterdrückt (richtig)
- Die Zeile wird beendet
- Die Zeile wird kommentiert
- Ein Fehler tritt auf
Welches Zeichen beginnt einen Kommentar?
- % (richtig)
- //
- /*
Weiter im Kurs
Weiter: Vektoren und Matrizen
Alle Kapitel: MATLAB im Überblick