54
Numerische Mathematik Vorlesungen, ¨ Ubungen, Algorithmen und Programme Privatdozent Dr. rer. nat. habil. Werner Neundorf Institut f¨ ur Mathematik der Fakult¨ at f¨ ur Mathematik und Naturwissenschaften der Technischen Universit¨ at Ilmenau Ilmenau 2002 Datei: maplemws.ps, *.pdf

Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

Numerische Mathematik

Vorlesungen, Ubungen,

Algorithmen und Programme

Privatdozent Dr. rer. nat. habil. Werner Neundorf

Institut fur Mathematik

der Fakultat fur Mathematik und Naturwissenschaften

der Technischen Universitat Ilmenau

Ilmenau 2002

Datei: maplemws.ps, *.pdf

Page 2: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

Anhang A

Anhang B

Page 3: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

Anhang C

Arbeitsblatter in Matlab und

Maple

C.1 Matlab

Zunachst mochte ich auf die Preprints

[1] Neundorf, W.: MATLAB - Teil I: - Vektoren, Matrizen, lineare Gleichungs-

systeme. Preprint M 20/99 IfMath der TU Ilmenau, Juli 1999. [2] Neundorf, W.:

MATLAB - Teil II: - Speicheraspekte, spezielle LGS, SDV, EWP, Graphik, NLG,

NLGS. Preprint M 23/99 TUI, September 1999.

[3] Neundorf, W.: MATLAB - Teil III: - Komplexe LGS, Interpolation, Splines.Preprint M 10/00 IfMath der TU Ilmenau, Mai 2000.

[4] Neundorf, W.: MATLAB - Teil IV: - Approximation, Numerische Intergrati-

on. Preprint M 11/00 IfMath der TU Ilmenau, Mai 2000.

zu den folgenden Gebieten der numerischen Mathematik verweisen.

- Vektoren, Matrizen,- LGS, spezielle LGS, komplexe LGS, EWP, SVD,- Interpolation, Splines,- Approximation, numerische Intergration.

Dazu findet der Leser zahlreiche Matlab m-Files sowie diverse Daten- und Ergebnis-files *.m, *.ps, *.eps, *.mat auf der personlichen Homepage im Internet.Dort sind auch die Postscript-Files primer35.ps, matlab.ps der 2. bzw 3. Edi-tion des Matlab Primers.

Homepage

http://imath.mathematik.tu-ilmenau.de/∼neundorf/index de.html

Navigator → Publications → Computeralgebra → MATLAB1..4

Page 4: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

4 Arbeitsblatter in Matlab und Maple

Zusammenstellung von Adressen - World Wide Web mit Matlab Seiten

Die TEX Quelle sowie das Postscript-File primer35.ps der 2. Edition des MatlabPrimers stehen auch mittels ftp auf ftp.math.ufl.edu im Verzeichnis pub/matlabzur Verfugung.

Die MathWorks Inc. und MathTools Ltd. entwickeln und vertreiben die Compu-tersoftware Matlab und andere Komponenten und sind naturlich mit ihrem ganzenAngebot auch im Internet zu finden.

http://www.mathworks.com/ The MathWorks Inc. Home(auch Simulink)

http://www.mathworks.com/products/matlab/ Matlab 6.1, Description,Function List, Demos u. a.

http://www.mathworks.com/products/compiler/ Matlab Compilerhttp://www.mathworks.com/support/books/ Matlab based books

http://www.mathtools.net/ The techmical computing portalhttp://www.mathtools.com/ Matlab Toolboxen von MathTools Ltd.

(auch MATCOM, MIDEVA)

http://krum.rz.uni-mannheim.de/cafgbench.html Computer Algebra Benchmarks(RZ/Uni Mannheim)

C.2 Maple

Auch hier ein Verweis auf die Preprints

[1] Neundorf, W.: Programming in Maple V Release 5. Extended Basics. PreprintM 07/99 IfMath der TU Ilmenau, Februar 1999.

[2] Neundorf, W. und B. Walther: Grafik, Animation und Ausgabeformate in

Maple V Release 5. Preprint M 12/00 IfMath der TU Ilmenau, Juni 2000.

die wie kleine Maple-Nutzerhandbucher zu den folgenden Gebieten untersetzt mitvielen Beispielen genutzt werden konnen.

- Formeln, Ausdrucke und Anweisungen,- Datenstrukturen,- Prozeduren, Programmierung,- Daten- und Programmeingabe sowie Ausgabe,- Grafik, Animation und Ausgabeformate.

Dazu findet der Leser zahlreiche Maple mws-Arbeitsblatter, m-Files (Prozeduren)sowie Daten- und Ergebnisfiles *.m, *.txt, *.c, *.res, *.pcx, *.ps, *.eps,

*.tif, *.gif auf der personlichen Homepage im Internet.

Homepage

http://imath.mathematik.tu-ilmenau.de/∼neundorf/index de.html

Navigator → Publications → Computeralgebra → MAPLE1..2

Page 5: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.2 Maple 5

Zusammenstellung von Adressen - World Wide Web mit Maple Seiten

http://ist.uwaterloo.ca/ew/software/scientific/ Waterloo Maple’s,University of Waterloo Ontario Canada

http://www.maplesoft.com Maple Home Page (Waterloo Maple Inc.Advancing Mathematics): Products, publi-cations, sales, support, services, company

http://www.mapleapps.com/ The Maple Application Centerhttp://www.mapleapps.com/categories/ Maple Application Center,

whatsnew/html/SCCCmapletutorial.shtml Tutorials and Traininghttp://www.maple4students.com/main.html Maple Student Center,

Tutorialshttp://www.scientific.de/maple/maple.html Deutsche Maple Home Page

http://SunSite.informatik.rwth-aachen.de/ Maple an der RWTH Aachen bilingualmaple/maplev.html Maple 7, Einfuhrung in Maple V

Geschichte, Mandelbrotmenge u. a.http://www.mthcsc.wfu.edu/docs/ Maple Tutorial, Wake Forest University

software.html

http://www.uni-karlsruhe.de/∼Maple/ Maple Seite der Uni Karlsruhe

http://www.uni-essen.de/hrz/mathe/maple/ Maple Seite der Uni Essen

http://www.minet.uni-jena.de/∼schmitzm/ Mathematik Didaktik mit Maplemaple/index.html an FSU Jena

http://www.ikg.rt.bw.schule.de/fh/ Maple am Isolde-Kurz-Gymnasiummathe.html Reutlingen

http://www.kp.tue.bw.schule.de/maple.html Maple am Kepler-Gymnasium Tubingen

http://saaz.lanl.gov/maple/Maple Home.html A Maple Tutorial from Los Alamos NL(The Cluster Team Pres.)

http://saaz.lanl.gov/maple/ Maple on the WebMaple Page13.html

http://web.mit.edu/afs/athena/software/ Software at MIT Cambridge, MA USA,maple/www/home.html Maple

http://www-math.cc.utexas.edu/math/Maple/ Maple index page, UT Austin

http://www.math.villanova.edu/archives/ Why MAPLE? Department of Math.why maple.htm Sciences, Villanova University PA, USA

http://math.unm.edu/∼wester/cas review.html M. Wester Hompage Albuquerque USA,A Critic of the Math. Abilities of CAS

http://www.SymbolicNet.org/ Symbolic Math. Computation InformationCenter, Kent State University USA

ftp://ftp.unibe.ch/pub/Maple/algcurve/ AlgCurve Package maintainedat University Bern CH

ftp://galois.maths.qmw.ac.uk/pub/mapledoc Maple Introduction

ftp://ftp.hprc.utoronto.ca/pub/ednet/maths/ Experiments using Maplemaple/readme.html

Page 6: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

6 Arbeitsblatter in Matlab und Maple

C.3 Kap.4 - Eigenwertproblem und Singularwert-

zerlegung

Konvergenzverbesserung der simultanen Vektoriteration durch Verschie-

bung und Aufspreizung des Spektrums

Nach dem 1. Schritt der Spektralverschiebung werden wir die dominanten EW durchein Aufspreizen mehr auseinanderziehen und somit im weiteren die Konvergenzrateverbessern. Dazu verwenden wir die Tschebyscheff-Polynome Tn(x) nicht wie gewohntim Intervall [−1, 1], sondern außerhalb des Intervalls. Fur x > 1 steigen die Polynomemit wachsendem Grad immer steiler an.

Darstellung der ersten Tschebyscheff-Polynome und zugehorigen gespreizten Eigen-werte mittels Maple.

Datei: nm4ip1.mws

Spektralverschiebung und Spreizung mit Tschebyscheff-Polynomen

> restart:

> with(plots):

> with(plottools):

> with(orthopoly); # orthopoly package

T(n,x) generates the nth Chebyshev polynomial of the first kind.

> # Arbeitsverzeichnis

> pfad := ‘D:/Neundorf/Nwptexte/Math93/‘;

> # Graphic output files

> name1 := ‘nm4ip1.ps‘;

> name2 := ‘nm4ip2.ps‘;

> name3 := ‘nm4ip3.ps‘;

> datei1 := cat(pfad,name1);

> datei2 := cat(pfad,name2);

> datei3 := cat(pfad,name3);

Suppose we want to manipulate symbolically the Chebyshev polynomials

of the first kind Tn(x). We can represent these in Maple as T(n,x).

Recall that Tn(x) satisfies the linear recurrence T0(x)=1, T1(x)=x,

and Tn(x) = 2 x T{n-1}(x) - T{n-2}(x).

Tschebyscheff-Polynome

> for i from 0 to 5 do

> T(i,x);

> od;

Page 7: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.3 Kap.4 - Eigenwertproblem und Singularwertzerlegung 7

> for i from 0 to 5 do

> p[i]:=plot(T(i,x),x=0..2,color=black):

> od:

> t0 := textplot([[1.9,14,‘T2(x)‘],

> [1.9,34,‘T3(x)‘],[1.9,95,‘T4(x)‘],[1.9,330,‘T5(x)‘]]):

> plots[display]([seq(p[i],i=0..5),t0], tickmarks=[4,4], thickness=2,

> title=‘Tschebyscheff-Polynome Ti(x), 0<=x<=2, i=0..5‘);

> interface(plotdevice=ps,

> plotoutput=datei1,

> plotoptions=‘width=640,height=480‘);

> plots[display]([seq(p[i],i=0..5),t0], tickmarks=[4,4], thickness=2,

> title=‘Tschebyscheff-Polynome Ti(x), 0<=x<=2, i=0..5‘);

> interface(plotdevice=win);

EW lambda(i)

> s:=1:

> la1:= la -> la-s:

> la2:= la -> (la-s)^2-s^2/2:

> la3:= la -> (la-s)^3 -3/4*(la-s)*s^2:

> p1:=plot(la1(la),la=0..3,color=black):

> p2:=plot(la2(la),la=0..3,color=black):

> p3:=plot(la3(la),la=0..3,color=black):

> t1 := textplot([[2.8,1.5,‘lambda(1)‘],[2.8,2.8,‘lambda(2)‘],

> [2.8,6,‘lambda(3)‘]]):

> plots[display]([p1,p2,p3,t1], tickmarks=[4,8], thickness=2,

> title=‘lambda(i)(la), 0<=la<=3, i=1,2,3‘);

> interface(plotdevice=ps,

> plotoutput=datei2,

> plotoptions=‘width=640,height=480‘);

> plots[display]([p1,p2,p3,t1], tickmarks=[4,8], thickness=2,

> title=‘lambda(i)(la), 0<=la<=3, i=1,2,3‘);

> interface(plotdevice=win);

EW 2^(i-1)*lambda(i)/s^i

> las1:= r -> r-1:

> las2:= r -> 2*(r-1)^2-1:

> las3:= r -> 4*(r-1)^3 -3*(r-1):

> ps1:=plot(las1(r),r=0..2.5,color=black):

> ps2:=plot(las2(r),r=0..2.5,color=black):

> ps3:=plot(las3(r),r=0..2.5,color=black):

> ps4:=plot([[2,0],[2,1]],color=black):

Page 8: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

8 Arbeitsblatter in Matlab und Maple

> t2 := textplot([[2.35,1.0,‘lambdas(1)‘],

> [2.35,2.8,‘lambdas(2)‘],[2.2,6,‘lambdas(3)‘]]):

> plots[display]([ps1,ps2,ps3,ps4,t2], tickmarks=[4,8], thickness=2,

> title=‘lambdas(i)(r), 0<=r<=2.5, i=1,2,3‘);

> interface(plotdevice=ps,

> plotoutput=datei3,

> plotoptions=‘width=640,height=480‘);

> plots[display]([ps1,ps2,ps3,ps4,t2], tickmarks=[4,8],

> thickness=2,

> title=‘lambdas(i)(r), 0<=r<=2.5, i=1,2,3‘);

> interface(plotdevice=win);

QR-Algorithmus

Unter Verwendung des Maple-Kommandos QRdecomp fur die QR-Faktorisierungwerden die Diagonalelemente von R positiv skaliert. Bei fortschreitender Iterationwerden die symbolischen Rechnungen immer aufwendiger und “langer“, damit lang-samer. In nachfolgenden Beispielen wird der QR-Algorithmus mit der Householder-Transformation durchgefuhrt, nur bei Beispiel 1 sind beide Varianten zum Vergleichangegeben.Zu den EWP werden die entsprechende Informationen ausgegeben wie det(A), EW,EV, charakteristisches Polynom, teilweise auch Zwischenergebnisse des QR-Algorith-mus.

Datei: nm4ip2.mws

QR-Algorithmus

> restart:

> with(linalg):

1.Beispiel

Matrix und EW, EV

> n:=4:

> A := matrix(n,n,[[ 1,-1, 1, 2],

> [-1, 2, 0, 1],

> [ 1, 0,-1, 1],

> [ 2, 1, 1, 0]]):

> det(A);

> vecs:=’vecs’:

> lambda := evalf(Eigenvals(A,vecs));

> print(vecs);

> E:=diag(1,1,1,1):

> P4:=sort(det(A-x*E),x);

Page 9: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.3 Kap.4 - Eigenwertproblem und Singularwertzerlegung 9

QR-Transformation

> R := QRdecomp(A, Q=’q’, rank=’r’);

> r;

> evalm(q);

> evalm(R &* q);

> evalf(evalm(R &* q));

Mehrere Schritte der QR-Transformation

> AA:=A;

> # For symbolic computation the low speed Gram-Schmidt process is

> # applied.

> for k from 1 to 40 do # 40 Iterationen

> R := QRdecomp(AA, Q=’q’, rank=’r’):

> AA:=R&*q:

> # print(evalf(evalm(R)));

> # print(evalf(evalm(q)));

> # print(evalf(evalm(AA)));

> od:

> R:=evalf(evalm(R));

> Q:=evalf(evalm(q));

> DD:=evalf(evalm(AA)); # Diagonalform

> x1:=DD[1,1];

> x2:=DD[2,2];

> x3:=DD[3,3];

> x4:=DD[4,4];

> x^2-x*(DD[2,2]+DD[3,3])+DD[2,2]*DD[3,3]-DD[2,3]^2;

> sol:=solve(x^2-x*(DD[2,2]+DD[3,3])+DD[2,2]*DD[3,3]-DD[2,3]^2,x);

> xx2:=sol[2];

> xx3:=sol[1];

> AA:=A; AA[1,1]:=1.0:

> # For matricies of floating point entries,

> # the high speed numerically stable

> # Householder transformations are used.

> for k from 1 to 40 do # 40 Iterationen

> R := QRdecomp(AA, Q=’q’, rank=’r’):

> AA:=R&*q:

> # print(evalf(evalm(R)));

> # print(evalf(evalm(q)));

Page 10: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

10 Arbeitsblatter in Matlab und Maple

> # print(evalf(evalm(AA)));

> od:

> R:=evalf(evalm(R));

> Q:=evalf(evalm(q));

> DD:=evalf(evalm(AA)); # Diagonalform

> x1:=DD[1,1];

> x2:=DD[2,2];

> x3:=DD[3,3];

> x4:=DD[4,4];

> x^2-x*(DD[2,2]+DD[3,3])+DD[2,2]*DD[3,3]-DD[2,3]^2;

> sol:=solve(x^2-x*(DD[2,2]+DD[3,3])+DD[2,2]*DD[3,3]-DD[2,3]^2,x);

> xx2:=sol[2];

> xx3:=sol[1];

2.Beispiel

Matrix und EW, EV

> n:=4:

> A := matrix(n,n,[[ 1,-1, 2, 2],

> [ 1, 1, 3, 4],

> [-2,-3, 1,-1],

> [-2,-4, 1, 1]]):

> det(A);

> vecs:=’vecs’:

> lambda := evalf(Eigenvals(A,vecs));

> print(vecs);

> E:=diag(1,1,1,1):

> P4:=sort(det(A-x*E),x);

QR-Transformation

> R := QRdecomp(A, Q=’q’, rank=’r’);

> r;

> evalm(q); evalm(R &* q);

> evalf(evalm(R &* q));

Mehrere Schritte der QR-Transformation

> AA:=A; AA[1,1]:=1.0:

> for k from 1 to 30 do # 30 Iterationen

> R := QRdecomp(AA, Q=’q’, rank=’r’):

> AA:=R&*q;

> od:

> R:=evalf(evalm(R));

Page 11: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.3 Kap.4 - Eigenwertproblem und Singularwertzerlegung 11

> Q:=evalf(evalm(q));

> DD:=evalf(evalm(AA)); # Blockdiagonalform

> x^2-x*(DD[1,1]+DD[2,2])+DD[1,1]*DD[2,2]-DD[1,2]*DD[2,1];

> sol12:=solve(x^2-x*(DD[1,1]+DD[2,2])+DD[1,1]*DD[2,2]

> -DD[1,2]*DD[2,1],x);

> x1:=sol12[1];

> x2:=sol12[2];

> x^2-x*(DD[3,3]+DD[4,4])+DD[3,3]*DD[4,4]-DD[3,4]*DD[4,3];

> sol34:=solve(x^2-x*(DD[3,3]+DD[4,4])+DD[3,3]*DD[4,4]

> -DD[3,4]*DD[4,3],x);

> x3:=sol34[1];

> x4:=sol34[2];

3.Beispiel

Matrix und EW, EV

> n:=4:

> A := matrix(n,n,[[ 2,-1, 0, 0],

> [-1, 2,-1, 0],

> [ 0,-1, 2,-1],

> [ 0, 0,-1, 2]]):

> det(A);

> vecs:=’vecs’:

> lambda := evalf(Eigenvals(A,vecs));

> print(vecs);

> E:=diag(1,1,1,1):

> P4:=sort(det(A-x*E),x);

QR-Transformation

> R := QRdecomp(A, Q=’q’, rank=’r’);

> r;

> evalm(q);

> evalm(R &* q);

> evalf(evalm(R &* q));

Mehrere Schritte der QR-Transformation

> AA:=A; AA[1,1]:=2.0:

> for k from 1 to 30 do # 30 Iterationen

> R := QRdecomp(AA, Q=’q’, rank=’r’):

> AA:=R&*q;

> od:

> R:=evalf(evalm(R));

> Q:=evalf(evalm(q));

Page 12: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

12 Arbeitsblatter in Matlab und Maple

> DD:=evalf(evalm(AA)); # Diagonalform

> x1:=DD[1,1];

> x2:=DD[2,2];

> x3:=DD[3,3];

> x4:=DD[4,4];

QR-Algorithmus als Zweistufen-Verfahren

Im Vorbereitungsschritt verwenden wir die Householder-Reflexion. Damit transfor-miert man die Matrix A = AT zunachst auf die Tridiagonalform.Im darauffolgenden Iterationsschritt wird die symmetrische Tridiagonalmatrix A′

durch die QR-Transformation auf Diagonalform gebracht.

Datei: nm4ip3.mws

Householder-Reflexion auf Tridiagonalform und QR-Algorithmus

> restart;

> with(linalg):

Matrix und EW, EV

> n:=4:

> A := matrix(n,n,[[1,-1,1,2],[-1,2,0,1],[1,0,-1,1],[2,1,1,0]]):

> det(A);

> vecs:=’vecs’:

> lambda := evalf(Eigenvals(A,vecs));

> print(vecs);

> E:=diag(1,1,1,1):

> P4:=sort(det(A-x*E),x);

Ueberfuehrung einer Matrix auf Tridiagonalform

mittels Householder-Reflexionen

1.Schritt

> y1:=submatrix(A,2..4,1..1);

> transpose(y1);

> y:=vector(3,[seq(y1[i,1],i=1..3)]);

> ny:=norm(y,2);

> if evalf(y[1])<0 then alpha:=ny else alpha:=-ny fi;

> e:=[1,0,0];

> v:=evalm(y-alpha*e);

> vn2:=norm(v,2)^2;

> v1:=matrix(3,1,v);

> dyad:= evalm(v1&*transpose(v1));

> Q1:=evalm(diag(1,1,1)-2/vn2*dyad); # Elementarmatrix

Page 13: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.3 Kap.4 - Eigenwertproblem und Singularwertzerlegung 13

Kontrolle

> E1:=evalm(Q1 &* transpose(Q1)): # nicht vereinfacht

> E1:=simplify(evalm(Q1 &* transpose(Q1))); # numerisch gerechnet

> E1:=evalf(evalm(Q1 &* transpose(Q1))); # Einheitsmatrix

> evalf(E1[1,1]); # simplify hilft nicht

> evalf(E1[2,2]);

> evalf(E1[3,3]);

> row(Q1,1);

> seq(Q1[1,k],k=1..3);

> P1:=matrix(4,4,[[1,0,0,0],

> [0,seq(Q1[1,k],k=1..3)],

> [0,seq(Q1[2,k],k=1..3)],

> [0,seq(Q1[3,k],k=1..3)]]);

> As:=simplify(evalm(P1&*A));

> A1:=simplify(evalm(As&*transpose(P1)));

> A11:=matrix(4,4,(i,j) -> evalf(A1[i,j]));

2.Schritt

> y1:=evalf(submatrix(A1,3..4,2..2));

> transpose(y1);

> y:=vector(2,[seq(y1[i,1],i=1..2)]);

> ny:=simplify(norm(y,2));

> if evalf(y[1])<0 then alpha:=ny else alpha:=-ny fi;

> e:=[1,0];

> v:=evalm(y-alpha*e);

> vn2:=norm(v,2)^2;

> v1:=matrix(2,1,v);

> dyad:= evalm(v1&*transpose(v1));

> Q1:=evalm(diag(1,1)-2/vn2*dyad);

> E1:=evalm(Q1 &* transpose(Q1)): # nicht vereinfacht

> E1:=simplify(evalm(Q1 &* transpose(Q1))); # Einheitsmatrix

> evalf(E1[1,1]);

> evalf(E1[2,2]);

> row(Q1,1);

> seq(Q1[1,k],k=1..2);

> P2:=matrix(4,4,[[1,0,0,0],[0,1,0,0],

> [0,0,seq(Q1[1,k],k=1..2)],

> [0,0,seq(Q1[2,k],k=1..2)]]);

> As:=simplify(evalm(P2&*A1));

Page 14: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

14 Arbeitsblatter in Matlab und Maple

> A2:=simplify(evalm(As&*transpose(P2)));

> A21:=matrix(4,4,(i,j) -> evalf(A2[i,j])); # Tridiagonalform

Charakteristisches Polynom von Tridiagonalmatrix und EW

> AA:=A21;

> va:=vector(4,i->AA[i,i]);

> vb:=vector(3,i->AA[i+1,i]);

> vc:=vector(3,i->AA[i,i+1]);

3-Term-Rekursion

> ‘expand/pn‘ := proc(n,x) option remember;

> global va,vb,vc;

> if n = 0 then 1

> elif n = 1 then va[1]-x

> elif not type(n,integer) then pn(n,x) # can’t do anything

> else expand((va[n]-x)*pn(n-1,x) - vb[n-1]*vc[n-1]*pn(n-2,x))

> fi

> end:

> pn(4,x);

> pne:=expand(pn(4,x));

> f:=x->pne;

> solve(f(x),x);

QR-Algorithmus fuer Tridiagonalmatrix

> R := QRdecomp(AA, Q=’q’, rank=’r’);

> r;

> evalm(q);

> evalm(R &* q);

Mehrere Schritte des QR-Algorithmus

> AAA:=AA:

> for k from 1 to 100 do # 100 Iterationen

> R := QRdecomp(AAA, Q=’q’, rank=’r’):

> AAA:=R&*q;

> od:

> R:=evalm(R);

> Q:=evalm(q);

> DD:=evalm(AAA); # Diagonalform

> x1:=DD[1,1];

> x2:=DD[2,2];

Page 15: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.3 Kap.4 - Eigenwertproblem und Singularwertzerlegung 15

> x3:=DD[3,3];

> x4:=DD[4,4];

EWP und SVD

Datei: nm4ip4.mws

SVD

> restart;

> with(linalg):

Matrix und EW, EV

> n:=4:

> A := matrix(n,n,[[1,-1,1,2],[-1,2,0,1],[1,0,-1,1],[2,1,1,0]]):

> det(A);

> vecs:=’vecs’:

> lambda := evalf(Eigenvals(A,vecs));

> print(vecs);

> E:=diag(1,1,1,1):

> P4:=sort(det(A-x*E),x);

> A2:=evalm(transpose(A) &* A);

> vecs1:=’vecs1’:

> lambda1 := evalf(Eigenvals(A2,vecs1));

> print(vecs1);

> sigma := [seq(sqrt(lambda1[i]),i=1..4)];

SVD

Beispiel A

> sv:=Svd(A,U,V);

> evalf(sv);

> evalm(U);

> evalm(V);

Beispiel B

> s:=0.01;

> B:=matrix(3,2,[[1,1],[s,0],[0,s]]);

> sv:=Svd(B,U,V);

> evalf(sv);

> evalm(U);

> evalm(V);

Page 16: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

16 Arbeitsblatter in Matlab und Maple

Beispiel C

> s:=100;

> C:=matrix(2,2,[[1,s],[0,1]]);

> sv:=Svd(C,U,V);

> evalf(sv);

> evalm(U);

> evalm(V);

C.4 Kap.6 - Polynome, Interpolation, Splines und

Differentiation

Lagrange-Interpolation, Basispolynome, Kondition der Interpolation

Datei: nm6ip1.mws

> restart:

> with(plots):

> with(plottools):

> # Arbeitsverzeichnis

> pfad := ‘D:/Neundorf/Nwptexte/Math93/‘;

> # Graphic output files

> name1 := ‘nm6ip01.ps‘;

> name2 := ‘nm6ip02.ps‘;

> name3 := ‘nm6ip03.ps‘;

> name4 := ‘nm6ip04.ps‘;

> datei1 := cat(pfad,name1);

> datei2 := cat(pfad,name2);

> datei3 := cat(pfad,name3);

> datei4 := cat(pfad,name4);

Lagrange-Interpolation, Referenz

> n := 3;

> xv := [-2, 1, 2, 4];

> yv := [ 3, 1,-3, 8];

Lagrange-Polynome, Basispolynome

> phi0 := x-> (x-xv[2])*(x-xv[3])*(x-xv[4])/

> ((xv[1]-xv[2])*(xv[1]-xv[3])*(xv[1]-xv[4]));

> simplify(phi0(x));

> phi1 := x-> (x-xv[1])*(x-xv[3])*(x-xv[4])/

> ((xv[2]-xv[1])*(xv[2]-xv[3])*(xv[2]-xv[4]));

Page 17: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.4 Kap.6 - Polynome, Interpolation, Splines und Differentiation 17

> simplify(phi1(x));

> phi2 := x-> (x-xv[1])*(x-xv[2])*(x-xv[4])/

> ((xv[3]-xv[1])*(xv[3]-xv[2])*(xv[3]-xv[4]));

> simplify(phi2(x));

> phi3 := x-> (x-xv[1])*(x-xv[2])*(x-xv[3])/

> ((xv[4]-xv[1])*(xv[4]-xv[2])*(xv[4]-xv[3]));

> simplify(phi3(x));

> Q := plot(phi0,-2..4,title=‘phi0(x)‘,axes=normal):

> plots[display](Q);

> interface(plotdevice=ps,

> plotoutput=datei1,

> plotoptions=‘width=640,height=480‘);

> plots[display](Q);

> interface(plotdevice=win);

> a:=array(1..2,1..2):

> a[1,1]:=plot(phi0,-2..4,title=‘phi0(x)‘,color=black):

> a[1,2]:=plot(phi1,-2..4,title=‘phi1(x)‘,color=black):

> a[2,1]:=plot(phi2,-2..4,title=‘phi2(x)‘,color=black):

> a[2,2]:=plot(phi3,-2..4,title=‘phi3(x)‘,color=black):

> Q := display(a,scaling=constrained,xtickmarks=6,ytickmarks=3):

> plots[display](Q);

> interface(plotdevice=ps,

> plotoutput=datei2,

> plotoptions=‘width=640,height=480‘);

> plots[display](Q);

> interface(plotdevice=win);

Lagrange-Interpolationspolynom

> L3 := x-> yv[1]*phi0(x)+yv[2]*phi1(x)+

> yv[3]*phi2(x)+yv[4]*phi3(x);

> simplify(L3(x));

> pl1 := plot(L3,-2..4,ytickmarks=8,title=‘Lagrange-IP L3(x)‘):

> punkte := [seq([xv[i],yv[i]],i=1..4)]:

> pl2 := polygon(punkte,style=point,symbol=circle):

> plots[display](pl1,pl2);

> interface(plotdevice=ps,

> plotoutput=datei3,

> plotoptions=‘width=640,height=480‘);

> plots[display](pl1,pl2);

> interface(plotdevice=win);

Page 18: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

18 Arbeitsblatter in Matlab und Maple

Legesgue-Konstante, Kondition der Interpolation

> AbsL3 := x-> abs(phi0(x))+abs(phi1(x))+abs(phi2(x))+abs(phi3(x));

> simplify(AbsL3(x));

> seq([-0.73+i/100,AbsL3(-0.73+i/100)],i=-1..1);

> Kabs := 3.598070750;

> pl3 := plot([1,AbsL3],-2..4,0..4, title=‘Kondition, AbsL3(x)‘):

> pl4 := plot([[-0.73,0],[-0.73,3.6]]):

> punkte := [seq([xv[i],1],i=1..4),[-0.73,3.6]]:

> pl5 := polygon(punkte,style=point,symbol=circle):

> plots[display](pl3,pl4,pl5);

> interface(plotdevice=ps,

> plotoutput=datei4,

> plotoptions=‘width=640,height=480‘);

> plots[display](pl3,pl4,pl5);

> interface(plotdevice=win);

----------------------------------------------------------------------

Datei: nm6ip2.mws

Prozedur zur Erzeugung des Interpolationspolynoms in verschiedenen

Darstellungen:

Newtonsche, Lagrangesche, baryzentrische Darstellung

> restart:

> interpol:=proc(stst::list,stw::list,var::name,typ::string)

> description ‘Polynominterpolation in verschiedenen Darstellungen‘;

> local i,k,n,omega,rho,ityp,x;

> n:=nops(stst);

> if nargs=3 then ityp:="Normal" else ityp:=typ; fi;

> if ityp="Lagrange" then

> omega:=[1];

> for k from 1 to n-1 do

> for i from 1 to nops(omega) do

> omega:=subsop(i=omega[i]/(stst[i]-stst[k+1]),omega);

> od;

> omega:=[op(omega),-sum(omega[j],j=1..nops(omega))];

> od;

> i:=’i’: k:=’k’:

> [sum(omega[i]/(var-stst[i])*stw[i],i=1..n)/

> sum(omega[i]/(var-stst[i]),i=1..n),

> sum(stw[i]*omega[i]*product(var-stst[k],k=1..i-1)*

> product(var-stst[k],k=i+1..n),i=1..n)];

> elif

Page 19: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.4 Kap.6 - Polynome, Interpolation, Splines und Differentiation 19

> ityp="Newton" then

> omega:=stw;

> for k from 0 to n-1 do rho:=stw[k+1];

> for i from 0 to k-1 do

> rho:=(rho-omega[i+1])/(stst[k+1]-stst[i+1]);

> od;

> omega:=subsop(k+1=rho,omega);

> od;

> i:=’i’: k:=’k’:

> sum(omega[k]*product(var-stst[i],i=1..k-1),k=1..n);

> else

> interp(stst,stw,var);

> fi;

> end:

Aufruf:

Anzahl der Parameter: 3 oder 4

Bedeutung:

1. Parameter: Stuetzstellen (Liste)

2. Parameter: Stuetzwerte (Liste)

3. Parameter: Name der zu verwendenden Variablen

4. Parameter: Newton oder Lagrange; wenn dieser Parameter fehlt, wird

Maples interne Interpolationsroutine interp(stst,stw,var) verwendet.

Besonderheiten:

Das Lagrangesche Interpolationspolynom kann auf zwei verschiedene

Arten ausgegeben werden; dies wird durch einen zusaetzlichen

Index [1] bzw. [2] erreicht.

Beispiel:

Referenz

> n := 3;

> xv := [1, 2, 5, 6];

> yv := [2, 4, 1, 1];

> interpol(xv,yv,x,"Newton");

> N3 := 2+2*(x-1)-3/4*(x-1)*(x-2)+1/5*(x-1)*(x-2)*(x-5);

> interpol(xv,yv,x,"Lagrange")[2]; # die konventionelle Darstellung

> interpol(xv,yv,x,"Lagrange")[1]; # die baryzentrische Darstellung

> interpol(xv,yv,x);

> interp(xv,yv,x); # Standardfunktion

Page 20: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

20 Arbeitsblatter in Matlab und Maple

Datei: nm6ip3.mws

Kubische Hermite-Basis

> restart:

> with(plots):

> with(plottools):

> # Arbeitsverzeichnis

> pfad := ‘D:/Neundorf/Nwptexte/Math93/‘;

> # Graphic output files

> name1 := ‘nm6ip05.ps‘;

> name2 := ‘nm6ip06.ps‘;

> datei1 := cat(pfad,name1);

> datei2 := cat(pfad,name2);

Kubische Hermite-Basis

Referenz

> n := 3;

> xv := [0, 0, 1, 1];

> yv0 := [1, 0, 0, 0];

> yv1 := [0, 1, 0, 0];

> yv2 := [0, 0, 1, 0];

> yv3 := [0, 0, 0, 1];

> d := [0, 1, 0, 1];

Basispolynome

> H30 := x-> 2*x^3-3*x^2+1;

> H31 := x-> x^3-2*x^2+x;

> H32 := x-> -2*x^3+3*x^2;

> H33 := x-> x^3-x^2;

> Q := plot(H30,-1..2,title=‘Hermite-Basispolynom H30(x)‘,

> axes=normal,ytickmarks=6):

> plots[display](Q);

> interface(plotdevice=ps,

> plotoutput=datei1,

> plotoptions=‘width=640,height=480‘);

> plots[display](Q);

> interface(plotdevice=win);

> pl0:=plot(H30,0..1,color=black):

> pl1:=plot(H31,0..1,color=black):

> pl2:=plot(H32,0..1,color=black):

Page 21: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.4 Kap.6 - Polynome, Interpolation, Splines und Differentiation 21

> pl3:=plot(H33,0..1,color=black):

> pl :=plot([[1,0],[1,1]]):

> t0 := textplot([0.25,0.7,‘H30(x)‘]):

> t1 := textplot([0.5,0.17,‘H31(x)‘]):

> t2 := textplot([0.75,0.7,‘H32(x)‘]):

> t3 := textplot([0.4,-0.15,‘H33(x)‘]):

> Q:=display([pl0,pl1,pl2,pl3,pl,t0,t1,t2,t3],

> scaling=constrained,xtickmarks=6,ytickmarks=7,

> titlefont=[TIMES,BOLD,12],

> title=‘Kubische Hermite-Basis H3i(x) auf [0,1]‘):

> plots[display](Q);

> interface(plotdevice=ps,

> plotoutput=datei2,

> plotoptions=‘width=640,height=480‘);

> plots[display](Q);

> interface(plotdevice=win);

-------------------------------------------------------------------

Datei: nm6ip4.mws

Rungesche Effekt bei der Interpolation der nichtglatten Funktion

f(x)=|x|, -1<=x<=1, mit aequidistanten Stuetzstellen

> restart:

> with(plots):

> with(plottools):

> # Arbeitsverzeichnis

> pfad := ‘D:/Neundorf/Nwptexte/Math93/‘;

> # Graphic output files

> name1 := ‘nm6ip07.ps‘;

> name2 := ‘nm6ip08.ps‘;

> datei1 := cat(pfad,name1);

> datei2 := cat(pfad,name2);

Referenz

> n := 2;

> xv := [-1, 0, 1];

> yv := [ 1, 0, 1];

> f := x-> abs(x);

> interp(xv,yv,x);

Page 22: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

22 Arbeitsblatter in Matlab und Maple

Interpolationspolynome vom Grade 2,4,8,10

> p2 := x->x^2;

> p4 := x->-4/3*x^4+7/3*x^2;

> p8 := x->-1024/63*x^8+1408/45*x^6-172/9*x^4+533/105*x^2;

> p10 := x->390625/5184*x^10-1015625/6048*x^8+

> 221875/1728*x^6-13375/324*x^4+1627/252*x^2;

> Q:=plot([p8,f],-1..1,0..1.5,scaling=constrained,

> xtickmarks=4,ytickmarks=4,

> title=‘f(x)=|x| und p8(x)‘,color=black):

> plots[display](Q);

> interface(plotdevice=ps,

> plotoutput=datei1,

> plotoptions=‘width=640,height=480‘);

> plots[display](Q);

> interface(plotdevice=win);

> a:=array(1..2,1..2):

> a[1,1]:=plot([ p2,f],-1..1,0..1.5, title=‘p2(x)‘,color=black):

> a[1,2]:=plot([ p4,f],-1..1,0..1.5, title=‘p4(x)‘,color=black):

> a[2,1]:=plot([ p8,f],-1..1,0..1.5, title=‘p8(x)‘,color=black):

> a[2,2]:=plot([p10,f],-1..1,0..1.5, title=‘p10(x)‘,color=black):

> Q := display(a,scaling=unconstrained,xtickmarks=2,ytickmarks=2):

> plots[display](Q);

> interface(plotdevice=ps,

> plotoutput=datei2,

> plotoptions=‘width=640,height=480‘);

> plots[display](Q);

> interface(plotdevice=win);

---------------------------------------------------------------------

Datei: nm6ip5.mws

Knotenpunktpolynome mit aequidistanten und Tschebyscheff-Knoten im

Vergleich

> restart:

> with(plots):

> with(plottools):

> # Arbeitsverzeichnis

> pfad := ‘D:/Neundorf/Nwptexte/Math93/‘;

> # Graphic output files

> name1 := ‘nm6ip09.ps‘;

> name2 := ‘nm6ip10.ps‘;

Page 23: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.4 Kap.6 - Polynome, Interpolation, Splines und Differentiation 23

> datei1 := cat(pfad,name1);

> datei2 := cat(pfad,name2);

1. Referenz

> n1 := 4;

> xva1 := [seq(-1+2*k/n1,k=0..n1)];

> xvn1 := [seq(-cos((2*k+1)/(2*n1+2)*Pi),k=0..n1)];

> evalf(xvn1);

> oma1 := x-> product(x-xva1[k],k=1..n1+1);

> evalf(oma1(x));

> oma1(1);

> omn1 := x-> product(x-xvn1[k],k=1..n1+1);

> evalf(omn1(x));

> omn1(1);

> evalf(2^n1*omn1(1));

> evalf(2^n1*omn1(-1));

2.Referenz

> n2 := 8;

> xva2 := [seq(-1+2*k/n2,k=0..n2)];

> xvn2 := [seq(-cos((2*k+1)/(2*n2+2)*Pi),k=0..n2)];

> evalf(xvn2);

> oma2 := x-> product(x-xva2[k],k=1..n2+1);

> evalf(oma2(x));

> oma2(1);

> omn2 := x-> product(x-xvn2[k],k=1..n2+1);

> evalf(omn2(x));

> omn2(1);

> evalf(2^n2*omn2(1));

> evalf(2^n2*omn2(-1));

Plot mit aequidistanten Stuetzstellen

> plot(oma1,-1..1,title=‘omega_(5)(x), xi(0..4) aequid.‘,color=black);

> plot(oma2,-1..1,title=‘omega_(9)(x), xi(0..8) aequid.‘,color=black);

> a:=array(1..1,1..2):

> a[1,1]:=plot(oma1,-1..1,

> title=‘omega_(5)(x), xi(0..4)‘,color=black):

> a[1,2]:=plot(oma2,-1..1,

> title=‘omega_(9)(x), xi(0..8) aequid.‘,color=black):

Page 24: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

24 Arbeitsblatter in Matlab und Maple

> Q := display(a,scaling=unconstrained,xtickmarks=2,ytickmarks=4):

> plots[display](Q);

> interface(plotdevice=ps,

> plotoutput=datei1,

> plotoptions=‘width=640,height=480‘);

> plots[display](Q);

> interface(plotdevice=win);

Plot mit Tschebyscheff-Stuetzstellen

> plot(omn1,-1..1,

> title=‘omega_(5)(x), xi(0..4) Tschebyscheff‘,color=black);

> plot(omn2,-1..1,

> title=‘omega_(9)(x), xi(0..8) Tschebyscheff‘,color=black);

> a:=array(1..1,1..2):

> a[1,1]:=plot(omn1,-1..1,

> title=‘omega_(5)(x), xi(0..4)‘,color=black):

> a[1,2]:=plot(omn2,-1..1,

> title=‘omega_(9)(x), xi(0..8) Tscheb.‘,color=black):

> Q := display(a,scaling=unconstrained,xtickmarks=2,ytickmarks=4):

> plots[display](Q);

> interface(plotdevice=ps,

> plotoutput=datei2,

> plotoptions=‘width=640,height=480‘);

> plots[display](Q);

> interface(plotdevice=win);

---------------------------------------------------------------------

Datei: nm6ip6.mws

Trigonometrische Interpolation mit aequidistanten Stuetzstellen (DFT)

und Fourier-Reihenentwicklung (Fourier-Polynome, Approximation im

quadratischen Mittel) fuer Signalfunktion f(x) mit Grafik

> restart:

> with(plots):

> with(plottools):

> # Arbeitsverzeichnis

> pfad := ‘D:/Neundorf/Nwptexte/Math93/‘;

> # Graphic output files

> name1 := ‘nm6ip11.ps‘;

> name2 := ‘nm6ip12.ps‘;

> name3 := ‘nm6ip13.ps‘;

> name4 := ‘nm6ip14.ps‘;

Page 25: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.4 Kap.6 - Polynome, Interpolation, Splines und Differentiation 25

> name5 := ‘nm6ip15.ps‘;

> name6 := ‘nm6ip16.ps‘;

> datei1 := cat(pfad,name1);

> datei2 := cat(pfad,name2);

> datei3 := cat(pfad,name3);

> datei4 := cat(pfad,name4);

> datei5 := cat(pfad,name5);

> datei6 := cat(pfad,name6);

> read ‘d:/neundorf/nwptexte/math93/cfourier.m‘;

> a:=-Pi; b:=Pi; T:=b-a; N:=8;

> u1:=x->piecewise(x=-Pi,0,x<0,-1,x>0,1,0);

> # smartplot

> u1(x);

> smartplot(piecewise(x<0,-1,x>0,1,0));

> readlib(discont):

> discont(u1(x),x);

> plot(u1(x),x=a-1..b+1,-1..1,thickness=3,color=red);

> u2:=x->if x<0 then -1 elif x>0 then 1 else evalf(1/2) fi;

> u2(x);

> u2(0.5);

> u3:=x->piecewise(x<a-T/2,-1,x<a,1,x<0,-1,x<b,1,x<b+T/2,-1,1);

> u3(x);

> plot(u3(x),x=-8..8,y=-1..1,thickness=2,title=‘u3(x)=sign(sin(x))‘);

> f:=x->u1(x-T*floor((x-a)/T));

> f(x);

> Q1:=plot([f(x),sin(x)],x=-8..8,-1..1,thickness=2,

> title=‘f(x)=sign(sin(x))‘):

> Q2:=plot(f(x),x=-8..8,-1.5..1.5,thickness=1):

> plots[display](Q1);

> interface(plotdevice=ps,

> plotoutput=datei1,

> plotoptions=‘width=640,height=480‘);

> plots[display](Q1);

> interface(plotdevice=win);

> xp:=[seq(k*T/N,k=0..N)];

> yp:=[seq(f(k*T/N),k=0..N)];

> ref:=[seq([xp[k],yp[k]],k=1..N+1)];

Page 26: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

26 Arbeitsblatter in Matlab und Maple

> ref0:=[seq([xp[k],0],k=1..N+1)];

> Q3:=plot(ref,style=point,symbol=circle):

> Q30:=plot(ref0,style=point,symbol=circle):

> PhiN:= x -> (1+sqrt(2))/2*sin(x)+(-1+sqrt(2))/2*sin(3*x);

> Q:=plot(PhiN(x),x=-8..8,-1.5..1.5,thickness=1,

> title=‘ f(x)=sign(sin(x)),

Phi8(x)=(1+sqrt(2))/2*sin(x)+(-1+sqrt(2))/2*sin(3*x)‘):

> plots[display]([Q,Q2,Q3]);

> interface(plotdevice=ps,

> plotoutput=datei2,

> plotoptions=‘width=640,height=480‘);

> plots[display]([Q,Q2,Q3]);

> interface(plotdevice=win);

Fourier-Polynom

Approximation im quadratischen Mittel

> cfourier(u1,a..b,N,‘reell‘,‘Plot‘);

> bk:= k -> 2*(1-(-1)^k)/(Pi*k);

> bk(1); bk(2); bk(k);

Fourier-Polynom, explizit

> n:=’n’:

> fs:=(x,n)-> sum(bk(k)*sin(k*x),k=1..n);

> fs(x,n);

> fs(x,4);

> Q:=plot(fs(x,4),x=-8..8,-1.5..1.5,thickness=2,

> title=‘f(x)=sign(sin(x)), 0 fs(x,4)=4/Pi*sin(x)+4/(3Pi)*sin(3*x)‘):

> plots[display]([Q,Q2]);

> interface(plotdevice=ps,

> plotoutput=datei3,

> plotoptions=‘width=640,height=480‘);

> plots[display]([Q,Q2]);

> interface(plotdevice=win);

> P:=array(1..2,1..2,[]):

> n:=2:

> for i from 1 by 1 to 2 do

> for j from 1 by 1 to 2 do

> n:=n+2:

> if i*j=1 then

> P[i,j]:=plot([f(x),fs(x,n)],x=-1..T+1,color=black,

> title=‘f(x) und Fourier-Polynome, n=4,6,8,10‘)

Page 27: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.4 Kap.6 - Polynome, Interpolation, Splines und Differentiation 27

> else

> P[i,j]:=plot([f(x),fs(x,n)],x=-1..T+1,color=black)

> fi:

> od:

> od:

> plots[display](P);

> interface(plotdevice=ps,

> plotoutput=datei4,

> plotoptions=‘width=640,height=480‘);

> plots[display](P);

> interface(plotdevice=win);

> Q:=plot([f(x),PhiN(x),fs(x,4)],x=-1..T+1,color=black,

> title=‘f(x)=sign(sin(x)), Phi8(x), fs(x,4)‘):

> plots[display]([Q,Q3]);

> interface(plotdevice=ps,

> plotoutput=datei5,

> plotoptions=‘width=640,height=480‘);

> plots[display]([Q,Q3]);

> interface(plotdevice=win);

> Q:=plot((PhiN(x)-fs(x,4)),x=-1..T+1,color=black,

> title=‘Fehler Phi8(x)-fs(x,4)‘):

> plots[display]([Q,Q30]);

> interface(plotdevice=ps,

> plotoutput=datei6,

> plotoptions=‘width=640,height=480‘);

> plots[display]([Q,Q30]);

> interface(plotdevice=win);

---------------------------------------------------------------------

Datei: nm6ip7.mws

Kubischer Spline

> restart:

> with(plots):

> with(plottools):

> # Arbeitsverzeichnis

> pfad := ‘D:/Neundorf/Nwptexte/Math93/‘;

> # Graphic output files

> name1 := ‘nm6ip17.ps‘;

> datei1 := cat(pfad,name1);

Page 28: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

28 Arbeitsblatter in Matlab und Maple

Referenz

> n:=2;

> xk:=[0, 1/2, 1];

> yk:=[1, -1/2, 2];

> s:=x->piecewise(x<xk[1],1-5*x, x<xk[2],1-5*x+8*x^3,x<xk[3],

> -1/2+(x-xk[2])+12*(x-xk[2])^2-8*(x-xk[2])^3, 2+7*(x-xk[3]));

> # smartplot

> s(x);

> smartplot(piecewise(x<xk[1],1-5*x,x<xk[2],1-5*x+8*x^3,x<xk[3],

> -1/2+(x-xk[2])+12*(x-xk[2])^2-8*(x-xk[2])^3,2+7*(x-xk[3])));

> plot(s(x),x=xk[1]-0.5..xk[3]+0.5,thickness=1,color=red);

> si1:=seq(s(i/50),i=0..50);

> evalf(si1);

> s(0.216); s(0.217);

> s(0.455); s(0.456); s(0.457);

> s(0.675); s(0.676);

> si2:=seq(s(i/4),i=0..4);

> evalf(si2);

> ref:=[seq([xk[i],yk[i]],i=1..n+1)];

> ref0:=[[0.216,0],[0.456,0],[0.675,0],[0.456,-0.521]];

> Qt:=textplot([[0.2,-0.1,‘xs1‘],[0.45,-0.1,‘xm‘],

> [0.7,-0.1,‘xs2‘]],color=black):

> Q2:=plot(ref,style=point,symbol=circle):

> Q3:=plot([[0.5,-0.5],[0.5,0],[1,0],[1,2]]):

> Q4:=plot(ref0,style=point,symbol=cross):

> Q1:=plot(s(x),x=xk[1]-0.2..xk[3]+0.2,thickness=2,xtickmarks=3,

> color=red,title=‘Kubischer Spline s(x)‘):

> plots[display]([Q1,Q2,Q3,Q4,Qt]);

> interface(plotdevice=ps,

> plotoutput=datei1,

> plotoptions=‘width=640,height=480‘);

> plots[display]([Q1,Q2,Q3,Q4,Qt]);

> interface(plotdevice=win);

Page 29: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.4 Kap.6 - Polynome, Interpolation, Splines und Differentiation 29

Datei: nm6ip8.mws

Basis-Splines (B-Splines) der Ordnung k=2,3,4

Berechnung und Grafik

> restart:

> with(plots):

> with(plottools):

> readlib(bspline): readlib(spline):

> # Arbeitsverzeichnis

> pfad := ‘D:/Neundorf/Nwptexte/Math93/‘;

> # Graphic output files

> name1 := ‘nm6ip18.ps‘;

> name2 := ‘nm6ip19.ps‘;

> name3 := ‘nm6ip20.ps‘;

> datei1 := cat(pfad,name1);

> datei2 := cat(pfad,name2);

> datei3 := cat(pfad,name3);

The functions bspline(d,v) or bspline(d,v,k) compute the segment

polynomials for the B-spline of degree d in the symbol v

on the knot sequence k.

If k is not specified, the uniform knot sequence [0,1,...,d+1] is used.

B-Spline der Ordnung k=2 (Grad k-1=1), "Huetchen"

Unterteilung {0,1,2,3,4}, n=5

> bspline(1,x,[0,0,1]);

> p0:=plot(bspline(1,x,[0,0,1]),x=-1..5,0..1,thickness=2):

> bspline(1,x,[0,1,2]);

> p1:=plot(bspline(1,x,[0,1,2]),x=-1..5,0..1,thickness=2):

> bspline(1,x,[1,2,3]);

> p2:=plot(bspline(1,x,[1,2,3]),x=-1..5,0..1,thickness=2):

> bspline(1,x,[2,3,4]);

> p3:=plot(bspline(1,x,[2,3,4]),x=-1..5,0..1,thickness=2):

> bspline(1,x,[3,4,4]);

> p4:=plot(bspline(1,x,[3,4,4]),x=-1..5,0..1,thickness=2):

> plots[display]([p0,p1,p2,p3,p4],

> title=‘B-Splines N(i,2)(x), i=0,1,2,3,4‘);

Page 30: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

30 Arbeitsblatter in Matlab und Maple

> interface(plotdevice=ps,

> plotoutput=datei1,

> plotoptions=‘width=640,height=480‘);

> plots[display]([p0,p1,p2,p3,p4],

> title=‘B-Splines N(i,2)(x), i=0,1,2,3,4‘);

> interface(plotdevice=win);

B-Spline der Ordnung k=3 (Grad k-1=2), "quadratische Parabel"

Unterteilung {0,1,2,3,4}

> bspline(2,x,[0,0,0,1]);

> pm1:=plot(bspline(2,x,[0,0,0,1]),x=-1..5,0..1,thickness=2):

> bspline(2,x,[0,0,1,2]);

> p0:=plot(bspline(2,x,[0,0,1,2]),x=-1..5,0..1,thickness=2):

> bspline(2,x,[0,1,2,3]);

> p1:=plot(bspline(2,x,[0,1,2,3]),x=-1..5,0..1,thickness=2):

> bspline(2,x,[1,2,3,4]);

> p2:=plot(bspline(2,x,[1,2,3,4]),x=-1..5,0..1,thickness=2):

> bspline(2,x,[2,3,4,4]);

> p3:=plot(bspline(2,x,[2,3,4,4]),x=-1..5,0..1,thickness=2):

> bspline(2,x,[3,4,4,4]);

> p4:=plot(bspline(2,x,[3,4,4,4]),x=-1..5,0..1,thickness=2):

> plots[display]([pm1,p0,p1,p2,p3,p4],

> title=‘B-Splines N(i,3)(x), i=-1,0,1,2,3,4‘);

> interface(plotdevice=ps,

> plotoutput=datei2,

> plotoptions=‘width=640,height=480‘);

> plots[display]([pm1,p0,p1,p2,p3,p4],

> title=‘B-Splines N(i,3)(x), i=-1,0,1,2,3,4‘);

> interface(plotdevice=win);

B-Spline der Ordnung k=4 (Grad k-1=3), "kubische Parabel"

Unterteilung {0,1,2,3,4}, n=5

> bspline(3,x,[0,0,0,0,1]);

> pm2:=plot(bspline(3,x,[0,0,0,0,1]),x=-1..5,0..1,thickness=2):

> bspline(3,x,[0,0,0,1,2]);

> pm1:=plot(bspline(3,x,[0,0,0,1,2]),x=-1..5,0..1,thickness=2):

Page 31: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.5 Kap.7 - Orthogonale Funktionensysteme und Termrekursion 31

> bspline(3,x,[0,0,1,2,3]);

> p0:=plot(bspline(3,x,[0,0,1,2,3]),x=-1..5,0..1,thickness=2):

> bspline(3,x,[0,1,2,3,4]);

> p1:=plot(bspline(3,x,[0,1,2,3,4]),x=-1..5,0..1,thickness=2):

> bspline(3,x,[1,2,3,4,4]);

> p2:=plot(bspline(3,x,[1,2,3,4,4]),x=-1..5,0..1,thickness=2):

> bspline(3,x,[2,3,4,4,4]);

> p3:=plot(bspline(3,x,[2,3,4,4,4]),x=-1..5,0..1,thickness=2):

> bspline(3,x,[3,4,4,4,4]);

> p4:=plot(bspline(3,x,[3,4,4,4,4]),x=-1..5,0..1,thickness=2):

> plots[display]([pm2,pm1,p0,p1,p2,p3,p4],

> title=‘B-Splines N(i,4)(x), i=-2,-1,0,1,2,3,4‘);

> interface(plotdevice=ps,

> plotoutput=datei3,

> plotoptions=‘width=640,height=480‘);

> plots[display]([pm2,pm1,p0,p1,p2,p3,p4],

> title=‘B-Splines N(i,3)(x), i=-2,-1,0,1,2,3,4‘);

> interface(plotdevice=win);

C.5 Kap.7 - Orthogonale Funktionensysteme und

Termrekursion

Datei: nm7ip1.mws

Orthogonale Polynome

> restart:

> with(plots):

> with(plottools):

> with(orthopoly); # orthopoly package

G(n, a, x) generates the nth Gegenbauer polynomial.

H(n, x) generates the nth Hermite polynomial.

L(n, x) generates the nth Laguerre polynomial.

L(n, a, x) generates the nth generalized Laguerre polynomial.

P(n, x) generates the nth Legendre polynomial.

Page 32: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

32 Arbeitsblatter in Matlab und Maple

P(n, a, b, x) generates the nth Jacobi polynomial.

T(n, x) generates the nth Chebyshev polynomial of the first kind.

U(n, x) generates the nth Chebyshev polynomial of the second kind.

> # Arbeitsverzeichnis

> pfad := ‘D:/Neundorf/Nwptexte/Math93/‘;

> # Graphic output files

> name1 := ‘nm7ip1.ps‘;

> name2 := ‘nm7ip2.ps‘;

> name3 := ‘nm7ip3.ps‘;

> name4 := ‘nm7ip4.ps‘;

> datei1 := cat(pfad,name1);

> datei2 := cat(pfad,name2);

> datei3 := cat(pfad,name3);

> datei4 := cat(pfad,name4);

Suppose we want to manipulate symbolically the Chebyshev polynomials

of the first kind Tn(x). We can represent these in Maple as T(n,x).

Recall that Tn(x) satisfies the linear recurrence T0(x)=1, T1(x)=x,

and Tn(x) = 2 x T{n-1}(x) - T{n-2}(x).

> T(2,x); # Chebyshev polynomials

Suppose also that for a particular value of n we want to expand

T(n,x) out as a polynomial in x. We can tell Maple’s expand function

how to do this by writing the own routine ‘expand/TT‘.

When expand sees T(n,x), it will invoke ‘expand/TT‘(n,x).

Hence we can write the following.

> ‘expand/TT‘ := proc(n,x) option remember;

> if n = 0 then 1

> elif n = 1 then x

> elif not type(n,integer) then TT(n,x)

> # can’t do anything

> else expand(2*x*TT(n-1,x) - TT(n-2,x))

> fi

> end:

This routine is recursive, but because we used the remember option,

the routine will compute T(100,x) quite quickly.

> whattype(TT(4,x));

> TT(4.0,x);

> TT(4,x);

> expand(TT(4,x));

Page 33: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.5 Kap.7 - Orthogonale Funktionensysteme und Termrekursion 33

> expand(TT(25,x)); # not sorted

> expand(TT(24,x)); # polynomial sorted up to degree 24

> sort(expand(TT(30,x))); # use sort

> p := sort(expand(TT(100,x))): # no display

> coeff(p,x,0);

> coeff(p,x,100);

Legendre-Polynome

> for n from 0 to 5 do

> P(n,x);

> od;

> for n from 0 to 5 do

> p[n]:=plot(P(n,x),x=-1..1):

> od:

> plots[display]([seq(p[n],n=0..5)],

> title=‘Legendre-Polynome P(n,x), n=0..5‘);

> interface(plotdevice=ps,

> plotoutput=datei1,

> plotoptions=‘width=640,height=480‘);

> plots[display]([seq(p[n],n=0..5)],

> title=‘Legendre-Polynome P(n,x), n=0..5‘);

> interface(plotdevice=win);

Tschebyscheff-Polynome

> for n from 0 to 5 do

> T(n,x);

> od;

> for n from 0 to 5 do

> p[n]:=plot(T(n,x),x=-1..1):

> od:

> plots[display]([seq(p[n],n=0..5)],

> title=‘Tschebyscheff-Polynome T(n,x), n=0..5‘);

> interface(plotdevice=ps,

> plotoutput=datei2,

> plotoptions=‘width=640,height=480‘);

> plots[display]([seq(p[n],n=0..5)],

> title=‘Tschebyscheff-Polynome T(n,x), n=0..5‘);

Page 34: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

34 Arbeitsblatter in Matlab und Maple

> interface(plotdevice=win);

Hermite-Polynome

> for n from 0 to 5 do

> H(n,x);

> od;

> for n from 0 to 5 do

> p[n]:=plot(H(n,x),x=-2.1..2.1):

> od:

> plots[display]([seq(p[n],n=0..5)],

> title=‘Hermite-Polynome H(n,x), n=0..5‘);

> interface(plotdevice=ps,

> plotoutput=datei3,

> plotoptions=‘width=640,height=480‘);

> plots[display]([seq(p[n],n=0..5)],

> title=‘Hermite-Polynome H(n,x), n=0..5‘);

> interface(plotdevice=win);

Laguerre-Polynome

> for n from 0 to 5 do

> L(n,x);

> od;

> for n from 0 to 5 do

> p[n]:=plot(L(n,x),x=0..12):

> od:

> plots[display]([seq(p[n],n=0..5)],

> title=‘Laguerre-Polynome L(n,x), n=0..5‘);

> interface(plotdevice=ps,

> plotoutput=datei4,

> plotoptions=‘width=640,height=480‘);

> plots[display]([seq(p[n],n=0..5)],

> title=‘Laguerre-Polynome L(n,x), n=0..5‘);

> interface(plotdevice=win);

Page 35: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.6 Kap.8 - Approximation, Ausgleichsrechnung und Fourier-Reihen 35

C.6 Kap.8 - Approximation, Ausgleichsrechnung

und Fourier-Reihen

Datei: nm8ip1.mws

Approximation - Uebersicht

> restart:

> with(plots):

> with(plottools):

> # Arbeitsverzeichnis

> pfad := ‘D:/Neundorf/Nwptexte/Math93/‘;

> # Graphic output files

> name1 := ‘nm8ip1.ps‘;

> name2 := ‘nm8ip2.ps‘;

> name3 := ‘nm8ip3.ps‘;

> name4 := ‘nm8ip4.ps‘;

> name5 := ‘nm8ip5.ps‘;

> name6 := ‘nm8ip6.ps‘;

> name7 := ‘nm8ip7.ps‘;

> name8 := ‘nm8ip8.ps‘;

> datei1 := cat(pfad,name1);

> datei2 := cat(pfad,name2);

> datei3 := cat(pfad,name3);

> datei4 := cat(pfad,name4);

> datei5 := cat(pfad,name5);

> datei6 := cat(pfad,name6);

> datei7 := cat(pfad,name7);

> datei8 := cat(pfad,name8);

Interpolation

Beispiel 1

> n := 4;

> xv := [-1,-0.5,0,0.5,1];

> yv := [seq(exp(xv[i]),i=1..n+1)];

> f := x->exp(x);

> N4 := x->0.043435*x^4+0.177348*x^3+0.499645*x^2+0.997853*x+1;

> p0:=plot([seq([xv[i],yv[i]],i=1..n+1)],style=point):

> p1:=plot(f(x),x=-1..1,thickness=2):

> p2:=plot(N4(x),x=-1..1):

Page 36: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

36 Arbeitsblatter in Matlab und Maple

> plots[display]([p0,p1,p2],

> title=‘Polynominterpolation fuer f(x)=exp(x), Ref.,N4(x)‘);

> interface(plotdevice=ps,

> plotoutput=datei1,

> plotoptions=‘width=640,height=480‘);

> plots[display]([p0,p1,p2],

> title=‘Polynominterpolation fuer f(x)=exp(x), Ref.,N4(x)‘);

> interface(plotdevice=win);

> p3:=plot(f(x)-N4(x),x=-1..1,thickness=2):

> plots[display]([p3],title=‘Interpolationsfehler f(x)-N4(x)‘);

> interface(plotdevice=ps,

> plotoutput=datei2,

> plotoptions=‘width=640,height=480‘);

> plots[display]([p3],title=‘Interpolationsfehler f(x)-N4(x)‘);

> interface(plotdevice=win);

Beispiel 2

> n := 4;

> xv := [0,1/4,1/2,3/4,1];

> yv := [seq(sin(Pi*xv[i]),i=1..n+1)];

> f := x->sin(Pi*x);

> interp(xv,yv,x);

> p4:=x->interp(xv,yv,x):

> p4(x);

> evalf(p4(x));

> p0:=plot([seq([xv[i],yv[i]],i=1..n+1)],style=point):

> p1:=plot(f(x),x=0..1,thickness=2):

> p2:=plot(p4(x),x=0..1):

> plots[display]([p0,p1,p2],

> title=‘Polynominterpolation fuer f(x)=sin(Pi*x), Ref.,p4(x)‘);

> interface(plotdevice=ps,

> plotoutput=datei7,

> plotoptions=‘width=640,height=480‘);

> plots[display]([p0,p1,p2],

> title=‘Polynominterpolation fuer f(x)=sin(Pi*x), Ref.,p4(x)‘);

> interface(plotdevice=win);

> p3:=plot(f(x)-p4(x),x=0..1,thickness=2):

> plots[display]([p3],title=‘Interpolationsfehler f(x)-p4(x)‘);

> interface(plotdevice=ps,

Page 37: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.6 Kap.8 - Approximation, Ausgleichsrechnung und Fourier-Reihen 37

> plotoutput=datei8,

> plotoptions=‘width=640,height=480‘);

> plots[display]([p3],title=‘Interpolationsfehler f(x)-p4(x)‘);

> interface(plotdevice=win);

Funktionenreihen: Taylor, Maclaurin

Beispiel

> M4 := x->0.041667*x^4+0.166667*x^3+0.5*x^2+x+1;

> p0:=plot([seq([xv[i],yv[i]],i=1..n+1)],style=point):

> p1:=plot(f(x),x=-1..1,thickness=2):

> p2:=plot(M4(x),x=-1..1):

> plots[display]([p0,p1,p2],

> title=‘Taylor-Reihe: Ref.,f(x)=exp(x), M4(x)‘);

> interface(plotdevice=ps,

> plotoutput=datei3,

> plotoptions=‘width=640,height=480‘);

> plots[display]([p0,p1,p2],

> title=‘Taylor-Reihe: Ref.,f(x)=exp(x), M4(x)‘);

> interface(plotdevice=win);

> p3:=plot(f(x)-M4(x),x=-1..1,thickness=2):

> plots[display]([p3],title=‘Fehler f(x)-M4(x)‘);

> interface(plotdevice=ps,

> plotoutput=datei4,

> plotoptions=‘width=640,height=480‘);

> plots[display]([p3],title=‘Fehler f(x)-M4(x)‘);

> interface(plotdevice=win);

Konvergenzbereich

Beispiel 1

> f := x->1/(1+x^2);

> phi0 := x->1;

> phi2 := x->1-x^2;

> phi4 := x->1-x^2+x^4;

> p0:=plot(phi0(x),x=0..2):

> pf:=plot(f(x),x=0..3,thickness=2):

> p2:=plot(phi2(x),x=0..1.2):

> p4:=plot(phi4(x),x=0..1.2):

> p1:=plot([[1,-0.1],[1,1.1]]):

> t0 := textplot([[2.1,1,‘phi0(x)‘],[1.3,-0.3,‘phi2(x)‘],

Page 38: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

38 Arbeitsblatter in Matlab und Maple

> [1.25,1.3,‘phi4(x)‘],[2.5,0.2,‘f(x)=1/(1+x^2)‘]]):

> plots[display]([p0,p1,p2,p4,pf,t0],

> title=‘Taylor-Reihe fuer f(x): phi0(x),phi2(x),phi4(x)‘);

> interface(plotdevice=ps,

> plotoutput=datei5,

> plotoptions=‘width=640,height=480‘);

> plots[display]([p0,p1,p2,p4,pf,t0],

> title=‘Taylor-Reihe fuer f(x): phi0(x),phi2(x),phi4(x)‘);

> interface(plotdevice=win);

Beispiel 2

> f := x->piecewise(x=0,0,exp(-1/x^2)); # auch einfach exp(-1/x^2)

> f(x);

> p0 := plot(f(x),x=-3..3,thickness=2):

> plots[display]([p0],title=‘f(x)=exp(-1/x^2)‘);

> interface(plotdevice=ps,

> plotoutput=datei6,

> plotoptions=‘width=640,height=480‘);

> plots[display]([p0],title=‘f(x)=exp(-1/x^2)‘);

> interface(plotdevice=win);

----------------------------------------------------------------------

Datei: nm8ip2.mws

Approximation von f(x)=ln(1+x), -0.5<=x<=0.5

> restart:

> with(plots):

> with(plottools):

> # Arbeitsverzeichnis

> pfad := ‘D:/Neundorf/Nwptexte/Math93/‘;

> # Graphic output files

> name1 := ‘nm8ip9.ps‘;

> name2 := ‘nm8ip10.ps‘;

> name3 := ‘nm8ip11.ps‘;

> name4 := ‘nm8ip12.ps‘;

> datei1 := cat(pfad,name1);

> datei2 := cat(pfad,name2);

> datei3 := cat(pfad,name3);

> datei4 := cat(pfad,name4);

Page 39: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.6 Kap.8 - Approximation, Ausgleichsrechnung und Fourier-Reihen 39

Interpolation

> n := 1;

> xv := [-0.5,0.5];

> yv := [seq(ln(1+xv[i]),i=1..n+1)];

> f := x->ln(1+x);

> f1:= x->x;

> f2:= x->x-x^2/2;

> evalf(f2(-0.5));

> evalf(f2(0.5));

> p1:= x->-(ln(2)-0.5*ln(3))+ln(3)*x;

> evalf(p1(0));

> q1:= x->-0.5*(ln(2)-0.5*ln(3))+ln(3)*x;

> evalf(q1(-0.5));

> evalf(q1(0.5));

> d:=evalf(0.5*(ln(2)-0.5*ln(3)));

> xd:=-0.05;

> dd:=evalf(f(xd)-q1(xd));

> xm:=evalf(1/ln(3)-1);

> dm:=evalf(f(xm)-q1(xm));

> pl0:=plot([seq([xv[i],yv[i]],i=1..n+1)],style=point):

> pl1:=plot(f(x),x=-0.5..0.5,thickness=3,xtickmarks=6,color=black):

> pl2:=plot(f1(x),x=-0.5..0.5):

> pl3:=plot(f2(x),x=-0.5..0.5):

> pl4:=plot(p1(x),x=-0.5..0.5):

> pl5:=plot(q1(x),x=-0.5..0.5,thickness=2):

> pl6:=plot({[[0,p1(0)],[0,q1(0)]],[[-0.5,p1(-0.5)],[-0.5,q1(-0.5)]],

> [[0.5,p1(0.5)],[0.5,q1(0.5)]]},thickness=4,color=black):

> pl7:=textplot([[0.11,0.08,‘f(x)‘],[0.46,0.5,‘f1(x)‘],

> [0.49,0.34,‘f2(x)‘],[0.35,0.2,‘p1(x)‘],

> [0.25,0.17,‘q1(x)‘]]):

> plots[display]([pl0,pl1,pl2,pl3,pl4,pl5,pl6,pl7],

> title=‘Approximationen fuer f(x)=ln(1+x)‘);

> interface(plotdevice=ps,

> plotoutput=datei1,

> plotoptions=‘width=640,height=480‘);

> plots[display]([pl0,pl1,pl2,pl3,pl4,pl5,pl6,pl7],

> title=‘Approximationen fuer f(x)=ln(1+x)‘);

> interface(plotdevice=win);

> pl8:=plot(f(x)-q1(x),x=-0.5..0.5,thickness=2,xtickmarks=6):

> pl9:=plot({[[0,0],[0,f(0)-q1(0)]],[[-0.5,0],[-0.5,f(-0.5)-q1(-0.5)]],

Page 40: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

40 Arbeitsblatter in Matlab und Maple

> [[0.5,0],[0.5,f(0.5)-q1(0.5)]],[[xm,0],[xm,dm]]},

> x=-0.5..0.5,thickness=3,color=black):

> d:=’d’:

> pl10:=textplot([[0.049,0.074,‘d=0.0719‘],[-0.15,0.035,‘dm=0.0764‘],

> [-0.485,-0.03,‘d‘],[ 0.485,-0.03,‘d‘]]):

> plots[display]([pl8,pl9,pl10],

> title=‘Approximationsfehler f(x)-q1(x)‘);

> interface(plotdevice=ps,

> plotoutput=datei2,

> plotoptions=‘width=640,height=480‘);

> plots[display]([pl8,pl9,pl10],

> title=‘Approximationsfehler f(x)-q1(x)‘);

> interface(plotdevice=win);

> r1:= x->p1(x)+0.5*(f(xm)-p1(xm));

> evalf(r1(-0.5)); evalf(r1(xm));

> evalf(r1(0.5));

> evalf(f(-0.5)-r1(-0.5));

> dopt:=evalf(f(xm)-r1(xm));

> evalf(f(0.5)-r1(0.5));

> pl12:=plot(r1(x),x=-0.5..0.5,thickness=2):

> pl13:=plot(r1(x)+dopt,x=-0.5..0.5):

> pl14:=plot({[[xm,f(xm)],[xm,r1(xm)]],[[-0.5,f(-0.5)],

> [-0.5,r1(-0.5)]],[[0.5,f(0.5)],[0.5,r1(0.5)]]},

> thickness=4,color=black):

> pl15:=textplot([[0.11,0.08,‘f(x)‘],[0.35,0.2,‘p1(x)‘],

> [0.25,0.17,‘r1(x)‘]]):

> plots[display]([pl0,pl1,pl4,pl12,pl13,pl14,pl15],

> title=‘Beste Approximation r1(x) fuer f(x)=ln(1+x)‘);

> interface(plotdevice=ps,

> plotoutput=datei3,

> plotoptions=‘width=640,height=480‘);

> plots[display]([pl0,pl1,pl4,pl12,pl13,pl14,pl15],

> title=‘Beste Approximation r1(x) fuer f(x)=ln(1+x)‘);

> interface(plotdevice=win);

> pl16:=plot(f(x)-r1(x),x=-0.5..0.5,thickness=2,xtickmarks=6):

> pl17:=plot({[[xm,0],[xm,f(xm)-r1(xm)]],

> [[-0.5,0],[-0.5,f(-0.5)-r1(-0.5)]],

> [[0.5,0],[0.5,f(0.5)-r1(0.5)]]},

> x=-0.5..0.5,thickness=3,color=black):

Page 41: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.6 Kap.8 - Approximation, Ausgleichsrechnung und Fourier-Reihen 41

> dopt:=’dopt’:

> pl18:=textplot([[-0.15,0.078,‘dopt=0.0742‘],

> [-0.475,-0.03,‘dopt‘],[ 0.475,-0.03,‘dopt‘]]):

> plots[display]([pl16,pl17,pl18],

> title=‘Approximationsfehler f(x)-r1(x)‘);

> interface(plotdevice=ps,

> plotoutput=datei4,

> plotoptions=‘width=640,height=480‘);

> plots[display]([pl16,pl17,pl18],

> title=‘Approximationsfehler f(x)-r1(x)‘);

> interface(plotdevice=win);

Tschebyscheff-Alternante

> altern:=[-0.5,xm,0.5];

--------------------------------------------------------------------

Datei: nm8ip3.mws

Gleichmaessige Approximation von f(x)=x^6, -1<=x<=1,

auf diskreter Menge xk=cos(k*pi/3), k=0,1,2,3

> restart:

> with(plots): with(plottools):

> # Arbeitsverzeichnis

> pfad := ‘D:/Neundorf/Nwptexte/Math93/‘;

> # Graphic output files

> name1 := ‘nm8ip13.ps‘;

> name2 := ‘nm8ip14.ps‘;

> datei1 := cat(pfad,name1);

> datei2 := cat(pfad,name2);

> n := 2;

> xv := [seq(cos(k*Pi/(n+1)),k=0..n+1)];

> yv := [seq(xv[i]^6,i=1..n+2)];

> f := x->x^6;

> p2:= x->-5/16+21/16*x^2;

> xx:=sqrt(sqrt(7))/2;

> xy:=evalf(xx);

> f(xx)-p2(xx);

> evalf(f(xy)-p2(xy));

> pl0:=plot([seq([xv[i],yv[i]],i=1..n+1)],style=point):

> pl1:=plot(f(x),x=-1..1,thickness=3,xtickmarks=4):

Page 42: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

42 Arbeitsblatter in Matlab und Maple

> pl2:=plot(p2(x),x=-1..1):

> pl5:=plot({[[0,0],[0,p2(0)]],[[-xx,p2(-xx)],[-xx,xx^6]],

> [[xx,p2(xx)],[xx,xx^6]]},thickness=4,color=black):

> pl6:=plot({[[xx,-0.02],[xx,xx^6]],[[-xx,-0.02],[-xx,xx^6]]},

> thickness=1,color=black):

> pl7:=textplot([[0.25,0.05,‘x^6‘],[0.65,0.45,‘p2(x)‘],

> [xy,-0.05,‘0.8133‘],[-xy,-0.05,‘-0.8133‘] ]):

> plots[display]([pl0,pl1,pl2,pl5,pl6,pl7],

> title=‘Approximation fuer f(x)=x^6 auf xk=cos(k*pi/3),k=0,1,2,3‘);

> interface(plotdevice=ps,

> plotoutput=datei1,

> plotoptions=‘width=640,height=480‘);

> plots[display]([pl0,pl1,pl2,pl5,pl6,pl7],

> title=‘Approximation fuer f(x)=x^6 auf xk=cos(k*pi/3),k=0,1,2,3‘);

> interface(plotdevice=win);

> q2:= x->-5/32+15/16*x^2;

> x1:=sqrt(sqrt(5))/2;

> x2:=evalf(x1);

> f(x1)-q2(x1);

> evalf(f(x1)-q2(x1));

> pl8:=plot(q2(x),x=-1..1):

> pl9:=plot({[[0,0],[0,q2(0)]],[[x2,q2(x2)],[x2,x2^6]],

> [[1,q2(1)],[1,1]],[[-x2,q2(-x2)],[-x2,x2^6]],

> [[-1,q2(-1)],[-1,1]]}, thickness=3,color=black):

> pl10:=textplot([[0.25,0.05,‘x^6‘],[0.7,0.45,‘q2(x)‘],

> [x2,-0.05,‘0.7477‘]]):

> pl11:=plot({[[x2,-0.02],[x2,x2^6]]},thickness=1,color=black):

> plots[display]([pl1,pl8,pl9,pl10,pl11],

> title=‘Approximation q2(x) fuer f(x)=x^6‘);

> interface(plotdevice=ps,

> plotoutput=datei2,

> plotoptions=‘width=640,height=480‘);

> plots[display]([pl1,pl8,pl9,pl10,pl11],

> title=‘Approximation q2(x) fuer f(x)=x^6‘);

> interface(plotdevice=win);

Tschebyscheff-Alternante naeherungsweise

> altern:=[-x2,0,x2,1];

> fa:=[seq(f(altern[i])-q2(altern[i]),i=1..4)];

> evalf(fa);

Page 43: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.6 Kap.8 - Approximation, Ausgleichsrechnung und Fourier-Reihen 43

Datei: nm8ip4.mws

Gleichmaessige Approximation von f(x)=sqrt(x), 1/16<=x<=1,

Remez-Algorithmus, xk, k =0,1,2,3

> restart:

> with(plots):

> with(plottools):

> with(linalg):

> # Arbeitsverzeichnis

> pfad := ‘D:/Neundorf/Nwptexte/Math93/‘;

> # Graphic output files

> name1 := ‘nm8ip15.ps‘;

> name2 := ‘nm8ip16.ps‘;

> datei1 := cat(pfad,name1);

> datei2 := cat(pfad,name2);

> n := 2;

> xv := [1/16,2/5,4/5,1];

> yv := [seq(sqrt(xv[i]),i=1..n+2)];

> f := x->sqrt(x);

> p2:= x->a0*x^2+a1*x+a2;

> p2m:= x->a0m*x^2+a1m*x+a2m;

Remez-Algorithmus

> A := matrix(4,4):

> a := vector(4):

> b := vector(4):

> x0:=xv[1]: x1:=xv[2]: x2:=xv[3]: x3:=xv[4]:

> iter:=5:

> printf(‘j=%2.0f\n‘,0);

> printf(‘x1=%12.9f ‘,x1);

> printf(‘x2=%12.9f\n‘,x2);

> for j from 1 to iter do

> A := [[x0^2,x0,1,1],

> [x1^2,x1,1,-1],

> [x2^2,x2,1,1],

> [x3^2,x3,1,-1]];

> b := [sqrt(x0),sqrt(x1),sqrt(x2),sqrt(x3)];

> inva:=evalm(inverse(A));

> a:=inva &* b;

Page 44: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

44 Arbeitsblatter in Matlab und Maple

> a:=evalm(a);

> det(A);

> a0:=evalf(a[1]);

> a1:=evalf(a[2]);

> a2:=evalf(a[3]);

> e:=evalf(a[4]);

> printf(‘j=%2.0f\n‘,j);

> printf(‘a0=%12.9f ‘,a0);

> printf(‘a1=%12.9f ‘,a1);

> printf(‘a2=%12.9f ‘,a2);

> printf(‘e =%12.9f\n‘,e);

> z:=solve(1/(2*sqrt(x))-(2*a0*x+a1),x):

> x1:=z[1];

> x2:=z[2];

> if x1>x2 then h:=x1; x1:=x2; x2:= h; fi;

> printf(‘x1=%12.9f ‘,x1);

> printf(‘x2=%12.9f\n‘,x2);

> od:

Modifizierter Remez-Algorithmus

> x0m:=xv[1]: x1m:=xv[2]: x2m:=xv[3]: x3m:=xv[4]:

> iter:=5:

> printf(‘j=%2.0f\n‘,0);

> printf(‘x1=%12.9f ‘,x1m);

> printf(‘x2=%12.9f\n‘,x2m);

> for j from 1 to iter do

> A := [[x0m^2,x0m,1,sqrt(x0m)],

> [x1m^2,x1m,1,-sqrt(x1m)],

> [x2m^2,x2m,1,sqrt(x2m)],

> [x3m^2,x3m,1,-sqrt(x3m)]];

> b := [sqrt(x0m),sqrt(x1m),sqrt(x2m),sqrt(x3m)];

> inva:=evalm(inverse(A));

> a:=inva &* b;

> a:=evalm(a);

> det(A);

> a0m:=evalf(a[1]);

> a1m:=evalf(a[2]);

> a2m:=evalf(a[3]);

> em:=evalf(a[4]);

Page 45: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.6 Kap.8 - Approximation, Ausgleichsrechnung und Fourier-Reihen 45

> printf(‘j=%2.0f\n‘,j);

> printf(‘a0=%12.9f ‘,a0m);

> printf(‘a1=%12.9f ‘,a1m);

> printf(‘a2=%12.9f ‘,a2m);

> printf(‘e =%12.9f\n‘,em);

> z:=solve(3*a0m*x^2+a1m*x-a2m,x):

> x1m:=z[1];

> x2m:=z[2];

> if x1m>x2m then h:=x1m; x1m:=x2m; x2m:= h; fi;

> printf(‘x1=%12.9f ‘,x1m);

> printf(‘x2=%12.9f\n‘,x2m);

> od:

Absolute Approximation

> pl1:=plot(f(x),x=0.0625..1,y=0..1,thickness=3,xtickmarks=4):

> pl2:=plot(p2(x),x=0.0625..1,thickness=1):

> pl7:=textplot([[0.7,0.76,‘f(x) = sqrt(x)‘],

> [0.4,0.4,‘p2(x) = -0.524x^2+1.319x+0.187‘],

> [0.085,0.02,‘1/16‘]]):

> plots[display]([pl1,pl2,pl7],

> title=‘Approximation p2(x) fuer f(x)=sqrt(x)‘);

> interface(plotdevice=ps,

> plotoutput=datei1,

> plotoptions=‘width=640,height=480‘);

> plots[display]([pl1,pl2,pl7],

> title=‘Approximation p2(x) fuer f(x)=sqrt(x)‘);

> interface(plotdevice=win);

Relative Approximation

> pl2m:=plot(p2m(x),x=0.0625..1,y=0..1,thickness=1):

> pl7m:=textplot([[0.7,0.76,‘f(x) = sqrt(x)‘],

> [0.4,0.4,‘p2m(x) = -0.650x^2+1.442x+0.172‘],

> [0.085,0.02,‘1/16‘]]):

> plots[display]([pl1,pl2m,pl7m],

> title=‘Modifizierte Approximation p2m(x) fuer f(x)=sqrt(x)‘);

> interface(plotdevice=ps,

> plotoutput=datei2,

> plotoptions=‘width=640,height=480‘);

> plots[display]([pl1,pl2m,pl7m],

> title=‘Modifizierte Approximation p2m(x) fuer f(x)=sqrt(x)‘);

Page 46: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

46 Arbeitsblatter in Matlab und Maple

> interface(plotdevice=win);

> f(x0)-p2m(x0);

> f(x1)-p2m(x1);

> f(x2)-p2m(x2);

> f(x3)-p2m(x3);

--------------------------------------------------------------------

Datei: nm8ip5.mws

Fourier-Reihenentwicklung (Fourier-Polynome, stetige Approximation

im Mittel) fuer f(x) mit Grafik

> restart:

> with(plots):

> with(plottools):

> # Arbeitsverzeichnis

> pfad := ‘D:/Neundorf/Nwptexte/Math93/‘;

> # Graphic output files

> name1 := ‘nm8ip17.ps‘;

> name2 := ‘nm8ip18.ps‘;

> name3 := ‘nm8ip19.ps‘;

> name4 := ‘nm8ip20.ps‘;

> datei1 := cat(pfad,name1);

> datei2 := cat(pfad,name2);

> datei3 := cat(pfad,name3);

> datei4 := cat(pfad,name4);

1.Funktion

f(x)=sign(sin(x))

> read ‘d:/neundorf/nwptexte/math93/cfourier.m‘;

> a:=-Pi; b:=Pi; T:=b-a;

> u1:=x->piecewise(x=-Pi,0,x<0,-1,x>0,1,0);

> # smartplot

> u1(x);

> smartplot(piecewise(x<0,-1,x>0,1,0));

> readlib(discont):

> discont(u1(x),x);

> plot(u1(x),x=a-1..b+1,-1..1,thickness=3,color=red);

> u2:=x->if x<0 then -1 elif x>0 then 1 else evalf(1/2) fi;

Page 47: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.6 Kap.8 - Approximation, Ausgleichsrechnung und Fourier-Reihen 47

> u2(x);

> u2(0.5); # Error, (in u2) cannot evaluate boolean

> u3:=x->piecewise(x<a-T/2,-1,x<a,1,x<0,-1,x<b,1,x<b+T/2,-1,1);

> u3(x);

> plot(u3(x),x=-8..8,y=-1..1,thickness=2,title=‘u3(x)=sign(sin(x))‘);

> f1:=x->u1(x-T*floor((x-a)/T));

> f1(x);

> Q1:=plot([f1(x),sin(x)],x=-8..8,-1..1,thickness=2,color=black,

> title=‘f(x)=sign(sin(x))‘):

> Q2:=plot(f1(x),x=-8..8,-1.5..1.5,thickness=1,color=black):

> plots[display](Q1);

> interface(plotdevice=ps,

> plotoutput=datei1,

> plotoptions=‘width=640,height=480‘);

> plots[display](Q1);

> interface(plotdevice=win);

Fourier-Polynom

Stetige Approximation im quadratischen Mittel

> cfourier(u1,a..b,8,‘reell‘,‘Plot‘);

> # W.Werner: Mathematik lernen mit Maple. dpunkt.verlag 1998

> bk:= k -> 2*(1-(-1)^k)/(Pi*k);

> bk(1); bk(2); bk(k);

Fourier-Polynom, explizit

> n:=’n’:

> pn:=(x,n)-> sum(bk(k)*sin(k*x),k=1..2*n+1);

> pn(x,n);

> pn(x,1);

> Q:=plot(pn(x,1),x=-8..8,-1.5..1.5,thickness=2,

> title=‘f(x)=sign(sin(x)), pn(x,1)=4/Pi*sin(x)+4/(3Pi)*sin(3*x)‘):

> plots[display]([Q,Q2]);

> P:=array(1..2,1..2,[]):

> n:=0:

> for i from 1 by 1 to 2 do

> for j from 1 by 1 to 2 do

> n:=n+1:

Page 48: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

48 Arbeitsblatter in Matlab und Maple

> if i*j=1 then

> P[i,j]:=plot([f1(x),pn(x,n)],x=-1..T+1,color=black,

> title=‘f(x) und Fourier-Polynome, n=1,2,3,4‘)

> else

> P[i,j]:=plot([f1(x),pn(x,n)],x=-1..T+1,color=black)

> fi:

> od:

> od:

> plots[display](P);

> interface(plotdevice=ps,

> plotoutput=datei2,

> plotoptions=‘width=640,height=480‘);

> plots[display](P);

> interface(plotdevice=win);

2.Funktion

f(x)=x^2, -pi<=x<=pi

> v1:=x->piecewise(x<0,x^2,x>0,x^2,0);

> # smartplot

> v1(x);

> smartplot(piecewise(x<0,x^2,x>0,x^2,0));

> discont(v1(x),x);

> plot(v1(x),x=a-1..b+1,-10..10,thickness=3,color=red);

> v3:=x->piecewise(x<a-T,0,x<a,(x+(-a+T/2))^2,x<b,x^2,

> x<b+T,(x-(b+T/2))^2,0);

> v3(x);

> plot(v3(x),x=-10..10,y=-0..10,thickness=2,title=‘v3(x)=x^2‘);

> f2:=x->v1(x-T*floor((x-a)/T));

> f2(x);

> Q4:=plot(f2(x),x=-10..10,0..10,thickness=2,

> title=‘f(x)=x^2, -pi<=x<=pi‘):

> Q5:=plot(f2(x),x=-10..10,-1..10,thickness=1):

> plots[display](Q4);

> interface(plotdevice=ps,

> plotoutput=datei3,

> plotoptions=‘width=640,height=480‘);

> plots[display](Q4);

> interface(plotdevice=win);

Fourier-Polynom

Stetige Approximation im quadratischen Mittel

Page 49: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.6 Kap.8 - Approximation, Ausgleichsrechnung und Fourier-Reihen 49

> cfourier(v1,a..b,8,‘reell‘,‘Plot‘);

> ak:= k -> if k=0 then 2/3*Pi^2 else 4*(-1)^k/(k*k) fi;

> ak(0); ak(1); ak(2); ak(k);

Fourier-Polynom, explizit

> n:=’n’:

> qn:=(x,n)-> ak(0)/2+sum(ak(k)*cos(k*x),k=1..n);

> qn(x,n);

> qn(x,1);

> Q6:=plot(qn(x,1),x=-10..10,-1..10,thickness=2,

> title=‘f2(x)=x^2, qn(x,1)=Pi^2/3-4*cos(x)‘):

> plots[display]([Q5,Q6]);

> PP:=array(1..2,1..2,[]):

> n:=0:

> for i from 1 by 1 to 2 do

> for j from 1 by 1 to 2 do

> n:=n+1:

> if i*j=1 then

> PP[i,j]:=plot([f2(x),qn(x,n)],x=a-1..b+1,color=black,

> ytickmarks=3,title=‘f(x) und Fourier-Polynome, n=1,2,3,4‘)

> else

> PP[i,j]:=plot([f2(x),qn(x,n)],x=a-1..b+1,color=black,

> ytickmarks=3)

> fi:

> od:

> od:

> plots[display](PP);

> interface(plotdevice=ps,

> plotoutput=datei4,

> plotoptions=‘width=640,height=480‘);

> plots[display](PP);

> interface(plotdevice=win);

-------------------------------------------------------------------

Datei: nm8ip6.mws

Stetige Approximation im Mittel

Faltungsschema von Runge

> restart:

> with(plots): with(plottools):

> with(linalg):

Page 50: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

50 Arbeitsblatter in Matlab und Maple

> # Arbeitsverzeichnis

> pfad := ‘D:/Neundorf/Nwptexte/Math93/‘;

> # Graphik output files

> name1 := ‘nm8ip21.ps‘;

> name2 := ‘nm8ip22.ps‘;

> datei1 := cat(pfad,name1);

> datei2 := cat(pfad,name2);

1.Faltung

> pl1:=plot([sin(x),cos(x)],x=0..2*Pi,y=-1.5..1.5,

> thickness=1,xtickmarks=6,color=black):

> pl2:=plot([[Pi,-1.5],[Pi,1.5]],thickness=3,color=black):

> pl3:=plot([[2*Pi,-0.1],[2*Pi,1]],thickness=1,color=black):

> pl4:=textplot([[5,0.7,‘cos(x)‘],[6,-0.6,‘sin(x)‘],[3.3,0.1,‘Pi‘]]):

> plots[display]([pl1,pl2,pl3,pl4],title=‘1.Faltung bei x=Pi‘);

> interface(plotdevice=ps,

> plotoutput=datei1,

> plotoptions=‘width=640,height=480‘);

> plots[display]([pl1,pl2,pl3,pl4],title=‘1.Faltung bei x=Pi‘);

> interface(plotdevice=win);

2.Faltung

> ql1:=plot([sin(x),cos(x)],x=0..Pi,y=-1.5..1.5,

> thickness=1,xtickmarks=3,color=black):

> ql2:=plot([[Pi/2,-1.5],[Pi/2,1.5]],thickness=3,color=black):

> ql3:=plot([[Pi,0.1],[Pi,-1]],thickness=1,color=black):

> ql4:=textplot([[2.8,0.6,‘sin(x)‘],[2.8,-0.7,‘cos(x)‘],

> [1.7,0.1,‘Pi/2‘]]):

> plots[display]([ql1,ql2,ql3,ql4],title=‘2.Faltung bei x=Pi/2‘);

> interface(plotdevice=ps,

> plotoutput=datei2,

> plotoptions=‘width=640,height=480‘);

> plots[display]([ql1,ql2,ql3,ql4],title=‘2.Faltung bei x=Pi/2‘);

> interface(plotdevice=win);

Page 51: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.6 Kap.8 - Approximation, Ausgleichsrechnung und Fourier-Reihen 51

Fourier-Reihenentwicklung

Maple-Prozedur cfourier.m nach W. Werner

% cfourier.m

% Fourier-Reihenentwicklung fuer F(x) in a..b mit n Knoten

> a:=-2; b:=2; n:=6;

> f:=x->piecewise(x<0,1,x>0,0,1/2);

> cfourier(u0,a..b,n,‘reell‘,‘Animation‘);

proc(F::procedure, U::range, m::integer)

local a, b, alpha, beta, k, n, T, c, c0, c1, c2, c3, ac, bc,

f, fc, u, v, om, opt, p, q, nA, nP, FourierSum, ParsevalSum,

FS, PS, AS, Ausn, QuadrMtl, Var, Vereinf, Koeff, brll, bkmplx,

bpars, bplot, bspkt, bgraf, banm, fopt, i, M;

if trunc(time()) mod 3 = 0 then

print(‘cfourier ist dem Buch‘);

print(‘W.Werner, Mathematik lernen mit Maple‘);

print(‘dpunkt.verlag, Heidelberg, 1998‘);

print(‘ISBN 3-920993-94-2‘);

print(‘entnommen.‘)

fi;

with(plots);

fopt := {};

for k from 4 to nargs do fopt := fopt union {args[k]} od;

brll := member(reell, fopt);

bkmplx := member(komplex, fopt);

bpars := member(Parseval, fopt);

bgraf := member(Graph, fopt);

bplot := member(Plot, fopt);

bspkt := member(Spektrum, fopt);

banm := member(Animation, fopt);

f := unapply(map(convert, F(t), rational), t);

p := array(1 .. 2);

alpha := op(1, U);

beta := op(2, U);

T := beta - alpha;

om := 2*Pi/T;

c0 := normal(int(f(t), t = U)/T);

c := unapply(

int(f(t)*(cos(om*u*t) - I*sin(om*u*t)), t = U)/T, u);

Var := select(type,

Page 52: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

52 Arbeitsblatter in Matlab und Maple

(indets(f(t)) minus {t}) union indets(U), name);

for k to nops(Var) do assume(op(k, Var), real) od;

if 0 < nops(Var) then print(

‘Hinweis: Evtl. Annahme z.B. ‘, op(Var),

‘ "nicht ganzzahlig" noetig.‘)

fi;

Ausn := select(type, {solve(denom(normal(c(u))) = 0, u)},

integer);

nA := nops(Ausn);

fc := proc(c, Ausn, n)

if member(n, Ausn) then limit(c(u), u = n)

else normal(simplify(c(n)))

fi

end;

assume(n, integer);

assume(t, real);

interface(showassumed = 0);

c1 := op(map(op, [seq(

[’n’ = Ausn[k], fc(c, Ausn, Ausn[k])], k = 1 .. nA)]))

;

Ausn := select(t -> evalb(0 <= t), Ausn);

nP := nops(Ausn);

c2 := op(map(op, [seq(

[’n’ = Ausn[k], evalc(2*Re(fc(c, Ausn, Ausn[k])))],

k = 1 .. nP)]));

c3 := op(map(op, [seq(

[’n’ = Ausn[k], evalc(-2*Im(fc(c, Ausn, Ausn[k])))],

k = 1 .. nP)]));

Vereinf := proc(expr, n)

factor(normal(subs(

{((-1)^n)^2 = 1, (-1)^(2*n) = 1}, evalc(expr))))

end;

u := Vereinf(c(n), n);

if bkmplx then

print(‘Komplexe Fourierkoeffizienten:‘);

if 0 < nA then print(c[n] = piecewise(c1, u))

else print(c[n] = u)

fi

fi;

a := ’a’;

b := ’b’;

if brll then

print(‘Reelle Fourierkoeffizienten:‘);

Page 53: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

C.6 Kap.8 - Approximation, Ausgleichsrechnung und Fourier-Reihen 53

if 0 < nA then

u := Vereinf(2*Re(expand(c(n))), n);

v := Vereinf(-2*Im(expand(c(n))), n);

print(a[n] = piecewise(c2, u),

b[n] = piecewise(c3, v))

else print(a[n] = normal(evalc(2*Re(c(n)))),

b[n] = normal(evalc(-2*Im(c(n)))))

fi

fi;

k := ’k’;

Koeff := [seq(evalc(fc(c, Ausn, k)), k = 1 .. m)];

k := ’k’;

FS := add(Koeff[k]*exp(I*k*om*t), k = 1 .. m);

FourierSum := map(factor, map(conjugate, FS) + c0 + FS);

if bkmplx then

print(‘Komplexe Fourierreihe: ‘);

print(f(t) = Sum(c[n]*exp(I*n*om*t),

n = -infinity .. infinity), ‘=‘);

print(‘=‘, FourierSum, ‘+...‘)

fi;

u := ‘if‘(0 < nA, max(op(Ausn)), 0);

FS := fc(c, Ausn, 0) + map(simplify,

add(2*Re(fc(c, Ausn, k))*cos(k*om*t), k = 1 .. m));

FourierSum := FS - map(simplify,

add(2*Im(fc(c, Ausn, k))*sin(k*om*t), k = 1 .. m));

if brll then

print(‘Reelle Fourierreihe: ‘);

print(f(t) = 1/2*a[0]

+ Sum(a[n]*cos(n*om*t), n = 1 .. infinity)

+ Sum(b[n]*sin(n*om*t), n = 1 .. infinity), ‘=‘)

;

print(‘=‘, FourierSum, ‘+...‘)

fi;

QuadrMtl := int(f(t)^2, t = U)/T;

if bpars then

print(‘Parsevalsche Gleichung: ‘);

PS := add(abs(fc(c, Ausn, k))^2, k = 1 .. u) + Sum(

Vereinf(abs(c(n)^2), n), n = u + 1 .. infinity);

print(map(normal, QuadrMtl = fc(c, Ausn, 0)^2 + 2*PS))

fi;

if nops(Var) = 0 then

QuadrMtl := evalf(Int(f(t)^2, t = U))/T;

Digits := 3;

Page 54: Numerische Mathematik€¦ · die wie kleine Maple-Nutzerhandbuc her zu den folgenden Gebieten untersetzt mit vielen Beispielen genutzt werden k onnen. - Formeln, Ausdr uc ke und

54 Arbeitsblatter in Matlab und Maple

u := evalf(sqrt(QuadrMtl));

opt := xtickmarks = [-evalf(T), 0, evalf(T)],

ytickmarks = [-u, u], color = black,

thickness = [2, 1], title = Fourierreihe;

Digits := 10;

q := array(1 .. 2, []);

p[1] := plot(

[f(t - floor((t - alpha)/T)*T), FourierSum],

t = -T .. T, discont = true, opt);

AS := seq(POLYGONS(map(evalf, [

[om*k, 0], [2*Pi*k/T, evalf(abs(fc(c, Ausn, k)))]

]), THICKNESS(2)), k = 1 .. 20);

with(plots);

p[2] := PLOT(AS, TITLE(Amplitudenspektrum));

if bgraf then display(p, scaling = constrained)

elif bplot then

display(p[1], scaling = unconstrained)

elif bspkt then

print(display(p[1], scaling = unconstrained));

print(display(p[2], scaling = unconstrained))

elif banm then

i := ’i’;

for i to m do

FS := fc(c, Ausn, 0) + map(simplify, add(

2*Re(fc(c, Ausn, k))*cos(k*om*t),

k = 1 .. i));

M[i] := plot([f(t - floor((t - alpha)/T)*T),

FS - map(simplify, add(

2*Im(fc(c, Ausn, k))*sin(k*om*t),

k = 1 .. i))], t = -T .. T,

title = cat(i), color = [blue, red])

od;

display([seq(M[i], i = 1 .. m)],

insequence = true)

fi

fi

end