Scientific ComputingforCSS
Scientific ComputingforCSS
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
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
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
%/e1/e0;
simplify(subs(e2=K*e1^p, e1=K*e0^p, %/K^p),assume=positive);
Walter Gander, ETH Zürich 18
1√ 1 1 1√
5+ , − 5
2 2 2 2
√
convergence factor p = (1 + 5)/2 = 1.618033989
Walter Gander, ETH Zürich 19
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;
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
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
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
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
> 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
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
[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
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
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
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
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
2π
−π
+ 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.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
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.