math 307 homework 4 – problem 3 solutionrdonald/resources/math307hw4soln.pdf · math 307 homework...

4

Click here to load reader

Upload: doanduong

Post on 28-Aug-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Math 307 Homework 4 – Problem 3 solutionrdonald/resources/math307hw4soln.pdf · Math 307 Homework 4 – Problem 3 solution 3 Figure 0.1: Comparison of the time complexity for solving

Math 307 Homework 4 – Problem 3 solution

UBC Department of Mathematics Winter 2013

Due: Friday 18 October 2013

Problem 2

This is a quick note on doing a cubic spline using the method outlined in Numerical Recipes.Numerical Recipes, Section 3.3, gives the formula for the control values y′′i :

16

[(xj − xj−1)y′′j−1 + 2(xj+1 − xj−1)y′′j + (xj+1 − xj)y′′j+1

]=

yj+1 − yj

xj+1 − xj− yj − yj−1

xj − xj−1. (0.1)

In our case, with points (0, 1), (0.5, 2), (1, 4), the xj are spaced evenly, with a separation of h = 0.5.The above equation simplifies substantially to

16

[y′′j−1 + 4y′′j + y′′j+1

]=

1h2

[yj−1 − 2yj + yj+1] . (0.2)

Writing this as a linear system over all points gives

16

4 1 . . .

1 4 1 . . ....

. . . . . . . . .

. . . 1 4

y′′1

y′′2...

y′′N

=1h2

−2 1 . . .

1 −2 1 . . ....

. . . . . . . . .

. . . 1 −2

y1

y2

...yN

. (0.3)

Note that writing the spline problem in this way indicates that we are equating a weighted sum ofy′′j values with a finite difference approximation to the second derivative of our data.

For our data, we fix y′′1 = y′′3 = 0, and so have only one equation:

46y′′2 =

1h2

[1 −2 1

] 124

= 4. (0.4)

1

Page 2: Math 307 Homework 4 – Problem 3 solutionrdonald/resources/math307hw4soln.pdf · Math 307 Homework 4 – Problem 3 solution 3 Figure 0.1: Comparison of the time complexity for solving

Math 307 Homework 4 – Problem 3 solution 2

For this, the solution is y′′2 = 6, so

0 ≤ x ≤ 0.5, y(x) = A1(x) + 2B1(x) + 6D1(x), (0.5)

0.5 ≤ x ≤ 1, y(x) = 2A2(x) + 4B2(x) + 6C2(x), (0.6)

where

A1(x) =0.5− x

0.5(0.7)

B1(x) =x

0.5(0.8)

D1(x) =0.52

6(B1(x)3 −B1(x)) (0.9)

A2(x) =1.0− x

0.5(0.10)

B2(x) =x− 0.5

0.5(0.11)

C2(x) =0.52

6(A2(x)3 −A2(x)) (0.12)

Problem 3

This problem solves a tridiagonal system using several different linear solvers. The purpose is todiscover and explain why some methods are more efficient than others.

Matlab code is in an attached zip file.

Figure 0.1 compares three different methods for solving a tridiagonal system. Based on the slopesof these log-log plots for large N :

• Matlab’s backslash command (it’s default linear solver) is a little better than O(N2)

• Matlab’s lu-solver is approximately O(N2)

• The hand-written tridiagonal solver is approximately O(N)

The efficiency of both the lu-factorization and Matlab’s backslash exceeds the theoretical O(N3)time complexity of Gaussian elimination with back-substitution. Probably, Matlab is aware of thesparsity of the input matrix, and ignoring zero entries to make these solves more efficient.

A clue that Matlab is indeed leveraging the sparsity of the tridiagonal matrix in Gaussian eliminationgiven by the breakdown of LU-factorization costs in Figure 0.2. The LU-factorization step is O(N2)for this tridiagonal matrix (rather than the worst-case O(N3)).

However, although the Matlab documentation suggests that backslash (or lu(A)) will use a tridiag-onal solver, we don’t see the optimal solution method for this problem unless we use our own solver.

Page 3: Math 307 Homework 4 – Problem 3 solutionrdonald/resources/math307hw4soln.pdf · Math 307 Homework 4 – Problem 3 solution 3 Figure 0.1: Comparison of the time complexity for solving

Math 307 Homework 4 – Problem 3 solution 3

Figure 0.1: Comparison of the time complexity for solving a tridiagonal system in three differentways.

Our tridiagonal solver looks at each of the N diagonal, N − 1 lower diagonal, and N right-hand-sideentries on its forward elimination pass, then again looks at the N diagonal, N − 1 upper-diagonaland N right-hand-side entries on its back-substitution pass, achieving an O(N) time complexity.

Page 4: Math 307 Homework 4 – Problem 3 solutionrdonald/resources/math307hw4soln.pdf · Math 307 Homework 4 – Problem 3 solution 3 Figure 0.1: Comparison of the time complexity for solving

Math 307 Homework 4 – Problem 3 solution 4

Figure 0.2: Breakdown of time complexity for solving a tridiagonal system using Matlab’s lu func-tion.