1% Literatur: Einfuehrung in MATLAB (ELT3 - FH Hagenberg)
2
3% MATrix LABoratory
4
5% Hilfsfunktionen
6 help % Ueberblick Funktionsgruppen
7 help general % allgemeine Befehle
8 help / % Operatoren und Sonderzeichen
9 help <function> % Funktionsbeschreibung
10 type <function> % Funktionsquellcode
11 lookfor <Stichwort> % Stichwortsuche im Hilfetext
12
13
14% Zahlendarstellung
15 % alle Zahlen im Format: double precision
16 % (1 Bit Vorzeichen, 11 Bit Exponent, 52 Bit Mantisse)
17 a = 123
18 b = -100.13
19 b = -1.0013e2; % ; verhindert Ausgabe der Operation
20
21 x = a + j*b % komplexe Zahl (j <=> i)
22
23 Inf % Unendlich
24 NaN % Not a Number
25 pi
26 realmax % groesste Zahl
27 realmin % kleinste Zahl
28
29
30% Operationen
31 + - * ^ / % arithmetische Operationen
32 .* .^./ % elementweise Operationen
33
34 x = A \ b % loest lineare Gleichungssysteme
35 A*x = b
36
37 == ~= < > <= >= % Vergleichsoperationen
38 isequal() % Vergleich von Matrizen
39 && || xor any all % logische Operatoren
40 & | ~ % logische bitweise Operatoren
41
42 [1 2]' = [1; 2] = % Transponierte Matrix
43 1
44 2
45
46 sin asin % Bogenmass
47 sind asind % Grad
48 -> cos tan cot sec csc
49
50 exp % Exponentialfunktion e^
51 log log10 log2 % Logarithmus (natuerl., 10er, dual)
52 sqrt
53 abs angle complex conj imag real % Funktionen fuer komplexe Zahlen
54 fix floor ceil round % Runden
55 mod rem % Modulus
56 sign % Vorzeichen
57
58
59% Variablen
60 who whos % Anzeige definierter Variablen
61 clear % alle Variablen loeschen
62 clear <variable>
63
64 A = [1 2 3] = [1,2,3] % Zeilenvektor
65 1 2 3
66 B = [1;2;3;4;5] % Spaltenvektor
67 1
68 2
69 3
70 C = [1 2 3;4 5 6] % Matrize
71 1 2 3
72 4 5 6
73
74 A = 1:3:15 % Nummer generieren
75 1 4 7 10 13
76 B = 1:4
77 1 2 3 4
78
79 A = linspace(6,8,5) % Intervall unterteilen
80 6.00 6.50 7.00 7.50 8.00
81 B = logspace(0,2,5) % Intervall logarithmisch
82 1.00 3.16 10.00 31.62 100.00 % unterteilen 10^0, 10^2
83
84 A = zeros(2,4) % Matrizen generieren
85 0 0 0 0
86 0 0 0 0
87 B = ones(2,3)
88 1 1 1
89 1 1 1
90 C = eye(3)
91 1 0 0
92 0 1 0
93 0 0 1
94 D = rand(2,4)
95
96 A=[1 1 2 2; % Submatrizen
97 3 3 4 4;
98 5 5 6 6;
99 7 7 8 8];
100 B=A(2:3,2:4)
101 B =
102 3 4 4
103 5 6 6
104
105 A=[1 2 3 4 5 6 7 8 9;
106 9 8 7 6 5 4 3 2 1];
107 A(:,2:2:end)
108 A =
109 2 4 6 8
110 8 6 4 2
111 A(:,5:end)=[] % Elemente loeschen
112 A =
113 1 2 3 4
114 9 8 7 6
115 A(1:3,2:3)=ones(3,2) % mit 1en fuellen
116 A =
117 1 1 1 4
118 9 1 1 6
119 0 1 1 0
120
121 A = zeros(2,2,0); % Array dynamisch erweitern
122 A(:,:,1) = zeros(2,2); % -> Bedingung: Elemente mit
123 A(:,:,2) = ones(2,2); % gleicher Groesse
124
125
126% Vektorfunktionen
127 length % Laenge eines Vektors
128 max % Element mit groessten Index
129 min % Element mit kleinsten Index
130 sum % Summe der Elemente
131 prod % Produkte der Elemente
132 any % liefert 0, wenn alle Elemente 0
133 all % liefert 1, wenn kein Element 0
134 mean % arithm. Mittelwert des Vektors
135 sort % sortiert Vektor aufsteigend
136
137 A' = transpose(A) % transponierte Matrix
138 A.' = ctranspose(A) % konjugiert komplex transponiert
139 fliplr(A) % vertikale Spiegelung
140 flipud(A) % horizontale Spiegelung
141 rot90(A) % Drehung
142 repmat(A,z,s) % zxs Blockmatrix aus A
143 reshape(A,z,s) % Matrixdimensionen aendern
144 diag(A,k) % k-te Nebendiagonale
145
146 eig % Eigenwert, Eigenvektor
147 inv % inverse Matrix
148 expm, sqrtm % Matrix-Exponential, -Wurzel
149 poly % charakteristische Polynom
150 det % Determinante
151 size % Matrixdimension
152 norm % Norm einer Matrix oder eines Vektors
153
154
155% Zeichenketten
156 a = 'Z'
157 char, double % Umwandlung
158 strcat str2mat strcmp upper lower % Funktionen
159 ischar isletter isspace % logische Funktionen
160
161 A = 'string'; % String erweitern
162 A = [A 'string2'];
163
164
165% Dateiverwaltung
166 pwd what cd type delete dir ls rmdir mkdir copyfile movefile fileattrib
167 save <Dateiname> <Variablenliste> % Variablen speichern
168 load <Dateiname> % Variablen laden
169 -ascii % im Textformat gespeichert
170
171 fid = fopen(filename, modus)
172 = -1 % falls Datei nicht existiert
173 'r' % read
174 'w' % write
175 'a' % append
176 [A, count] = fread(fid, size, precision)
177 % size - angegebene Anzahl an Daten
178 % precision - Binaerformat ('int16', 'int32', 'float32', 'float64')
179 count = fwrite(fid, A, precision)
180 position = ftell(fid) % Position einer Datei ermitteln
181 status = fseek(fid, offset, origin) % Position anpassen
182 % (origin: 'bof', 'eof')
183 count = fprintf(fid, format, A, ...)% formatiertes Schreiben
184 %c % einzelnes Zeichen
185 %d % Dezimaldarstellung
186 %u % Dezimaldarstellung (unsigned)
187 %e % Exponentialdarstellung
188 %f % Fliesskommadarstellung
189 %s % Zeichenkette
190 %x % Hexadezimaldarstellung
191 \n \b \r \t \\
192 [A, count] = fscanf(fid, format, size) % formatiertes Lesen
193 fclose(fid)
194
195
196% Scripts
197 pause % auf Tastendruck warten
198 pause(n) % n Sekunden warten
199
200 if expression % bedingte Anweisung
201 statements
202 elseif expression
203 statements
204 else
205 statements
206 end
207
208
209 switch switch_expr % switch Anweisung
210 case case_expr,
211 statement, ..., statement
212 case {case_expr1, case_expr2, case_expr3,...}
213 statement, ..., statement
214 ...
215 otherwise,
216 statement, ..., statement
217 end
218
219
220 for variable = expr % for Schleife (zahler = n:1:10)
221 statement, ..., statement
222 end
223
224
225 while expression % while Schleife
226 statements
227 end
228
229
230% Funktionen
231 % in .m Datei gespeichert
232 % Funktionsname sollte Namen entsprechen
233 % Hilftext startet unterhalb der 1. Zeile
234 % Variablen sind lokal gueltig
235 function[Rueckgabeliste] = Name(Parameterliste)
236
237 nargin % Anzahl Eingabeargumente
238 nargout % Anzahl Ausgabeargumente
239 exist % existiert Variable/Funktion
240 varargin % Eingabepar.liste unbest. Laenge
241 varargout % Ausgabepar.liste unbest. Laenge
242
243 global % Variable global definieren
244
245
246% Grafiken
247 plot(x,y) % Polygonzug mit den Knoten (xk,yk)
248 plot(y) = plot(1:length(y),y)
249 plot(x,y,s) % Stilparameter s
250 % Punkttypen: .,o,x,+,*,s,d,v,^,<,>,p,h
251 % Linientypen: -,:,-.,--
252 % Farben: b,g,r,c,m,y,k
253 % Beispiel: plot(x,y,'c+:')
254 plot(x1,y1,s1,x2,y2,s2,...) % mehrere Linien
255
256 % Achsensystem
257 axis([xmin xmax ymin ymax]) % Grenzen des Achsensystems setzen
258 axis on, axis off % Achsensystem ein- und ausschalten
259 axis auto, axis manual % autom. Anpassung ein-/ausschalten
260 axis equal % gleiche Laengeneinheiten
261 axis tight % an Daten angepasster Ausschnitt
262 axis image = axis equal, axis tight
263 axis square % quadratischer Ausschnitt
264 axis fill % Ausfuellen des Bildfensters
265 grid on, grid off % Gitterlinien ein- und ausschalten
266 box on, box off % Achsensystem im Box-Format
267 pbaspect([x,y,z]) % Seitenverhaeltn. des Achsensystems
268 view(az,el) % Blickwinkel einstellen (in Grad)
269
270 % Beschriftungen
271 title % Ueberschrift
272 xlabel, ylabel, zlabel % Beschriftung der Achsen
273 legend % Legende anfuegen
274 text % Text in der Grafik
275 colorbar % Farblegende
276
277 % mehrere Grafiken
278 hold on % in einem Achsensystem
279 subplot % in einem Fenster
280 figure % in unterschiedlichen Fenstern
281 figure(figurenumber) % Umschalten zwischen Fenstern
282 clf, cla % Grafik loeschen
283 close(figurenumber), close all % Fenster schliessen
284
285 % Beispiel 1
286 t = 0:0.01:10;
287 x1 = exp(-2*t).*cos(5*t);
288 x2 = exp(-2*t).*sin(5*t);
289 figure(1), clf
290 plot(t,x1), grid
291 hold
292 plot(t,x2,'.g:')
293 hold
294 axis([0,5,-1,1])
295 xlabel('t [s]')
296 ylabel('Voltage [V]')
297 legend('Funktion 1','Funktion 2')
298 title('Darstellung von Funktionen')
299 pause
300
301 % Beispiel 2
302 d = 0.1; wk = 1e4;
303 w = logspace(1,6,1000);
304 s = j*w;
305 G = 1./(1+2*d*(s/wk)+(s/wk).^2); % Transfer function
306 figure(2), clf
307 subplot(211)
308 semilogx(w,20*log10(abs(G))), grid
309 ylabel('|G(j\omega)| [dB]');
310 title('Bodediagramm fuer G(s)');
311 subplot(212);
312 semilogx(w,180/pi*angle(G)), grid
313 xlabel('\omega [s^{-1}]');
314 ylabel('\angle G(j\omega) [grad]');
315 % Find maximum
316 [max_abs_G, i] = max(20*log10(abs(G)));
317 fprintf('\n')
318 fprintf('Max(abs(G)) in dB: %g\n', max_abs_G)
319 fprintf('bei w = %g\n', w(i))
320 pause
321
322 % Beispiel 3
323 x = 1:0.1:10;
324 y=exp(x);
325 figure(3), clf
326 semilogy(x,y)
327 grid
328
329
330% Symbolic Math Toolbox
331 % ermoeglicht Arbeiten mit symbolischen Variablen
332 syms x y
333 e = (1+x)^4/(1+x^2)+4/(1+x^2)
334 pretty(e)
335 4
336 (1 + x) 4
337 -------- + ------
338 2 2
339 1 + x 1 + x
340
341 % Vereinfachungen
342 simplify, expand, factor, collect
343 simple % Search for shortest form
344 numden % Numerator and denominator
345 horner % Nested polynomial representation
346 subexpr % Rewrite in terms of subexpressions
347 coeffs % Coefficients of a multivariate polynomial
348 sort % Sort symbolic vectors or polynomials.
349 subs % Symbolic substitutionify(e)
350
351 % grafische Darstellung von Funktionen
352 syms x
353 ezplot(exp(-x*x/2),-5,5); grid;
354
355 % sonstige Funktionen
356 taylor, diff, int, laplace, ilaplace, dsolve