example 4 - faculty.jacobs-university.de · 120202: esm4a - numerical methods 39 visualization and...

29
120202: ESM4A - Numerical Methods 36 Visualization and Computer Graphics Lab Jacobs University Example 4 The Taylor series represents the function, if . For other values of x, the error term may not converge to 0. Hence, for x > 1, we cannot use the Taylor series. Conclusion: We have to compute the so-called range of convergence before we apply Taylor expansion.

Upload: others

Post on 26-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 36

Visualization and Computer Graphics LabJacobs University

Example 4

• The Taylor series represents the function, if .• For other values of x, the error term may not converge

to 0.• Hence, for x > 1, we cannot use the Taylor series.

• Conclusion: We have to compute the so-called range of convergence before we apply Taylor expansion.

Page 2: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 37

Visualization and Computer Graphics LabJacobs University

Putting it into practice

• Use Taylor series to approximate function values.• Example 1: cos (0.1)• Actual value:• Taylor series at c=0:• Approximate values for cos (0.1) using truncated

Taylor series:

• Conclusion: We can quickly get good approximations.

Page 3: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 38

Visualization and Computer Graphics LabJacobs University

Speed of convergence

• We have observed that the Taylor expansion does nothave to converge to the actual solution.

• Question: If it does converge, how fast does itconverge?

• In practice: How many terms of the truncated Taylor series do we need for a good approximation?

Page 4: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 39

Visualization and Computer Graphics LabJacobs University

Observation• Compute ln (2):• First solution: Determine Taylor series for ln (1+x) at

c=0 and evaluate Taylor series for x=1.

Truncating after 8 terms delivers ln (2) ≈ 0.63452.• Second solution: Determine Taylor series for

at c=0 and evaluate Taylor series for x=1/3.

Truncating after 4 terms delivers ln (2) ≈ 0.69313.• The actual value is 0.69315.• The second solution converges much faster.

Page 5: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 40

Visualization and Computer Graphics LabJacobs University

Proximity of x to c

• The closer x is to c, the higher the accuracy of ourapproximation.

• Note that this error is in addition to the truncationerror.

Page 6: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 41

Visualization and Computer Graphics LabJacobs University

Taylor‘s theorem for f(x+h)

• Let .• Then, we get for that

with

truncatedTaylor series

error term

Page 7: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 42

Visualization and Computer Graphics LabJacobs University

Remarks

• This second theorem follows directly from the firstone for xold = x+h and c = x.

• If h->0, the error term converges to 0 with at least the speed of hn+1, if the (n+1)-st derivative is boundedon the interval [x,x+h]. We write error term = O(hn+1).The O-notation means (there exists a C such that)

In our case, C > .

Page 8: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 43

Visualization and Computer Graphics LabJacobs University

Example• Evaluation of interest:• Use f(z) = ln (z) and expand at e. • Derivatives:

• Expansion:

• Range of convergence: sufficient

Page 9: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 44

Visualization and Computer Graphics LabJacobs University

Summary: Taylor series approximation

• Given problem: evaluate f(x) with error bound e.• Known: f(c) for c close to x.• Requirement: for .• Check: Taylor series represents function f on [a,b].• Estimate maximum error when computing f(x) using a

truncated Taylor series with n terms.• Choose n such that the estimated maximum error is

smaller than error bound e.• Evaluate the truncated Taylor series with n terms to

approximate f(x).

Page 10: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 45

Visualization and Computer Graphics LabJacobs University

Generalization: Numerical approach

• Given: “hard“ problem.• Solution: Find an algorithmic approach to solve the

problem approximately.• Caveat: Check the limitations/constraints of the

applicability of the approach.• Approximation error: Compare the maximum error to

the error threshold determined by the application.• Convergence: Numerical methods often improve when

executing more computations. Does the approximationconverge towards the actual solution? I.e., does theerror go to 0?

• Convergence rate: How fast does the error go to 0?

Page 11: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 46

Visualization and Computer Graphics LabJacobs University

Goals revisited

In this course, we will:• Discuss algorithmic approaches to solve standard

mathematical problems with applications in engineering and science.

• Discuss the approaches with respect to theirapplicability (constraints, convergence).

• Discuss the approaches with respect to thepracticability (approximation error, convergencerate).

Page 12: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 47

Visualization and Computer Graphics LabJacobs University

1.2 Number Representations

Page 13: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 48

Visualization and Computer Graphics LabJacobs University

Definition

• Let b є N\{1}.• Every number x є N0 can be written in a unique

representation with respect to base b by

with ai є N0 and ai < b.

Page 14: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 49

Visualization and Computer Graphics LabJacobs University

b=10• Base 10:

• Notation:

• Fractions:

• Real numbers:

Page 15: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 50

Visualization and Computer Graphics LabJacobs University

Infinite representations

• For irrational numbers (such as e or π) an infinite number of coefficients bi is required.

• But: not every infinite representation impliesirrationality. Counter-example: 1/3.

Page 16: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 51

Visualization and Computer Graphics LabJacobs University

Base representations in computers

• Computer systems are using– base 2 (binary)– base 8 (octal)– base 16 (hexadecimal)

Page 17: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 52

Visualization and Computer Graphics LabJacobs University

Simple base representations

• A number with a simple base representation withrespect to one base may have a complicated baserepresentation (many coefficients, maybe eveninfinite) with respect to another base.

Page 18: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 53

Visualization and Computer Graphics LabJacobs University

Base conversion

• How do we get from one base representation to another?

• In particular, how can we switch between bases 2, 8, 16, and 10?

Page 19: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 54

Visualization and Computer Graphics LabJacobs University

Conversion b->10

• (an an-1 … a0)b = an bn + an-1 bn-1 + … a0 b0

• Then, just do the math …• Example: (42)8 = 4x81 + 2x80 = (34)10

Page 20: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 55

Visualization and Computer Graphics LabJacobs University

Conversion 2 <-> 8 and 2 <-> 16

• 2 <-> 8:Three consecutive bits represent one octal digit.Example:

• 2 <-> 16:Four consecutive bits represent one hexadecimal digit.

Page 21: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 56

Visualization and Computer Graphics LabJacobs University

Conversion 10 -> b

• The only somewhat more sophisticated part is theconversion from basis 10 to basis b.

• Two approaches:– Algorithm by Euclid (330 - 275 b.c.)– Algorithm using Horner‘s scheme (1786 - 1827)

Page 22: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 57

Visualization and Computer Graphics LabJacobs University

Algorithm by Euclid

Input: (x)10

Output: (x)b. 1. Determine exponent n with x < bn+1

2. For i = n to 0, compute– ai := x div bi // integer division– x := x mod bi // modulo operation

3. Return an an-1 … a0

Page 23: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 58

Visualization and Computer Graphics LabJacobs University

Example

Conversion 10->8: (34)10

1. 81 < 34 < 82 -> n=1.2. Iteration:

• a1 := 34 div 81 = 4x := 34 mod 81 = 2

• a0 := 2 div 80 = 2x := 2 mod 80 = 0

3. Output (42)8

Page 24: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 59

Visualization and Computer Graphics LabJacobs University

Remark

• The algorithm can be easily extended to rational numbers. Only the stopping criteria needs to bechanged.

• The algorithm is intuitive, but the first step isinefficient for a computer implementation.

Page 25: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 60

Visualization and Computer Graphics LabJacobs University

Horner‘s scheme

• Nested form for polynomial representation:(an an-1 … a0) b = an bn + an-1 bn-1 + … + a2 b2 + a1 b1 + a0 b0

= ((…((an) b+ an-1) b … + a2) b + a1) b + a0

Page 26: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 61

Visualization and Computer Graphics LabJacobs University

Algorithm using Horner‘s scheme

Input: (x)10

Output: (x)b.

1. i := 02. While (x != 0), compute

– ai := x mod b – x := x div b– i := i+1

3. Return an an-1 … a0

Page 27: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 62

Visualization and Computer Graphics LabJacobs University

Example

Conversion 10->8: (34)10

• a0 := 34 mod 8 = 2x := 34 div 8 = 4

• a1 := 4 mod 8 = 4x := 4 div 8 = 0

• Stop and output (42)8

Page 28: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 63

Visualization and Computer Graphics LabJacobs University

Remarks

• This algorithm is directly applicable to real numbers. However, one should stop after a certain maximumnumber of iterations, if the representation is infinite.

• It does not require the knowledge of the highestexponent n.

• Moreover, it avoids the division by large numbers.Why is this a problem?

Page 29: Example 4 - faculty.jacobs-university.de · 120202: ESM4A - Numerical Methods 39 Visualization and Computer Graphics Lab Jacobs University Observation • Compute ln (2): • First

120202: ESM4A - Numerical Methods 64

Visualization and Computer Graphics LabJacobs University

Computer representation

• In computer systems, only a finite number of digits(or bits) is available.

• Hence, infinite representations need to be truncated.• This introduces a truncation or roundoff error.