einfu¨hrung in matlab dlr gottingen¨ - num.math.uni...
TRANSCRIPT
![Page 1: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/1.jpg)
Einfuhrung in MATLABDLR Gottingen
Visualisierung22.11.2005
Gerd [email protected]
Institut fur Numerische und Angewandte Mathematik
Georg-August Universitat Gottingen
Dr. G. Rapin Einfuhrung in MATLAB – p.1/64
![Page 2: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/2.jpg)
VisualisierungInput und Output
Zweidimensionale Grafikmöglichkeiten
Eindimensionale Inter- und Extrapolation
Dreidimensionale Grafikmöglichkeiten
Mehrdimensionale Interpolation
Dr. G. Rapin Einfuhrung in MATLAB – p.2/64
![Page 3: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/3.jpg)
Input / Output
Dr. G. Rapin Einfuhrung in MATLAB – p.3/64
![Page 4: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/4.jpg)
Input und OutputFormatierte Ausgabe
Schreiben in Dateien
Einlesen von Daten aus Dateien
Durch help iofun erhält man eine Übersichtaller Ein- und Ausgabe - Befehle
Dr. G. Rapin Einfuhrung in MATLAB – p.4/64
![Page 5: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/5.jpg)
AusgabeAngeben einer Variable ohne Semicolon:>> text=[’Pi mit 6 signifikanten Stellen : ’ num2str(pi,6)]
text =
Pi mit 6 signifikanten Stellen : 3.14159
Ausgabe des Strings X durch disp(X)>> disp(text)
Pi mit 6 signifikanten Stellen : 3.14159
Ausgabe durch fprintf()>> fprintf(’Pi mit %1.0f Nachkomma-Stellen : %6.4f \n’,4,p i)
Pi mit 4 Nachkomma-Stellen : 3.1416
Dr. G. Rapin Einfuhrung in MATLAB – p.5/64
![Page 6: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/6.jpg)
fprintf- Formartierte Ausgabefprintf( Format, Argument1, Argument2,...)
Format ist ein String der das genaue Output-Form der Argumente (Werte der Variablen)bestimmt:
Format=’< * >%<±> <v1.n1><typ1>< * >%<±> <v2.n2><typ2>< * >...’
<*> Hier kann beliebieger Text eingegeben werden.
<±> Durch ’+’ wird die Angabe des Vorzeichens erzwungen. Durch ’-’ wird einelinksbündige Ausgabe erzeugt. Weglassen von <±> erzeugt eine rechtsbündigeAusgabe ohne Anzeige des ’+’ Zeichens.
vi Durch vi wird die Anzahl der insgesamt dargestellten Zeichen von Argumentigesteuert.
ni Hierdurch wird entsprechend die Anzahl von Nachkommastellen angegeben.
typi Gibt den Datentyp und Darstellungsformat von Argumenti an: f(Standarddarstellung von Gleitkommazahlen), e (Expontialdarstellung von Gl.), g(entweder Darst. f oder e), s (Strings),...
Dr. G. Rapin Einfuhrung in MATLAB – p.6/64
![Page 7: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/7.jpg)
Bemerkungen zu fprintfDie formatierte Ausgabe ist an den Ansi-CStandard angelehnt.
Durch ’\n’ wird ein Zeilenumbruch bewirkt.’\%’ erzeugt %.
sprintf funktioniert wie fprintf . Allerdingswird die Ausgabe als String zurückgegeben.
Ist ein Argument eine Matrix, so wird fprintf’vektorisiert’.
Dr. G. Rapin Einfuhrung in MATLAB – p.7/64
![Page 8: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/8.jpg)
Schreiben in Dateien - Beispiel% waehrung.m
%
% Erstellt eine Umrechnungstabelle zwischen
% Euro und anderer Waehrung
waehrung_name=input(’Umrechnungstabelle fuer welche Wa ehrung ? ’,’s’);
fprintf(’Ein Euro entspricht wievielen %s ? ’,waehrung_na me);
umrechnung=input(’’);
a=[1 2 3 5 10 20 50 100 200 1000];
fid=fopen(’umrechnung.txt’,’w’);
fprintf(fid,[’Umrechnungstabelle: Euro-’,waehrung_na me,’\n\n’]);
fprintf(fid,[’%7.2f Euro = %7.2f ’,waehrung_name,’\n’], ...
[a;umrechnung * a]);
fprintf(fid,’\n \n Umrechnungskoeffizient: %3.2f \n’,um rechnung);
fclose(fid);
Dr. G. Rapin Einfuhrung in MATLAB – p.8/64
![Page 9: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/9.jpg)
fopenfid=fopen(dateiname, erlaubnis)fopen öffnet die Datei dateiname im Moduserlaubnis und erzeugt einen Datei-Handle fid .Für erlaubnis gibt es u.a. die folgendenMöglichkeiten:
’r’ Lesen aus der Datei.
’w’ Schreiben in die Datei (Erzeugen falls nötig)
’a’ Hinzufügen (Erzeugen falls nötig)
’r+’ Lesen und schreiben (aber nicht erzeugen)
Dr. G. Rapin Einfuhrung in MATLAB – p.9/64
![Page 10: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/10.jpg)
Weitere Kommandosfclose(fid) schliesst die Datei mit demHandle fidMit dem Befehlfprintf( Datei-Handle, Format, Argument1, Argument2,... )
wird in die durch das Datei-Handle angegebeneDatei gemäß der obigen Konventionengeschrieben.
Durch ein zusätzliches Output-Argumentkönnen Fehler aufgefangen werden.[fid, message]=fopen(dateiname, erlaubnis)
Ist die Datei nicht zu öffnen, so ist fid=-1 .
Dr. G. Rapin Einfuhrung in MATLAB – p.10/64
![Page 11: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/11.jpg)
Lesen aus einer Datei% waehrung_auslesen.m
%
% Liest eine Umrechnungstabelle aus der
% Datei ’umrechnung.txt’
clear all;
fid=fopen(’umrechnung.txt’,’r’);
waehrung_name=fscanf(fid,’Umrechnungstabelle: Euro-% s’);
daten=fscanf(fid,[’%f Euro = %f ’,waehrung_name],[2 inf] );
umrechnung=fscanf(fid,’Umrechnungskoeffizient: %f’);
fclose(fid);
% Ausgabe
fprintf(’Umrechnung: Euro - %s: Kurs: %f \n’,...
waehrung_name,umrechnung);
fprintf(’ %7.2f Euro = %7.2f \n’,daten);
Dr. G. Rapin Einfuhrung in MATLAB – p.11/64
![Page 12: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/12.jpg)
fscanf[daten,anz]=fscanf(fid,format,Gr oße)
fscanf liest Daten aus der Datei mit demHandle fid .
Die Daten werden in daten gespeichert. Deroptionale Wert anz gibt die Anzahl erfolgreichgelesener Daten an.
format gibt das vorgegebene Suchmuster vor.
Die Große gibt die Dimension der Output-Matrixan.
Dr. G. Rapin Einfuhrung in MATLAB – p.12/64
![Page 13: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/13.jpg)
Weitere BefehleDer Befehl fgetl(fid) liest eine Zeile aus derDatei mit Handle fid und gibt die Zeile alsString zurück.
Ob das Dateiende erreicht ist, kann durch denBefehl feof(fid) geprüft werden. feof(fid)gibt eine 1 zurück, falls das Dateiende erreichtist und 0 sonst.
Dr. G. Rapin Einfuhrung in MATLAB – p.13/64
![Page 14: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/14.jpg)
Beispiel - BubblesortBubblesort durchläuft die Datenmenge vonAnfang bis zum Ende und vergleicht paarweisedie nebeneinanderstehenden Elemente.
Sind zwei benachbarte Elemente nicht in derrichtigen Reihenfolge, so werden siemiteinander vertauscht.
Ist man am Ende angekommen, beginnt manwieder von vorne.
Die Datenmenge ist sortiert, falls bei einemDurchlauf keine Vertauschungen mehrvorgenommen werden.
Dr. G. Rapin Einfuhrung in MATLAB – p.14/64
![Page 15: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/15.jpg)
Beispiel - Bubblesortfunction sortieren(dateiname1, dateiname2)
% sortieren Die Datei dateiname1 wird alphabetisch sortier t
% und als dateiname2 abgespeichert.
% INPUT: STRING dateiname1
% STRING dateiname2
% Datei laden
[fid,message]=fopen(dateiname1,’r’);
if fid==-1
error(’Datei nicht gefunden’);
end;
% Datei lesen
anz=0;
while feof(fid)==0
anz=anz+1;
inhalt{anz}=fgetl(fid);
end
fclose(fid);
Dr. G. Rapin Einfuhrung in MATLAB – p.15/64
![Page 16: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/16.jpg)
Beispiel - Bubblesort (Forts.)% Sortieren
sortierungen=1;
while sortierungen>0
sortierungen=0;
for k=1:anz-1
% vergleich_gr(a,b) ist 1 fuer a<b, 0 sonst
if vergleich_gr(inhalt{k+1},inhalt{k})
hilf=inhalt{k}; inhalt{k}=inhalt{k+1}; inhalt{k+1}=hi lf;
sortierungen=sortierungen+1;
end
end
end
% Datei schreiben
fid=fopen(dateiname2,’w’);
for k=1:anz
fprintf(fid,’%s \n’,inhalt{k});
end;
fclose(fid);
Dr. G. Rapin Einfuhrung in MATLAB – p.16/64
![Page 17: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/17.jpg)
BemerkungenEs ist auch möglich temporäre Dateien zuerzeugen.
Binäre Dateien können erzeugt und gelesenwerden mit Hilfe der Befehle fread undfwrite .
Mittels xlsread können Excel-Tabelleneingeladen werden.
Bilddateien werden durch imread importiert.
Audiodateien (.wav) bzw. Videodateien (.avi)können durch wavread bzw. avireadimportiert werden.
Dr. G. Rapin Einfuhrung in MATLAB – p.17/64
![Page 18: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/18.jpg)
Beispiel: Binäre Daten%-------------------- beispiel_bin_data.m
A = hilb(10);
% Schreibe binaere Datei
fwriteid = fopen(’hilb10.bin’,’w’);
count = fwrite(fwriteid,A,’double’);
fclose =(fwriteid);
% Lesen binaere Datei
freadid = fopen(’hilb10.bin’,’r’);
B = fread(freadid, count, ’double’);
C = reshape(B,10,10);
disp(norm(A - C))
Dr. G. Rapin Einfuhrung in MATLAB – p.18/64
![Page 19: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/19.jpg)
Statistische GrößenMinimum min , Maximum max
Durschschnittswert mean, Median median
Summe sum, Produkt prod
Standardabweichung std
Dr. G. Rapin Einfuhrung in MATLAB – p.19/64
![Page 20: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/20.jpg)
Statistische Größen>> x=1:2:100;>> [min(x), max(x)]
ans = 1 99>> [mean(x), median(x)]
ans = 50 50>> sum(x), prod(x)
ans = 2500ans = 2.7254e+78
>> std(x)ans = 29.1548
Dr. G. Rapin Einfuhrung in MATLAB – p.20/64
![Page 21: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/21.jpg)
ZweidimensionaleGrafikmöglichkeiten
Dr. G. Rapin Einfuhrung in MATLAB – p.21/64
![Page 22: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/22.jpg)
Standard–Plotplot(x,y)zeichnet für Vektoren x = (x1, . . . , xN) undy = (y1, . . . , yN) eine Grafik, die die Punkte (xi, yi)und (xi+1, yi+1) miteinander verbindet.
>> x=linspace(0,2 * pi,100);
>> y1=sin(3 * x);
>> plot(x,y1)
0 1 2 3 4 5 6 7−1
−0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
1
Dr. G. Rapin Einfuhrung in MATLAB – p.22/64
![Page 23: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/23.jpg)
Erweiterungenplot(x,y, string )
String besteht aus drei Elementen, die die Farbe,Linienstil und die Markierung der Punktekontrollieren. Die Reihenfolge der drei Elemente istbeliebig.
Beispiel: Durchplot(x,y,’r * --’) wirddie Linie gestrichelt (- -) inrot (r) gezeichnet und diePunkte durch * markiert.
0 1 2 3 4 5 6 7−1
−0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
1
Dr. G. Rapin Einfuhrung in MATLAB – p.23/64
![Page 24: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/24.jpg)
Optionen
Farben r (rot), g (grün), b (blau), c (hellblau),m (magenta), y (gelb), k (schwarz), w(weiß)
Marker o (Kreis), * (Stern), . (Punkt), + (Plus),x (Kreuz), s (Quadrat), d (Raute),...
Linien-Stil - (durchgezogene Linie), -- (gestri-chelte Linie), : (gepunktete Linie), -.(Strich-Punkt Linie)
Läßt man den Linien-Stil weg, so werden die Punkte
nicht verbunden.
Dr. G. Rapin Einfuhrung in MATLAB – p.24/64
![Page 25: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/25.jpg)
Optionen IIplot(x,y, string , Eigenschaft , Spez.)
Eigenschaften:’MarkerSize’ (Default 6), ’LineWidth’ (Default0.5), ’MarkerEdgeColor’ , ’MarkerFaceColor’
Beispiel:
>>plot(x,y1,’b-.d’,’LineWidth’,3,...
’MarkerEdgeColor’,’g’)
0 1 2 3 4 5 6 7−1
−0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
1
Dr. G. Rapin Einfuhrung in MATLAB – p.25/64
![Page 26: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/26.jpg)
Beispiel - Legendre Polynome
>> x=linspace(-1,1,100);
>> p1=x;
>> p2=(3/2) * x.ˆ2-1/2;
>> p3=(5/2) * x.ˆ3-(3/2) * x;
>> p4=(35/8) * x.ˆ4 - (15/4) * x.ˆ2+3/8;
>> plot(x,p1,’r:’,x,p2,’g--’,x,p3,...
’b-.’,x,p4,’m-’,’LineWidth’,2)
−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1−1
−0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
1
Dr. G. Rapin Einfuhrung in MATLAB – p.26/64
![Page 27: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/27.jpg)
Darstellung von Daten
Daten:>> n=linspace(0,10,40);>> y=n.ˆ2. * exp(-n);
Balkendiagramm: bar(y)
Histogramm: hist(y,5)
einfacher Plot: area(n,[y’,2 * y’])
Tortengrafik: pie3([ 1 2 3 4])
Dr. G. Rapin Einfuhrung in MATLAB – p.27/64
![Page 28: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/28.jpg)
Darstellung von Daten
0 10 20 30 40 500
0.2
0.4
0.6Balkendiagramm
0 0.2 0.4 0.60
5
10
15
20Histogramm
0 2 4 6 8 100
0.5
1
1.5
2Area plot
40%
30%
Tortengrafik
10%
20%
Dr. G. Rapin Einfuhrung in MATLAB – p.28/64
![Page 29: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/29.jpg)
Darstellung von Datenn=linspace(0,10,40);
y=n.ˆ2. * exp(-n);
% Balkendiagramm
subplot(2,2,1),
bar(y); title(’Balkendiagramm’);
% Histogramm
subplot(2,2,2),
hist(y,5); title(’Histogramm’);
% Area plot
subplot(2,2,3),
area(n,[y’,2 * y’]); title(’Area plot’);
% Tortengrafik
subplot(2,2,4),
pie3([ 1 2 3 4]); title(’Tortengrafik’);
Dr. G. Rapin Einfuhrung in MATLAB – p.29/64
![Page 30: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/30.jpg)
Aufgabe 1Betrachten Sie die Datei daten.dat mittelsdes Befehls type daten.dat
Schreiben Sie ein Programm, dass die Datenimportiert und die Funktion anhand dergegebenen Daten plottet.
Dr. G. Rapin Einfuhrung in MATLAB – p.30/64
![Page 31: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/31.jpg)
Aufgabe 2Importieren Sie die Daten mittelsload beispiel.mat .
Stellen Sie die Daten grafisch dar.
Erstellen Sie ein Histogramm.
Berechnen Sie das Maximum, das Minimum unddie Standardabweichung.
Dr. G. Rapin Einfuhrung in MATLAB – p.31/64
![Page 32: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/32.jpg)
PolynomeInterpolationExtrapolation
Dr. G. Rapin Einfuhrung in MATLAB – p.32/64
![Page 33: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/33.jpg)
PolynomeIn MATLAB werden Polynome
p(x) = p1xn + p2x
n−1 + · · · + pn+1
repräsentiert durch einen Zeilenvektorp = [p(1) p(2) . . . p(n + 1)].
Vorsicht: Normalerweise werden Polynome in der
Form∑n
i=0pix
i dargestellt. In MATLAB dagegen ist
die Darstellung invers und beginnt bei 1.
Dr. G. Rapin Einfuhrung in MATLAB – p.33/64
![Page 34: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/34.jpg)
Problemstellungen1. Auswerten: Bei gegebenen Koeffizienten, das
zugehörige Polynom an bestimmten Stellenauswerten.
2. Nullstellenbestimmung: Bestimme zugegebenen Koeffizienten die Nullstellen deszugehörigen Polynoms.
3. Interpolation: Bestimme zu einer gegebenenMenge von Punkten (xi, yi)
ni=0
ein Polynomn.-ten Grades, das durch diese Punkte verläuft.
Dr. G. Rapin Einfuhrung in MATLAB – p.34/64
![Page 35: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/35.jpg)
AuswertenDurch y=polyval(p,x) werden aus einemvorgegebenen Koeffizientenvektor p undentsprechenden Stellen x die zugehörigenFunktionswerte y berechnet. x kann eine Matrixsein. y ist dann von der gleichen Dimens. wie x.
Beispiel: p(x) := x3− x2 + 1
>> x=-2:0.1:2;
>> y=polyval([1 -1 0 1],x);
>> plot(x,y,’r--’,’Linewidth’,3);
−2 −1.5 −1 −0.5 0 0.5 1 1.5 2−12
−10
−8
−6
−4
−2
0
2
4
6
Dr. G. Rapin Einfuhrung in MATLAB – p.35/64
![Page 36: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/36.jpg)
Bestimmung von NullstellenIst p der obige Koeffizientenvektor, so können dieNullstellen z durch z= roots(p) berechnet werden.
Beispiel:Nullstellen von p(x) := x3
− x2 + 1>> roots([1 -1 0 1])
ans =
0.8774 + 0.7449i
0.8774 - 0.7449i
-0.7549
>> x=-1:0.1:1; [X,Y]=meshgrid(x,x);
>> Z=abs(polyval([1 -1 0 1],X+i * Y));
>> surf(X,Y,Z)−1
−0.5
0
0.5
1
−1−0.8
−0.6−0.4
−0.20
0.20.4
0.60.8
1
0
1
2
3
4
5
Dr. G. Rapin Einfuhrung in MATLAB – p.36/64
![Page 37: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/37.jpg)
InterpolationSuche zu gegebenen Punkten (xi, yi)
ni=0
einPolynom p n.-ten Grades, so dass p(xi) = yi gilt füri = 0, . . . , n.In MATLAB: p=polyfit(x,y,n)
Ruft man p=polyfit(x,y,m) mit m < n auf, sosucht MATLAB die Least Square Lösung, d.h. dasPolynom p der Ordnung m, welches∑n
i=0(p(xi) − yi)
2 minimiert.
Dr. G. Rapin Einfuhrung in MATLAB – p.37/64
![Page 38: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/38.jpg)
Data FittingEin weiterer Befehl zur Interpolation istyi=interp1(x,y,xi,’method’).Dabei sind (x, y) die gegebenen Punkte, xi sind dieStellen, an die die Interpolante berechnet wird undyi sind die entsprechenden Funktionswerte. Als’methode’ gibt es
’nearest’ stückweise konstante Approximation
’linear’ Lineare Interpolation
’spline’ stückweise kubischer Spline u (u ∈ C2, u|[xi,xi+1] ∈ P3)
’cubic’ kubische Hermite Interpolation
Dr. G. Rapin Einfuhrung in MATLAB – p.38/64
![Page 39: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/39.jpg)
Beispiel
−5 0 50
0.2
0.4
0.6
0.8
1Punkte
−5 0 5−0.5
0
0.5
1
1.5
2polynomial
−5 0 50
0.2
0.4
0.6
0.8
1Linearer Spline
−5 0 50
0.2
0.4
0.6
0.8
1Kubischer Spline
−5 0 50
0.2
0.4
0.6
0.8
1kubische Interpol.
−5 0 50
0.2
0.4
0.6
0.8
1konst. Approx.
Dr. G. Rapin Einfuhrung in MATLAB – p.39/64
![Page 40: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/40.jpg)
BemerkungenNur für die Spline-Methoden können beiinterp1 auch Stellen außerhalb desInterpolationsintervalls berechnet werden.
Data Fitting kann auch über die Oberflächedurchgeführt werden. Plotten Sie die Daten undwählen Sie Basic Fitting im Menü Tools .
Dr. G. Rapin Einfuhrung in MATLAB – p.40/64
![Page 41: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/41.jpg)
Aufgabe 3(a) Laden Sie mittels load census die U.S.
Population von 1790 bis 1990 in ihren Speicherund stellen Sie die Zahlen grafisch dar.
(b) Interpolieren Sie mit Hilfe der Oberfläche dieDaten! Welche Methode funktioniert am besten!
(c) Schätzen Sie mit Hilfe des kubischen Splinesdie Bevölkerungszahl 2050.
Dr. G. Rapin Einfuhrung in MATLAB – p.41/64
![Page 42: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/42.jpg)
DreidimensionaleGrafikmöglichkeiten
Dr. G. Rapin Einfuhrung in MATLAB – p.42/64
![Page 43: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/43.jpg)
Dreidimensionale GrafikenDreidimensionale Version von plot : plot3
Darstellung von Funktionen f : R2→ R:
• Contourplot (zeichnet die Niveaulinien):contour , contourf , contour3
• Darstellung des Graphen mit Gitterlinien:mesh , meshc
• Flächige Darstellung des Graphen: surf ,surfc
Darstellung von Funktionen f : R3→ R:
Streifenansichten slice
Dr. G. Rapin Einfuhrung in MATLAB – p.43/64
![Page 44: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/44.jpg)
Beispiel plot3t=0:0.1:20 * pi;x=exp(-t/20). * sin(t);y=exp(-t/20). * cos(t);z=t;
plot3(x,y,z,’b-o’,’LineWidth’,1);grid onxlabel(’x(t)’), ylabel(’y(t)’);
zlabel(’z(t)’);title(’Beispiel: plot3’,’FontSize’,15);
Dr. G. Rapin Einfuhrung in MATLAB – p.44/64
![Page 45: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/45.jpg)
3D-FunktionenplotsDarstellung von Funktionen
f : R2
→ R
Beispiel:
f(x, y) := exp(−x2− y2) sin(πxy)
Dr. G. Rapin Einfuhrung in MATLAB – p.45/64
![Page 46: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/46.jpg)
Beispiel: Funktionenplot
−20
2
−2
0
2−0.5
0
0.5
mesh
−20
2
−2
0
2−0.5
0
0.5
surf
−2 −1 0 1 2−2
−1
0
1
2contour
−20
2−2
0
2−0.5
0
0.5
surfc
Dr. G. Rapin Einfuhrung in MATLAB – p.46/64
![Page 47: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/47.jpg)
Programm% Erzeugen des Gitters
x=linspace(-2,2,30);
y=linspace(-2,2,30);
[X,Y]=meshgrid(x,y);
% Funktionswerte
Z=exp(-X.ˆ2-Y.ˆ2). * sin(pi * X. * Y);
% verschiedenen Darstellungen
subplot(2,2,1),
mesh(X,Y,Z), title(’mesh’);
subplot(2,2,2),
surf(X,Y,Z), title(’surf’);
subplot(2,2,3),
contour(X,Y,Z,10), title(’contour’);
subplot(2,2,4),
surfc(X,Y,Z);
view(-26,20), title(’surfc’);
Dr. G. Rapin Einfuhrung in MATLAB – p.47/64
![Page 48: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/48.jpg)
meshgrid
Zu Vektoren x = (xi)ki=1
, y = (yj)nj=1
erzeugt[X,Y]=meshgrid(x,y)
Matrizen X,Y ∈ Rn×k, wobei jede Zeile von X eine
Kopie des Vektors x ist und Y als Spalten denVektor y enthält.Dann hat Z=X. * Y die Komponenten
Z(i, j) = x(j) ∗ y(i).
Dr. G. Rapin Einfuhrung in MATLAB – p.48/64
![Page 49: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/49.jpg)
DarstellungsmöglichkeitenContourplot (zeichnet die Niveaulinien):contour
Darstellung des Graphen mit Gitterlinien: mesh ,meshc
Flächige Darstellung des Graphen: surf ,surfc
mesh(X,Y,Z) z.B. stellt für MatrizenX,Y, Z ∈ R
n×k die Punkte
(X(i, j), Y (i, j), Z(i, j)) dar.
(Die Koordinaten (x,y) müssen ein kartesisches Gitter bilden).Dr. G. Rapin Einfuhrung in MATLAB – p.49/64
![Page 50: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/50.jpg)
Contour Plots% verschiedenen Darstellungen
subplot(2,2,1),
contourf(X,Y,Z,10), title(’contourf’)
subplot(2,2,2),
contour(X,Y,Z,[0 0.2 0.4]), title(’special contours’);
subplot(2,2,3),
[C,h]=contour(X,Y,Z,[0 0.05 0.1 0.15 0.2 ]);
title(’contour with labeling’);
clabel(C,h)
subplot(2,2,4),
contour3(X,Y,Z,10), title(’contour3’)
Dr. G. Rapin Einfuhrung in MATLAB – p.50/64
![Page 51: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/51.jpg)
Funktionenplota mit Contour
contourf
−2 −1.5 −1 −0.5 0 0.5 1 1.5 2−2
−1.5
−1
−0.5
0
0.5
1
1.5
2special contours
−2 −1.5 −1 −0.5 0 0.5 1 1.5 2−2
−1.5
−1
−0.5
0
0.5
1
1.5
2
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0.05
0.05
0.05
0.05
0.05
0.0
5
0.1
0.1
0.1
0.1
0.15
0.15
0.2
0.2
contour with labeling
−2 −1.5 −1 −0.5 0 0.5 1 1.5 2−2
−1.5
−1
−0.5
0
0.5
1
1.5
2
−2−1
01
2
−2
0
2−0.4
−0.2
0
0.2
0.4
contour3
Dr. G. Rapin Einfuhrung in MATLAB – p.51/64
![Page 52: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/52.jpg)
Erläuterungen zu Contourcontour(X,Y,Z,n) zeichnet für n ∈ N
n-Konturlinien. Ist n ein Vektor, werdenKonturlinien zu den Werten in dem Vektor ngeplottet.
contourf funktioniert wie contour nur das dieFlächen zwischen den Konturlinien ausgefülltwerden.
label(C,h) beschriftet die Konturlinien, derenWerte in C gespeichert sind und die zumGrafik-Handle h gehören.
contour3 zeichnet jede Konturlinie auf eineranderen Höhe.
Dr. G. Rapin Einfuhrung in MATLAB – p.52/64
![Page 53: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/53.jpg)
Sliceslice(X,Y,Z,V,sx,sy,sz)
zeichnet Schnitte zu den Funktionswerten V (i) zu(X(i), Y (i), Z(i)). Schnitte sind durch die Vektorensx, sy und sz gegeben.
Beispiel:
f(x, y, z) := exp(−x2− y2) sin(πxyz)
Dr. G. Rapin Einfuhrung in MATLAB – p.53/64
![Page 54: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/54.jpg)
Beispielx = linspace(-2,2,20);[X,Y,Z] = meshgrid(x,x,x);V = exp(-X.ˆ2-Y.ˆ2). * sin(pi * X. * Y. * Z);sx = [-0.5,0,0.5]; sy = [-1,0,1];sz = [];slice(X,Y,Z,V,sx,sy,sz)alpha(0.6) % Transparency
Dr. G. Rapin Einfuhrung in MATLAB – p.54/64
![Page 55: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/55.jpg)
MehrdimensionaleInterpolation
Dr. G. Rapin Einfuhrung in MATLAB – p.55/64
![Page 56: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/56.jpg)
ProblemDaten liegen häufig in Form von Vektoren(x, y, z) vor. Man möchte eine Funktion F mitz(i) = F (x(i), y(i)) plotten.
Befehle surf und mesh funktionieren nur wenndie Einträge in x und y monoton sind und dieDaten auf einem kartesischen Gitter vorliegen.
Ausweg: Interpolieren der Daten auf einentsprechendes Gitter.
Dr. G. Rapin Einfuhrung in MATLAB – p.56/64
![Page 57: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/57.jpg)
Beispiel>> load seamount>> plot(x,y,’.’,’markersize’,10)>> figure, plot3(x,y,z,’.’)
210.8 210.9 211 211.1 211.2 211.3 211.4 211.5 211.6 211.7 211.8−48.45
−48.4
−48.35
−48.3
−48.25
−48.2
−48.15
−48.1
−48.05
−48
−47.95
Dr. G. Rapin Einfuhrung in MATLAB – p.57/64
![Page 58: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/58.jpg)
Beispiel>> xi=linspace(min(x),max(x),40);>> yi=linspace(min(y),max(y),40);>> [XI,YI]=meshgrid(xi,yi);>> ZI=griddata(x,y,z,XI,YI,’cubic’);>> surf(XI,YI,ZI)
210.8
210.9
211
211.1
211.2
211.3
211.4
211.5
211.6
211.7
211.8
−48.45−48.4−48.35−48.3−48.25−48.2−48.15−48.1−48.05−48−47.95
−4000−2000
0
Dr. G. Rapin Einfuhrung in MATLAB – p.58/64
![Page 59: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/59.jpg)
griddataZI=griddata(x,y,z,XI,YI,methode);
Vektoren x, y, z enthalten Werte (x(i), y(i), z(i)).
griddata interpoliert auf die Stellen(XI(i, j), Y I(i, j)) mit Matrizen XI, Y I. ErgebnisZI(i, j).
Die Art des Interpolierens ist entweder ’nearest’ ,’linear’ oder ’cubic’ . Entsprechend wirdentweder stückweise konstant, linear oder durchbi-kubische Splines interpoliert.
Es wird nur innerhalb der konvexen Hülle der Punkte(x(i), y(i)) interpoliert. Ansonsten FunktionswertNaN .
Dr. G. Rapin Einfuhrung in MATLAB – p.59/64
![Page 60: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/60.jpg)
BemerkungenDer Interpolation liegt eine DelaunayTriangulation zugrunde. Die Werte (x(i), y(i))sind Eckpunkte der entstehendenDreiecksmenge.
Danach werden mit Hilfe der DreieckeFunktionen definiert, die entsprechende Wertebesitzen.
Mittels griddatan ist die Technik auch aufhöhere Dimensionen anwendbar. Dreieckewerden durch entsprechendehöher-dimensionale Simplizes ersetzt.(In 3D Tetraeder)
Dr. G. Rapin Einfuhrung in MATLAB – p.60/64
![Page 61: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/61.jpg)
interp2ZI=interp2(X,Y,Z,XI,YI,methode)
Allgemein sind X,Y, Z Matrizen. Dabei istZ(i, j) der Funktionswert an (X(i, j), Y (i, j)).X und Y sind in der Regel durch meshgriderzeugt.
Es wird an den Stellen (XI(i, j), Y I(i, j))interpoliert. Das Ergebnis ist ZI(i, j). DieEinträge von XI bzw. Y I können beliebig sein.
Die Art des Interpolierens ist entweder’nearest’ , ’linear’ oder ’cubic’ .
Dr. G. Rapin Einfuhrung in MATLAB – p.61/64
![Page 62: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/62.jpg)
Aufgabe 4Plotten Sie die Funktionen
(a) f(t) := ((1 + t2) sin(20t), (1 + t2) cos(20t), t),t ∈ [−5, 5],
(b) g(x, y) := sin(3y − x2 + 1) + cos(2y2− 2x) auf
[−2, 2] × [−1, 1] (mittels contour )
(c) h(z) := real(z1
3 ), z = x + iy mit x, y ∈ [−1, 1](mittels mesh)
Dr. G. Rapin Einfuhrung in MATLAB – p.62/64
![Page 63: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/63.jpg)
Aufgabe 5Erzeugen Sie die beiden Vektorenx = linspace(-5,5,13);y = 1./(1+x.ˆ2);
Plotten Sie die Punkte (x, y).
Interpolieren Sie die Daten mit Hilfe einesPolynoms 12.-Ordnung, 3.-Ordnung und 8.-terOrdnung. Benutzen Sie auch die anderenMethoden.
Vergleichen Sie die Ergebnisse.
Dr. G. Rapin Einfuhrung in MATLAB – p.63/64
![Page 64: Einfu¨hrung in MATLAB DLR Gottingen¨ - num.math.uni ...num.math.uni-goettingen.de/grapin/matlab/ws2005/Vorlesung_MATLA… · Einfu¨hrung in MATLAB DLR Gottingen¨ Visualisierung](https://reader033.vdocuments.pub/reader033/viewer/2022051009/5a78bf947f8b9a4f1b8ba655/html5/thumbnails/64.jpg)
Aufgabe 6Erzeugen Sie drei Vektoren durch
x = rand(2000,1);y = rand(2000,1);z = sin(4 * pi * x). * cos(2 * pi * y);
Plotten Sie zuerst nur die Punkte.
Erstellen Sie Grafiken mit surf , mesh undcontour .
Beschriften Sie die Konturlinien von contour .
Untersuchen Sie den Einfluß der verschiedenenInterpolationsmethoden.
Dr. G. Rapin Einfuhrung in MATLAB – p.64/64