0% found this document useful (0 votes)
17 views

Scientific ComputingforCSS

The document discusses the curriculum for computer science students at ETH Zurich which includes two mandatory courses on numerical and scientific computation. It covers topics like linear algebra, interpolation, integration, solving ODEs and PDEs. It also demonstrates how to define domains, meshes, boundary conditions and solve PDEs using MATLAB.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Scientific ComputingforCSS

The document discusses the curriculum for computer science students at ETH Zurich which includes two mandatory courses on numerical and scientific computation. It covers topics like linear algebra, interpolation, integration, solving ODEs and PDEs. It also demonstrates how to define domains, meshes, boundary conditions and solve PDEs using MATLAB.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Walter Gander, ETH Zürich 1

Scientific Computing for CS Students


Walter Gander
ETH Zürich
Switzerland
[email protected]
http://www.inf.ethz.ch/personal/gander/
Walter Gander, ETH Zürich 2

Abstract: At ETH Zürich we have redesigned our former courses on


numerical analysis for the education of our compter science students.
We make use of computer algebra for derivation of algorithms and for
some proofs. Furthermore we try to explain the fundamentals, on which
packages like the PDE Toolbox of MATLAB are built.
Walter Gander, ETH Zürich 3

Curriculum at ETH for CS students


2 mandatory courses à 3h lecture + 2h exercises per week (2nd year)
1. Numerical and Symbolic Computation (WS)
(arithmetic, exact computing, lin. and nonlinear eq., automatic
differentiation, system of eq., least squares, interpolation/extrapolation)
2. Scientific Computation (SS)
(quadrature, ODE, BVP, calculus of variation, finite elements)
Goals:
• understand and implement algorithms
• explain software (Maple, Matlab)
Walter Gander, ETH Zürich 4

Matlab PDE-Toolbox
Walter Gander, ETH Zürich 5

Define Domain
1

0.8

0.6

0.4 R2
R1
0.2

−0.2

−0.4

−0.6
SQ1
−0.8

−1
−1.5 −1.3 −1.1 −0.9 −0.7 −0.5 −0.3 −0.1 0.1 0.3 0.5 0.7 0.9 1.1 1.3 1.5
Walter Gander, ETH Zürich 6

Define Boundary Conditions


1

0.8

0.6

0.4

0.2

−0.2

−0.4

−0.6

−0.8

−1
−1.5 −1.3 −1.1 −0.9 −0.7 −0.5 −0.3 −0.1 0.1 0.3 0.5 0.7 0.9 1.1 1.3 1.5
Walter Gander, ETH Zürich 7

Create Mesh
1

0.8

0.6

0.4

0.2

−0.2

−0.4

−0.6

−0.8

−1
−1.5 −1.3 −1.1 −0.9 −0.7 −0.5 −0.3 −0.1 0.1 0.3 0.5 0.7 0.9 1.1 1.3 1.5
Walter Gander, ETH Zürich 8

Solve

100

90
Time=0.0233 Color: u
1
80
0.8

0.6 70

0.4
60
0.2

0 50

−0.2
40
−0.4

−0.6 30

−0.8
20
−1
−1.5 −1.3 −1.1 −0.9 −0.7 −0.5 −0.3 −0.1 0.1 0.3 0.5 0.7 0.9 1.1 1.3 1.5

10
Walter Gander, ETH Zürich 9

Asymptotic Solution

100

90
Time=1 Color: u
1
80
0.8

0.6 70

0.4
60
0.2

0 50

−0.2
40
−0.4

−0.6 30

−0.8
20
−1
−1.5 −1.3 −1.1 −0.9 −0.7 −0.5 −0.3 −0.1 0.1 0.3 0.5 0.7 0.9 1.1 1.3 1.5

10
Walter Gander, ETH Zürich 10

From Hairer-Wanner: Analysis by History:


M. Cauchy annonce, que, pour se conformer au voeu du
Conseil, il ne s’attachera plus à donner, comme il a fait jusqu’à
présent, des demonstrations parfaitement rigoureuses.
Conseil d’instruction de l’Ecole Polytechnique Paris, 24 nov.
1825
(Mr. Cauchy announces that he will comply to the wish of the
council and that he will not anymore, as he did till today, give
perfectly rigorous proofs.)
Walter Gander, ETH Zürich 11

Numerical Computing
Topics: finite arithmetic, interval arithmetic, floating-point numbers,
rounding errors, stability, well and ill-conditioned problems
Example: notion of convergence
Cauchy: {xn } convergent ⇐⇒ kxn − xm k < ε, ∀n, m > N
“epsilonic” for mathematicians
finite machine: no limit computations possible,
need approximation, i.e. termination criteria
limit computations in a computer algebra system (PhD D. Gruntz, 1996)
Thesis: good algorithms work thanks to rounding errors
Walter Gander, ETH Zürich 12

 
1 a
Square root (Heron): x1 = a, xi+1 = 2 xi + xi

termination criterion?
check successive iterates: |xi+1 − xi | < ε often wrong
check residual: a − x2n ≈ 0 better, but also often wrong
P. Rechenberg, G. Pomberger: Informatik-Handbuch, 1997:
SquareRoot (↓ a ↓ eps↑ x):
begin
x := a/10;
while |a − x2 | >eps∗a do
x := (x + a/x)/2
end
end SquareRoot
Walter Gander, ETH Zürich 13

Use Monotonicity
√ √
For Newton’s iteration: a < xn+1 < xn , ∀n (if x0 > a)
finite arithmetic: necessarily some xn+1 ≥ xn
xa = (1 + a)/2;
xn = (xa + a/xa)/2;
while xn < xa,
xa = xn
xn = (xa + a/xa)/2;
end
• elegant (no bells and whistles)
• machine-independent (works on any computer)
• no “epsilonic”
• does not work in theory
• makes use of finite arithmetic
Walter Gander, ETH Zürich 14

Bisection
f (x) continuous, f (a) < 0, f (b) > 0 ⇒ ∃s, a ≤ s ≤ b s.t. f (s) = 0
Usual algorithm:
while b-a> eps
x = (a+b)/2;
if f(x) <0, a=x; else b=x; end
end

Better termination criterion:


x = (a+b)/2;
while (a<x) & (x<b)
if f(x) <0, a=x; else b=x; end
x = (a+b)/2;
end

Does not work in exact arithmetic!


Walter Gander, ETH Zürich 15

Symbolic/Analytic versus Numerical Computation


y 00 = x2 y + x + 1 − 1
x

> dsolve(diff(y(x),x$2) = x^2 *y(x) + x+1-10*x,y(x));


y(x) =
−1/8 x3/2 −4 BesselI (1/4, 1/2 x2 )π 2 csgn(x)hypergeom([1/4], [3/2, 3/4], 1/16 x4 )
√ 2
+2 BesselI (1/4, 1/2 x )π 2 (Γ(3/4)) x hypergeom([1/2], [3/2, 11
2
8 ], 1/16 x4
)
2 3/2
√ 2
+9 BesselI (1/4, 1/2 x )π 2 (Γ(3/4)) x BesselK (3/4, 1/2 x2 )
2
StruveL(1/4, 1/2 x2 ) + 4 x (Γ(3/4)) BesselK (1/4, 1/2 x2 )
hypergeom([1/2], [3/2, 5/4], 1/16 x4 ) +
2 2
√ 3/2
9 x (Γ(3/4)) BesselK (1/4, 1/2 x ) 2π
2
 −1
2 −1
BesselI (−3/4, 1/2 x )StruveL(1/4, 1/2 x ) π (Γ(3/4)) +
√ √
C1 xBesselI (1/4, 1/2 x ) + C2 xBesselK (1/4, 1/2 x2 )
2
Walter Gander, ETH Zürich 16

Topics from symbolic computation


1. Polynomial arithmetic: e.g. Karatsuba algorithm for multiplication:
O(n1.585 ) instead of O(n2 )
2. Euclidian algorithm

for computing inverse elements in number fields, e.g. in Q( 2).
Factorization of polynomials.
3. Deriving of formulas with Maplea , e.g.:
• order of convergence for Newton’s method for multiple roots.
• proof that the order of convergence for the secant method is

(1 + 5)/2
4. Automatic differentiation, e.g. for the “billiard problem”
a W.Gander and D. Gruntz Derivation of Numerical Methods using Computer
Algebra, SIAM Review, Vol 41, Number 3, 1999.
Walter Gander, ETH Zürich 17

Convergence of the Secant Method


f (x), f (s) = 0 simple zero
f (xk ) (xk − xk−1 )
xk+1 = F (xk , xk−1 ) = xk −
f (xk ) − f (xk−1 )
F := (u,v) -> u - f(u)*(u-v)/(f(u)-f(v));
x[k+1] = F(x[k],x[k-1]);
f(s) := 0:
e2 = normal(readlib(mtaylor)(F(s+e1,s+e0) - s, [e0,e1], 4));

1 e0 (D(2) )(f )(s) e1


e2 =
2 D(f )(s)

%/e1/e0;
simplify(subs(e2=K*e1^p, e1=K*e0^p, %/K^p),assume=positive);
Walter Gander, ETH Zürich 18

(p2 −p−1) 1 K −p (D(2) )(f )(s)


e0 =
2 D(f )(s)
solve(ln(lhs(%)), p);

1√ 1 1 1√
5+ , − 5
2 2 2 2


convergence factor p = (1 + 5)/2 = 1.618033989
Walter Gander, ETH Zürich 19

Derivation of Simpson’s Rule


f := x-> a0 + a1*x + a2*x^2;
solve( {f(a)=y0, f(a+h)=y1, f(a+2*h)=y2},{a0,a1,a2});
assign(%);
i := int(f(x), x=a..a+2*h);
simplify(%);
n
3 ay0 h+a2 y0 −2 a2 y1 +a2 y2 −4 ay1 h+ay2 h+2 y0 h2
a0 = 1/2 h2
,
a1 = −1/2 3 y0 h+2 ay0 −4 ay1h+2 2
ay2 −4 y1 h+y2 h
, a2 = 1/2 y0 −2hy1 2
+y2

2 2 2 2
i := 3 ay0 h+a y0 −2 a y1 +a hy2 −4 ay1 h+ay2 h+2 y0 h
(3 y0 h+2 ay0 −4 ay1 +2 ay2 −4 y1 h+y2 h)((a+2 h)2 −a2 ) (y0 −2 y1 +y2 )((a+2 h)3 −a3 )
− 4h2
+ 6h2

1/3 h (y0 + 4 y1 + y2 )
Walter Gander, ETH Zürich 20

Newton-Cotes Integration
f := interp([0,1,2], [y1, y2, y3],z);
Q := int(f,z=0..2)/2;

1/6 y3 + 2/3 y2 + 1/6 y1

Derivation of the Integration Formula for quad8 in Matlab 5


f:=interp([0,1,2,3,4,5,6,7,8], [y1,y2,y3,y4,y5,y6,y7,y8,y9],z):
Q := int(f,z=0..8);
3956 23552 3712 41984 3632
14175 y1 + 14175 y2 − 14175 y3 + 14175 y4 − 2835 y5
+ 41984
14175 y6 −
3712
14175 y7 + 23552
14175 y8 + 3956
14175 y9
Walter Gander, ETH Zürich 21

More elegant
> closedcotes := n -> factor(int(interp([seq(i*h, i=0..n)],
[seq(f(i*h), i=0..n)], z),z=0..n*h)):
> simpson := closedcotes(2);
h b−a
(f (0) + 4 f (h) + f (2 h)) = (f (0) + 4 f (h) + f (2 h))
3 6
> milne := closedcotes(4);
2
h (7 f (0) + 32 f (h) + 12 f (2 h) + 32 f (3 h) + 7 f (4 h))
45
Walter Gander, ETH Zürich 22

Discretization Error
> for i from 1 by 1 to 4 do
> rule := closedcotes(i);
> err := taylor(rule - int(f(x), x= 0..i*h), h=0,i+4);
> od;
We obtain for the following first terms of the Taylor series:

i rule error

1 00 3
1 Trapezoidal 12 f (0) h
1 (4) 5
2 Simpson 90 f (0) h
3 3 (4) 5
3 8 -Rule 80 f (0) h
8 (6)
4 Milne 945 f (0) h7
Walter Gander, ETH Zürich 23

Analytical solution may be inexact

y 00 + 5y 0 + 4y = 1 − ex , y(0) = y 0 (0) = 0

de := diff(y(x),x$2)+5*diff(y(x),x)+4*y(x) = 1-exp(x);
dsolve({de, y(0)=0, D(y)(0)=0}, y(x));

ex −x
 1 −4 x e−x
y(x) = 5e − 2 + e −
20 60 6

f:= unapply(rhs(%),x);
for x from 0 by 0.001 to 0.01 do print(evalf(f(x))) od;
Walter Gander, ETH Zürich 24

MAPLE exact MAPLE (series) Runge Kutta h = 10−3


x y y y
1.0E-03 -.1498500750E-9 -.1665001417E-9 -1.6649995450E-10
2.0E-03 -.1347302699E-8 -.1330671200E-8 -1.3306708206E-09
3.0E-03 -.4436670003E-8 -.4486534425E-8 -4.4865337977E-09
4.0E-03 -.1055768469E-7 -.1062414507E-7 -1.0624143938E-08
5.0E-03 -.2064650894E-7 -.2072960938E-7 -2.0729606977E-08
6.0E-03 -.3578464671E-7 -.3578510160E-7 -3.5785096100E-08
7.0E-03 -.5675134695E-7 -.5676888099E-7 -5.6768868635E-08
8.0E-03 -.8457072070E-7 -.8465530880E-7 -8.4655282685E-08
9.0E-03 -.1204114060E-6 -.1204148653E-6 -1.2041481369E-07
1.0E-02 -.1649918048E-6 -.1650141667E-6 -1.6501407093E-07
cancellation in analytical expression!
Walter Gander, ETH Zürich 25

Circular Billiard Problem


g

X
α2
r
P
α1

py
x
Q
a px x

W. Gander and D. Gruntz, The Billard Problem, Int. J. Math. Educ. Sci.
Technol., 1992, Vol. 23, No. 6, 825-830
Walter Gander, ETH Zürich 26

X
α2
r
P
α1

py
x
Q
a px x

Equation for X
• Rotational symmetry
   
cos x − sin x
• Unit circle ⇒ X =  ⇒r= 
sin x cos x

• α1 = α2 ⇐⇒ (eXQ + eXP )T r = 0.
Walter Gander, ETH Zürich 27

Computer algebra system Maple


xp1 := px-cos(x);
xp2 := py-sin(x);
xq1 := a-cos(x);
xq2 := -sin(x);

lp := sqrt((xp1)**2 + (xp2)**2);
ep1 := xp1/lp; ep2 := xp2/lp;

lq := sqrt((xq1)**2 + (xq2)**2);
eq1 := xq1/lq; eq2 := xq2/lq;

f := (ep1+eq1)*sin(x) - (ep2+eq2)*cos(x);
Walter Gander, ETH Zürich 28

Result of Maple Computation (red: Parameters)


 
px − cos x a − cos x
f (x) := q
 +q  sin x
2 2 2
(px − cos x) + (py − sin x) (a − cos x) + sin x2
 
py − sin x sin x
− q
 −q  cos x = 0
2 2 2
(px − cos x) + (py − sin x) (a − cos x) + sin x2

For the solution with Newton’s method we need the derivative


Walter Gander, ETH Zürich 29

> D(f);
f0 (x) =
sin(x) (px −cos(x))(2 (px −cos(x)) sin(x)−2 (py−sin(x)) cos(x))
√ − 1/2 +
(px −cos(x))2 +(py−sin(x))2 ((px −cos(x))2 +(py−sin(x))2 )3/2

sin(x)
√ − 1/2 (a−cos(x))(2 (a−cos(x)) sin(x)+2 sin(x) cos(x))
2 3/2
sin(x)+
(a−cos(x))2 +(sin(x))2 ((a−cos(x))2 +(sin(x)) )
 
px −cos(x) a−cos(x)
√ 2 2
+ √ 2 2
cos(x)
(px −cos(x)) +(py−sin(x)) (a−cos(x)) +(sin(x))

cos(x) (py−sin(x))(2 (px −cos(x)) sin(x)−2 (py−sin(x)) cos(x))
− −√ − 1/2 3/2 −
(px −cos(x))2 +(py−sin(x))2 ((px −cos(x))2 +(py−sin(x))2 )

cos(x) sin(x)(2 (a−cos(x)) sin(x)+2 sin(x) cos(x))
√ + 1/2 3/2 cos(x) +
(a−cos(x))2 +(sin(x))2 ( (a−cos(x))2 +(sin(x))2 )
 
py−sin(x) sin(x)
√ 2 2
− √ 2 2
sin(x)
(px −cos(x)) +(py−sin(x)) (a−cos(x)) +(sin(x))
Walter Gander, ETH Zürich 30

Better Solution by Automatic Differentiation


function [y ys] = ffs(x)
% computes the functions f and its derivative fs for the
% billiard problem
global px py a
cs = -sin(x); c = cos(x);
ss = cos(x); s = sin(x);
xp1s = -cs ; xp2s = -ss; xp1 = px-c ; xp2 = py-s;
xq1s = -cs ; xq2s = -ss; xq1 = a-c ; xq2 = -s;
hs = (xp1*xp1s + xp2*xp2s)/sqrt(xp1^2 + xp2^2); h = sqrt(xp1^2 + xp2^2);
ep1s = (h*xp1s - xp1*hs)/h^2; ep1 = xp1/h;
ep2s = (h*xp2s - xp2*hs)/h^2; ep2 = xp2/h;
hs = (xq1*xq1s + xq2*xq2s)/sqrt(xq1^2 + xq2^2); h = sqrt(xq1^2 + xq2^2);
eq1s = (h*xq1s - xq1*hs)/h^2; eq1 = xq1/h;
eq2s = (h*xq2s - xq2*hs)/h^2; eq2 = xq2/h;
ys = (ep1s+eq1s)*s+(ep1+eq1)*ss-(ep2s+eq2s)*c-(ep2+eq2)*cs;
y = (ep1+eq1)*s - (ep2+eq2)*c;
Walter Gander, ETH Zürich 31

0.8 R1
R2

0.6
P
0.4 R3

0.2
Solutions for P = (0.5, 0.5)
0 Q
and Q = (−0.6, 0)
−0.2

−0.4

−0.6

−0.8

R4
−1 −0.5 0 0.5 1
Walter Gander, ETH Zürich 32

Analytical Solution?
2t
“Rationalizing” with t = tan(x/2) in f (x) ⇒ sin(x) = (1+t2 ) ,
1−t2
cos(x) = 1+t2
 
2 2
2t r px − 1−t
1+t2
a− 1−t
1+t2
f (x) = g(t) = 1+t2 + r 
1−t2 2 1−t2 2
2 
4 t2
  
2t a− 1+t2 +
px − 1+t2 + py− 1+t2
(1+t2 )2
 
2t t
1−t2 py− 1+t 2 2 1+t 2
− 1+t2  r − r
2 2
=0
1−t2 2
2 
4 t2
 
px − 1+t2 2t
+ py− 1+t2 a− 1−t
1+t2
+
(1+t2 )2

> sols := {solve(g = 0, t)};


RootOf ((py a + py) Z 4 + (4 px a + 2 a + 2 px ) Z 3 − 6 Z 2 py a +
(−4 px a + 2 a + 2 px ) Z − py + py a)
+ polynomial of degree 2 (squaring)
Walter Gander, ETH Zürich 33

[x(t),y(t)]
a
[X(t),Y(t)]
Toy/Child Problem α
vT
velocity of child vC

velocity of toy vT
vC

X−x ẋ
2 2 2
 
(X − x) + (Y − y) = a , Y −y =λ ẏ with λ > 0.
||v~T || = ||v~C || cos(α) = v~T · w
~
function zs = f(t,z)
%
[X Xs Y Ys] = child(t);
v =[Xs; Ys];
w =[X-z(1); Y-z(2)];
w = w/norm(w);
zs = (v’*w)*w;
Walter Gander, ETH Zürich 34

Jogger and Dog


• ẋ2 + ẏ 2 = w2 dog velocity
• velocity of dog parallel to difference vector
   
ẋ X −x
=λ with λ > 0
ẏ Y −y
2
X−x w
2 2 2 2

⇒ w = ẋ + ẏ = λ and ⇒ λ = X−x >0
Y −y ||(
Y −y )||
   
ẋ w X −x
=
ẏ X−x
 Y −y
Y −y
Walter Gander, ETH Zürich 35

Demos: Toy/Child Problem


d0: child walking on straight line pushing toy
d1: child walking on circle, long rod
d2: child walking on circle, rod equal radius
d3: child walking on sin-curve
d4: jogger on ellipse, fast dog
d5: jogger on ellipse, slower dog
Walter Gander, ETH Zürich 36

R1
Gauss Quadrature −1
f (x) dx ≈ w1 f (x1 ) + w2 f (x2 ) + w3 f (x3 )
6 unknowns: w1 , w2 , w3 , x1 , x2 , x3 demand exact values for monomes xj :
Z 1
w1 xj1 + w2 xj2 + w3 xj3 = xj dx, j = 0, . . . , 5
−1

u1 := w1 +w2 +w3: u2 := w1*x1 +w2*x2 +w3*x3:


u3 := w1*x1^2+w2*x2^2+w3*x3^2: u4 := w1*x1^3+w2*x2^3+w3*x3^3:
u5 := w1*x1^4+w2*x2^4+w3*x3^4: u6 := w1*x1^5+w2*x2^5+w3*x3^5:
solve({u1=2,u2=0,u3=2/3,u4=0,u5=2/5,u6=0}, {w1,w2,w3,x1,x2,x3});
x3 = RootOf (−3 + 5 Z 2 ), x1 = 0, x2 = −RootOf (−3 + 5 Z 2 ),


w2 = 5/9, w3 = 5/9, w1 = 8/9}


fsolve({u1=2,u2=0,u3=2/3,u4=0,u5=2/5,u6=0}, {w1,w2,w3,x1,x2,x3});
x1 = −5.1250001 × 10−24 , x2 = −0.7745966692x3 = 0.7745966692,


w1 = 0.8888888888, w2 = 0.5555555556, w3 = 0.5555555556, }


Walter Gander, ETH Zürich 37

Gauss Quadratur and orth. Polynomes


Polynome-division:

P2n−1 (x) = Hn−1 (x)Qn (x) + Rn−1 (x)


Z 1 Z 1 Z 1
P2n−1 dx = Hn−1 Qn dx + Rn−1 dx
−1 −1 −1
n
X n
X n
X
wi P2n−1 (xi ) = wi Hn−1 (xi )Qn (xi ) + wi Rn−1 (xi )
i=1 i=1 i=1
Z 1 Xn Z 1 Xn
error:= Hn−1 Qn dx− wi Hn−1 (xi )Qn (xi )+ Rn−1 dx− wi Rn−1 (xi )
−1 i=1 −1 i=1
R1
1. Qn O-Pol. on (−1, 1) ⇒ −1 Hn−1 Qn dx = 0
Pn
2. xi zero of Qn ⇒ i=1 wi Hn−1 (xi )Qn (xi ) = 0
R1 Pn
3. wi according to Newton-Cotes ⇒ −1 Rn−1 dx = i=1 wi Rn−1 (xi )
Walter Gander, ETH Zürich 38

with(orthopoly);
X := sort([fsolve(P(12,0,0,x)=0,x)]);
f := interp(X,[y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12],z):
Q:= int(f,z=-1..1);
X :=
[−0.9815606342, −0.9041172564, −0.7699026742, −0.5873179543, −
0.3678314990, −0.1252334085, 0.1252334085, 0.3678314990,
0.5873179543, 0.7699026742, 0.9041172564, 0.9815606342]
Q := +0.04717506586 y1 + 0.1069394295 y2 + 0.1600776434 y3 +
0.2031689029 y4 + 0.2334973032 y5 + 0.2491475198 y6 +
0.2491470907 y7 + 0.2334921379 y8 + 0.2031674530 y9 +
0.1600783833 y10 + 0.1069391353 y11 + 0.04717532540 y12
Unstable ! but Digits := 30;
Walter Gander, ETH Zürich 39

Q = 0.0471753363865118271946159582131 y1 +
0.106939325995318430960254714440 y2 +
0.160078328543346226334652551494 y3 +
0.203167426723065921749064450415 y4 +
0.233492536538354808760849926553 y5 +
0.249147045813402785000562439776 y6 +
0.249147045813402785000562434283 y7 +
0.233492536538354808760849896404 y8 +
0.203167426723065921749064452856 y9 +
0.160078328543346226334652528161 y10 +
0.106939325995318430960254716884 y11 +
0.0471753363865118271946159617832 y12
Walter Gander, ETH Zürich 40

Theory of Golub-Welsh
2
1. Three term recurrence relation pi+1 = (x − δi+1 )pi − γi+1 pi−1 ⇐⇒
      
xp0 δ1 1 p0 0
   2    
 xp1   γ2 δ2 1   p1   0 
= +
      
 .. .. .. ..   .
. .. 

 .  
  . . . 

 .
 
  . 

xpn−1 γn2 δn pn−1 pn
 
x1
 
2. ∃ T̃ ∼ T , with T̃ symmetric, T̃ U = U 
 .. 
. 
 
xn
T 2
Rb
3. normalize U U = I ⇒ wi = u1i a w(x) dx
Walter Gander, ETH Zürich 41

Variational problem: Ritz Ansatz

d
− (py 0 ) + qy = f
dx
is Euler-Lagrange equation of (δL = 0)
Z π
2
L= (py 0 + qy 2 − 2f y) dx
0

Ritz-Ansatz
N
X
y h (x) = cj ϕj (x)
j=1

Lh = cT Ac + bT c + γ
δLh = 0 ⇐⇒ 2Ac + b = 0
Walter Gander, ETH Zürich 42

D(u(x)) = −u00 (x) + 2u(x) = f (x), u(0) = 0, u0 (π) = 0


17 3
For f (x) = sin( 32
x), solution : u(x) = sin( x)
4 2
DG := u -> -D(D(u)) + 2*u - f: f := x -> 17/4*sin(3/2*x):
dsolve({DG(u)(x)=0, u(0)=0, D(u)(Pi)=0}, u(x)): assign(%):
y := x -> c1*(x-x^2/(2*Pi)) + c2*x*exp(-x/Pi);
integrand := diff(y(x),x)^2+2*y(x)^2-2*f(x)*y(x);

 2
 x
y := x 7→ c1 x − 1/2 xπ + c2 xe− π
x x 2
x −π x −π

integrand := c1 1 − + c2 e π − c2 πe +
  2
 x
2   2
 x

2 c1 x − 2π x
+ c2 xe− π − 17
2 sin( 3x
2 ) c1 x− x

−π
+ c2 xe
Walter Gander, ETH Zürich 43

L := int(integrand,x=0..Pi): evalf(L);
L := −0.8016693423 c1 − 1.582748542 c2 + 14.51403010 c2 c1 +
9.315538006 c1 2 + 5.691636323 c2 2
v := linalg[grad](L, [c1,c2]):
solve({v[1],v[2]}, {c1,c2}): evalf(%);
{c2 = 12.52353174, c1 = −9.713086137}
assign(%);
plot({y(x), u(x), y(x)-u(x)}, x=0..Pi, color=black);
Walter Gander, ETH Zürich 44

y(x)

u(x)

0.5

y(x)-u(x)

0 0.5 1 1.5 2 2.5 3


x

-0.5

-1
Walter Gander, ETH Zürich 45

Method of Galerkin

N
d X
Dy = − (py 0 ) + qy = f, Ritz-Ansatz: y h (x) = cj ϕj (x)
dx j=1

Residual ρ = Dy h − f
cj such that residual is orthogonal to {ψj (x)}, i = 1, . . . , N
Z π
⇐⇒ ρ ψj (x) dx = 0, i = 1, . . . , N
0

system of lin. eq. for cj : Ac = b


Z π Z π
aij = ψi (x)Dϕj (x) dx, bi = f (x)ψi (x) dx
0 0
Walter Gander, ETH Zürich 46

DG := u -> -D(D(u)) + 2*u - f: f := x -> 17/4*sin(3/2*x):


dsolve({DG(u)(x)=0, u(0)=0, D(u)(Pi)=0}, u(x)): assign(%):
psi1 := x -> sin(x): psi2 := x -> cos(x):
y := x -> c1*(x-x^2/(2*Pi)) + c2*x*exp(-x/Pi);
eq := {int(DG(y)(t)*psi1(t), t=0..Pi), int(DG(y)(t)*psi2(t),t=0..Pi)}:
evalf(eq); fsolve(eq, {c1,c2});
plot({y(x), u(x), y(x)-u(x)}, x=0..Pi, color=black);

{−1.038400155 c2 − 5.100000003 − 2.000000001 c1 ,


5.051451973 c1 − 3.400000002 + 4.146353669 c2 }

{c2 = 10.68573206, c1 = −8.098032910}


Walter Gander, ETH Zürich 47

Finite element method


1. discretization (intervals, triangles)
e
Pp
2. elements ui (x) = k=1 uk Nk (x)
knot variables ui , form functions Nk (x)




 ui+1

ui
-x
xi xi+1

x−xi
uei (ξ) = ui (1 − ξ) + ui+1 ξ, ξ = xi+1 −xi
h
PN e
3. Ritz-ansatz u = i=1 ui Ni
minimize functional or apply the method of Galerkin
Walter Gander, ETH Zürich 48

Conclusions
• scientific computing makes use of powerful software for numerical
and symbolic computing
• interesting and more realistic problems can be solved in class
• formulas used in numerical analysis and error estimates can be
derived using a computer algebra system
• the students can concentrate more on learning principles instead of
mechanically applying recipes
Walter Gander, ETH Zürich 49

References
1. W. Gander and D. Gruntz
The Billard Problem
Int. J. Math. Educ. Sci. Technol., 1992, Vol. 23, No. 6, 825-830.
2. W. Gander and J. Hřebı́ček, ed.
Solving Problems in Scientific Computing using Maple and Matlab
Springer, third edition 1997.
3. W. Gander and D. Gruntz
Derivation of Numerical Methods using Computer Algebra,
SIAM Review, Vol 41, Number 3, 1999.
4. W. Gander and W. Gautschi
Adaptive Quadrature - Revisited,
BIT Vol. 40, No. 1, March 2000, pp. 84–101.

You might also like