Matlab SS2022 Teil 01
Matlab SS2022 Teil 01
Dieses Skript unterliegt dem deutschen Urheberrecht. Es darf nur zum persönlichen Gebrauch zu
Zwecken des Studiums verwendet werden. Insbesondere ist es nicht erlaubt, das Skript oder Teile
daraus zu bearbeiten, zu übersetzen, zu kopieren oder in elektronischer Form zu speichern und an
andere Personen weiterzugeben, weder in Kopie, noch auf elektronischem Wege per Email, auf
Speichermedien (z. B. CD, USB-Stick usw.), über Datenbanken oder andere Medien und Systeme.
Lediglich die Herstellung von Kopien und Downloads für den persönlichen, privaten und nicht
kommerziellen Gebrauch ist erlaubt.
This script is subject to German copyright law. It is restricted to personal use for the purpose of
studying. In particular it is not allowed to edit the script or parts of it, to translate, copy or save it in
electronic form and to pass it on to other people, neither as a copy nor electronically via email, data
storage media (e.g., CD, USB flash drive, etc.), databases or other media and systems. Only the
production of copies and downloads for personal, private and noncommercial use is permitted.
Angermann, A.; Beuschel, M.; Rau, M.; Wohlfarth, U.: Matlab – Simulink – Stateflow: Grundlagen,
Toolboxen, Beispiele, De Gruyter Studium, 2020 (E-Book)
Quarteroni, M.; Saleri, F.: Wissenschaftliches Rechnen mit MATLAB, Springer Verlag, Heidelberg,
2006 (E-Book)
Pietruszka, W. D.: MATLAB und Simulink in der Ingenieurpraxis, Springer, 2021 (E-Book)
Schweizer, W.: MATLAB kompakt, DE Gruyter/Oldenburg, 2022 (E-Book)
.
.
Starten:
Beenden:
Workspace
Current Directory Editor Browser
Browser Anzeige der
Textverarbeitungsprogramm aktuellen Variablen
und „Debugger“
Anzeige des aktuellen
Verzeichnisses
Hilfe
Aktuelles Verzeichnis
Beispiel:
>> help exit
exit Exit from MATLAB.
exit terminates MATLAB after running finish.m, if finish.m exists.
It is the same as QUIT and takes the same termination options.
For more information, see the help for QUIT.
See also quit.
Documentation for exit
>> help demo
>> help elfun Liste der elementaren math. Funktionen
>> help ops Liste aller Operationen
>> version Zeigt die Vesionsnummer an
>> ver Liste der installierten Toolboxen auf dem PC mit den jeweiligen Versionen
>> whatsnew MATLAB Release Notes (whatsnew will be removed in a future release) >> lookfor exit
>> 12*3
ans = ans wird überschrieben
36
>> alpha=[Link]
alpha =
0 45 90 135 180
>> sin(alpha).^2+cos(alpha).^2
ans =
1.0000 1.0000 1.0000 1.0000 1.0000
u.s.w.
try, break, case, else, for, end, if, function, return, switch, spmd, global, catch, otherwise, parfor,
persistent, while
Um dieses Problem für die imaginäre Einheit zu vermeiden, sollte für diese
grundsätzlich 1i oder 1j anstelle von i bzw. j geschrieben werden!
Beispiele:
>> i, j, pi >> 1/0 >> 0/0 >> eps
ans =
0 + 1.0000i ans = ans = ans =
ans =
0 + 1.0000i Inf NaN 2.2204e-16
ans =
3.1416
>> sin(pi)
ans =
1.2246e-16
Das Fließkommaformat
Beispiele:
>> who >> whos
Name Size Bytes Class Attributes
Your variables are:
alpha 1x5 40 double
alpha ans x y z ans 1x1 8 double
x 1x1 8 double
>> clc y 1x1 8 double
>> clear x z 1x1 8 double
>> who
>> type help
>> type sign Befehle können nicht rückgängig
>> edit sign gemacht werden!
>> edit gradient
Beispiele:
>> a= [4 9 2] >> b=[4;9;7] >> B= [2 4 6
b= 8 10 12
a= 4 14 16 18]
9 B=
4 9 2 7 2 4 6
8 10 12
14 16 18
>> A= [1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
Die Elemente einer Matrix können auch als Ergebnis
beliebiger mathematischer Operation sein
C=
v= w=
1 3 5 7 9 10 9 8 7 6 5 4 3
u= a=
Beispiele:
a= b =.......
4) Spezial-Matrizen
Einheitsmatrix Eins-Matrix
Syntax Syntax
A = eye(n) A = ones(n)
A = eye(m,n) A = ones(m,n)
>> eye(4) >> ones(6)
ans = ans =
1 0 0 0 1 1 1 1 1 1
0 1 0 0 1 1 1 1 1 1
0 0 1 0 1 1 1 1 1 1
0 0 0 1 1 1 1 1 1 1
1 1 1 1 1 1
>> eye(2,3) 1 1 1 1 1 1
ans =
1 0 0
0 1 0
4) Spezial-Matrizen
Null-Matrix Diagonal-Matrix magisches Quadrat
Syntax Syntax Syntax
B = zeros(n) B = diag(m:n) B = magic(n), n > 2
B = zeros(m,n)
Syntax Syntax
B = rand(m,n) B = randn (m,n)
6) Block-Matrizen
Syntax Erzeugt eine Blockmatrix mit
replicate
B = repmat(A,m,n) (nxm) aus A Blöcken
Erzeugt aus den Vektoren x, y der Länge n und m die Matrizen X und Y mit je nxm Elementen. Jede
Zeile von X besteht aus x und jede Spalte von Y besteht aus y.
>> [X,Y]=meshgrid(1:4,-1:3)
X= Y=
1 2 3 4 -1 -1 -1 -1
1 2 3 4 Die Matrizen X und Y werden für
0 0 0 0
1 2 3 4 das Plotten von Funktionen mit zwei
1 1 1 1
1 2 3 4 Variablen und für dreidimensionale
2 2 2 2
1 2 3 4 Graphiken verwendet.
3 3 3 3
19. April 2022 | Sinapius | Al Natsheh
Simulation adaptronischer Systeme mit MATLAB/SIMULINK | Seite 37
Grundlagen Einfache Fehler
>> I=eye(3)+i*ones(3)
I=
1.0000 + 1.0000i 0 + 1.0000i 0 + 1.0000i
0 + 1.0000i 1.0000 + 1.0000i 0 + 1.0000i
0 + 1.0000i 0 + 1.0000i 1.0000 + 1.0000i
>> I’
ans =
1.0000 - 1.0000i 0 - 1.0000i 0 - 1.0000i Konjugierte
0 - 1.0000i 1.0000 - 1.0000i 0 - 1.0000i Transposition
0 - 1.0000i 0 - 1.0000i 1.0000 - 1.0000i
>> I.’
ans =
1.0000 + 1.0000i 0 + 1.0000i 0 + 1.0000i Transposition
0 + 1.0000i 1.0000 + 1.0000i 0 + 1.0000i
0 + 1.0000i 0 + 1.0000i 1.0000 + 1.0000i
>> I'*I >> I.'*I
19. April 2022 | Sinapius | Al Natsheh
Simulation adaptronischer Systeme mit MATLAB/SIMULINK | Seite 41
Vektoren und Matrizen Indizierung/Zugriff auf Element
y
>> M=[5 2 4 3 6 1];
>> XYZ=[3 4 0
460
650
2kg 6kg 720
4kg 960
5kg 10 3 0];
1kg >> rs=M*XYZ/sum(M)
3kg rs =
6.2857 4.6190 0
x
σ 𝑟റ𝑖 𝑚𝑖
𝑟റ𝑠 =
σ 𝑚𝑖
Skalarprodukt: 𝑎റ ∙ 𝑏 = 𝑎റ 𝑏 cos 𝜑
>> b*a
>> norm(a) >> abs(a) ans =
ans = ans = 4 -8 12
3.7417 1 2 3 5 -10 15
6 -12 18
19. April 2022 | Sinapius | Al Natsheh
Simulation adaptronischer Systeme mit MATLAB/SIMULINK | Seite 48
Vektoren und Matrizen Skalarprodukt
Weitere Operationen:
>> a.*a >> a*a' >> a.^2
ans = ans = ans =
1 4 9 14 1 4 9
>> a*a
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix
matches the number of rows in ……. .
Kreuzprodukt: 𝑎റ × 𝑏 = 𝑎റ 𝑏 sin 𝜑
>> cross(a,b) >> cross(b,a)
ans = ans =
-27 6 13 27 -6 -13
𝑎റ × 𝑏
>> phi=asind(norm(cross(a,b))/(norm(a)*norm(b))) 𝜑 = sin−1
phi = 𝑎റ 𝑏
68.5624
>> A.*B
ans =
5 12 1 2 5 6 1∙5 2∙6 5 12
∙∗ = =
21 32 3 4 7 8 3∙7 4∙8 21 32
Kumulative Summe
Funktion Beschreibung
max(x) Maximun
min(x) Minimum
sum(x) Summe
diff(x) Differenz
prod(x) Produkt
mean(x) Arithmetischer Mittelwert
Median(x) Median
std(x) Standardabweichung
sort(x) Sortiert
cumsum(x) Kumulative Summe
cumprod(x) Kumulatives Produkt
19. April 2022 | Sinapius | Al Natsheh
Simulation adaptronischer Systeme mit MATLAB/SIMULINK | Seite 57
Vektoren und Matrizen Weitere Funktionen (4)
B=
1 4 0 9
Umordnen von Matrizen
2 5 9 10
3 2 12 8
C=
1 2 3 4 5 6 7 8 9 10 11 12
2 2 0 0 0 0 0 0 0 0 0 0
3 0 3 0 0 0 0 0 0 0 0 0
4 0 0 4 0 0 0 0 0 0 0 0
5 0 0 0 5 0 0 0 0 0 0 0
6 0 0 0 0 6 0 0 0 0 0 0
7 0 0 0 0 0 7 0 0 0 0 0
8 0 0 0 0 0 0 8 0 0 0 0
9 0 0 0 0 0 0 0 9 0 0 0
10 0 0 0 0 0 0 0 0 10 0 0
11 0 0 0 0 0 0 0 0 0 11 0
12 0 0 0 0 0 0 0 0 0 0 12
A\b verwendet aus numerischer Sicht bessere Verfahren. Dies betrifft zum einen die
Geschwindigkeit, vor allem aber die Genauigkeit der Lösung.
x = A\b liefert auch dann eine Lösung, wenn das Gleichungssystem nicht eindeutig lösbar ist. Im
Fall eines überbestimmten Systems wird eine Lösung angegeben, bei der der quadratische Fehler
von Ax−b minimal ist.
I II III IV
x1 x2 x3
𝑥1 = 1,1
𝑥2 = 1,9
𝑥3 = 3,2
𝑥1 + 𝑥2 = 3,1
𝑥1 + 𝑥2 + 𝑥3 = 6,0
𝑥2 + 𝑥3 = 4,9