3. Grafik in MATLAB (1) - TU Bergakademie Freibergqueck/lehre/math/matlab/Kurs16/kurs16_3.… ·...
Transcript of 3. Grafik in MATLAB (1) - TU Bergakademie Freibergqueck/lehre/math/matlab/Kurs16/kurs16_3.… ·...
Start Inhalt Grafik in MATLAB (1) 1(1)
3. Grafik in MATLAB (1)
3.1 Das figure-Fenster
Figures und Plots. Figure-GUI.
3.2 2D – Grafik
Die plot-Anweisung. Gestaltung von plots.
3.3 Beispiele
3.4 Speichern von Grafiken
*.fig. Andere Bildformate. Generierung von m-Files.
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) Das figure-Fenster 2(1)
Das figure-Fenster
MATLAB stellt Grafik in einem separaten Fenster Figure dar.
Man kann mit mehreren ”figures” arbeiten. Eins ist das aktuelle,und in genau diesem werden Grafikanweisungen realisiert.
Es gibt viele Moglichkeiten zur interaktiven Manipulation vonGrafiken.Siehe dazu die Online–Hilfe:Graphics – 2-D and 3-D Plots undGraphics – Formatting and Annotation
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) 2D – Grafik 3(1)
2D – Grafik
I Basisanweisung plot.I Gestaltung von Grafiken.I Mehrere Plots in einem Fenster: subplotI Weitere Zeichenbefehle. Beispiele.
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) 2D – Grafik 4(1)
Darstellung von Wertepaaren: plot
Daten: x = [1.5 2.2 3.1 4.6 5.7 6.3 9.4]
y = [2.3 3.9 4.3 7.2 4.5 3.8 1.1]
plot(y) : verbindet Punkte (j , yj)plot(x,y) : verbindet (xj , yj)plot(x,y,Zeichenkette) : Spezifiziert Farbe, Strichart, Symbole, etwaplot(x,y,’r*--’) : rote Sternchen u. gestrichelte Linie
(Siehe help plot fur alle Optionen.)
>> t = [0:20]/20*2*pi; s = sin(t); c = cos(t);
>> plot(t,s,’g-’,t,c,’r--’)
>> plot(t,[s;c])
>> plot([t;t]’,[s;c]’)
>> plot(exp(i*t),’bp-’), axis equal
>> plot(t,exp(i*t))
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) 2D – Grafik 5(1)
Weitere Einstellungen bei plot
Festlegung der Eigenschaften LineWidth (Strichstarke, default 2)sowie MarkerSize (Symbolgroße, default 6):
>> plot(x,y,’LineWidth’,2)
>> plot(x,y,’p’,’MarkerSize’,10)
Die Einheit hierbei ist Point=1/72 Zoll (inch).Bei Symbolen mit wohldefiniertem Inneren kann auch dieInnenfarbe spezifiziert werden.
>> plot(x,y,’s’,’MarkerEdgeColor’,’m’,’MarkerFaceColor’,’g’)
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) 2D – Grafik 6(1)
Nutzliche Grafik-Anweisungen
hold on : folgende Plots uber bestehende Plots zeichnenhold off : folgende Plots ersetzen bestehende Plotshold : wechselt zwischen diesen Modi hin und herclf : loscht aktuelles Grafikfensterfigure : offnet neues Grafikfensterfigure(n) : macht n-tes bestehendes Grafikfenster zum aktuellen
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) 2D – Grafik 7(1)
Manipulation des Koordinatensystems
axis([xmin xmax ymin ymax]) : legt Koordinatebereich festaxis auto : automatische Bereichswahlaxis equal : gleicher Skalierung aller Achsenaxis square : macht Koordinatenbereich quadratischaxis tight : Koordinatengrenzen = Datenextremaxlim([xmin xmax]) : x-Bereich festlegenylim([ymin ymax]) : y -Bereich festlegen
Beispiel:
>> plot(fft(eye(17)))
>> axis equal
>> axis square
>> axis off
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) 2D – Grafik 8(1)
Beschriftung von Plots
title(’zk’) : Zeichenkette ’zk’ wird Uberschriftxlabel(’zk’) : beschriftet die x–Achseylabel(’zk’) : beschriftet die y–Achsezlabel(’zk’) : beschriftet die z–Achselegend(...) : Erzeugung einer Legende (viele Varianten)colorbar(...) : Hizufugen eines Farbbalkensannotation(...) : Hizufugen von Grafikobjektentext(x,y,’zk’) : Hizufugen von ’zk’ an Position (x,y)
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) 2D – Grafik 9(1)
Mehrere Plots in einem Grafikfenster: subplotsubplot(m,n,p) teilt das Grafikfenster in m ∗ n Teilfenster undmacht das p-te (1 ≤ p ≤ nm) aktuell.Beispiel:
>> subplot(2,2,1), fplot(’exp(sqrt(x)*sin(12*x))’,[0 2*pi])
>> subplot(2,2,2), fplot(’sin(round(x))’,[0,10],’--’)
>> subplot(2,2,3), fplot(’cos(30*x)/x’,[0.01 1 -15 20],’-.’)
>> subplot(2,2,4),
>> fplot(’[sin(x),cos(2*x),1/(1+x)]’,[0 5*pi -1.5 1.5])
Was auch geht:
>> x = linspace(0,15,100);
>> subplot(2,2,1), plot(x,sin(x))
>> subplot(2,2,2), plot(x,round(x))
>> subplot(2,1,2), plot(x,sin(round(x)))
>> subplot(2,2,3:4), plot(x,sin(round(x)))
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) 2D – Grafik 10(1)
Weitere Zeichenbefehle
Ausser der plot-Anweisung gibt es weitere Anweisungen, um2D-Grafiken zu erzeugen.
Auswahl:
plot plotyy
loglog semilogx semilogy
bar barh hist pie
contour ezplot ezcontour
...
Vollstandiger Uberblick in der Online-Hilfe unterGraphics – 2-D and 3-D Plots
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) Beispiele 11(1)
Beispiel 1 : Singularitat
Wir zeichnen den Graphen einer Funktion mit zwei Polen, dererst durch Einschrankung des y -Achsenbereichs informativwird.
>> x = linspace(0,3,500);
>> plot(x,1./(x-1).^2 + 3./(x-2).^2)
>> grid on
>> ylim([0 50])
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) Beispiele 12(1)
Beispiel 2: Epizykloid
Beispiel zu Achseneinstellungen
Wir zeichnen den Epizykloiden gegeben durch die Gleichung
x(t) = (a + b) cos(t)− b cos((a/b + 1)t)y(t) = (a + b) sin(t)− b sin((a/b + 1)t)
fur t ∈ [0,10π] und a = 12,b = 5.
Siehe Script-Datei epizykloidBsp.m (Higham-Buch).
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) Beispiele 13(1)
Beispiel 3: Legendre-PolynomeBeispiel fur xlabel, legend, text
Wir erstellen ein Schaubild mit den Graphen ersten vierLegendre-Polynome
P1(x) = x
P2(x) =32
x2 − 12
P3(x) =52
x3 − 32
x
P4(x) =358
x4 − 154
x2 +38
Siehe Script-Datei legendreBsp.m (Higham-Buch).Beachte Verwendung der Funktionen legend,text sowie derEigenschaft ’FontAngle’.
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) Beispiele 14(1)
Beispiel 4: Bezier-Kurve
Beispiel fur fill, text Wir zeichnen die kubische
Bezier-Kurve
p(u) = (1−u)3P1+3u(1−u)2P2+u2(1−u)P3+u3P4, 0 ≤ u ≤ 1,
mit den vier Kontollpunkten P1, . . . ,P4. Mittels fill farben wirdas durch die vier Kontrollpunkte gegebene Kontrollpolygon.
Siehe Script-Datei bezierBsp.m (Higham-Buch).
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) Beispiele 15(1)
Beispiel 5: Anwendung fplot
Zeichnen des Graphs einer Funktion f (t)
>> f = @(t) 3*exp(-0.2*t).*sin(pi/10*t+pi/4);
>> fplot(f,[0 6*pi])
Alternative zu
>> f = @(t) 3*exp(-0.2*t).*sin(pi/2*t+pi/4);
>> t = linspace(0,6*pi,200);
>> plot(t,f(t))
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) Beispiele 16(1)
Beispiel 6: Anwendung ezplot und polar
Die Gleichung einer Lemniskate lautet in kartesischen Koordinaten
(x2 + y2)− 2a2(x2 − y2) = 0 (a > 0)
und in Polarkoordinaten
ρ = a√
2 cos(2ϕ) (a > 0)
Mit a = 3:
>> lemniskate = @(x,y)(x.^2+y.^2).^2 -2*3^2*(x.^2-y.^2);
>> h = ezplot(lemniskate); set(h,’LineWidth’,2);
oder
>> lempol=@(phi) 3*sqrt(2*cos(2*phi));
>> t = linspace(-pi/4,pi/4,200);
>> rho = lempol(t);
>> polar(t,rho); hold on; polar(t,-rho);
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) Beispiele 17(1)
Beispiel 7: Anwendung ezplot
Die Gleichung einer Astroide lautet in kartesischen Koordinaten
(x2/3 + y2/3) = a2/3 (a > 0)
und in Parameterform
x = a cos3(ϕ), y = a sin3(ϕ) (a > 0)
Mit a = 3:
>> astroide = @(x,y) abs(x).^(2/3) + abs(y).^(2/3) - 3^(2/3);
>> h = ezplot(astroide,[-4 4 -4 4]);
>> set(h,’LineWidth’,2), axis equal
oder
>> asx = @(phi) 3*cos(phi).^3;
>> asy = @(phi) 3*sin(phi).^3;
>> ezplot(asx,asy,[0 2*pi])
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) Beispiele 18(1)
2D Balkendiagramme
barBsp.m (Higham-Buch)
1 2 3 4 50
2
4
6
8
10bar(...,’grouped’)
0 5 10 15 200
2
4
6
8
10bar(...,’grouped’)
1 2 3 4 50
5
10
15
20bar(...,’stacked’)
0 2 4 6 8 10
1
2
3
4
5
barh
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) Beispiele 19(1)
3D Balkendiagramme
barBsp2.m (Higham-Buch)
12
3
12
34
5
0
5
10
bar3(...,’detatched’)
12
34
5
0
5
10bar3(...,’grouped’)
12
34
5
0
10
20
bar3(...,’stacked’)
0
5
10
1
2
3
4
5
bar3h
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) Beispiele 20(1)
Histogramme
histBsp.m (Higham-Buch)
0 1 2 30
50
100
150
200
250
3001000 mal 1 Datenvektor, 10 Behälter
0 1 2 30
20
40
60
80
100
120
14025 Behälter
0 1 2 30
20
40
60
80
100
120
140Behälterbreite 0.1
0 1 2 3 40
50
100
150
200
250
300
3501000 mal 3 Datenmatrix
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) Beispiele 21(1)
Kuchendiagramme
pieBsp.m (Higham-Buch)
16%
37%
46%
16%
37%
46%
Stück 1
Stück 2
Stück 3
46%
37%
16%
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) Beispiele 22(1)
Flachendiagramme
areaBsp.m (Higham-Buch)
2 4 6 8 10 12 14 16 18 20 22
−5
0
5
10
15
20
25
30
2 4 6 8 10 12 14 16 18 20 220
5
10
15
20
25
30
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) Speichern von Grafiken 23(1)
Speichern von Grafiken (Figure-Fenster)
Das MATLAB-eigene Bildformat hat die Erweiterung fig.
Im Figure–Fenster wird mit File – Save eine Grafik imfig-Format gespeichert und mittels File – Open geladen.
Unter File – Save As kann man alternative Formate auswahlen,z. B. eps, jpg, pdf, tif, ...
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) Speichern von Grafiken 24(1)
Speichern von Grafiken (Kommandozeile)
Mit dem print-Kommando lassen sich MATLAB-Grafiken insehr vielen Formaten auf eine Datei schreiben.
Beispiel: Als farbige Encapsulated Postscript Level 2 Dateimyfig.eps speichern:print -depsc2 myfig.eps
Mit saveas kann eine Grafik in einem in MATLABwiederladbaren Format abgespeichert werden.
Beispiel: Speichern der aktuellen Grafik als myfig.fig unddanach wieder in MATLAB laden:saveas(gcf, ’myfig’, ’fig’)
open(’myfig.fig’)
TUBA Fak 1, WS 2016/17, SS 2017
Start Inhalt Grafik in MATLAB (1) Speichern von Grafiken 25(1)
Speichern von Grafiken (m–File)
Im Figure-Fenster kann man durch Auswahl vonFile – Generate M-File ...ein m-File mit der function-Zeile
function createfigure(...)
erzeugen lassen, welches gespeichert, editiert, gestartetwerden kann.
TUBA Fak 1, WS 2016/17, SS 2017