Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Week 11 Numerical methods for ODEs 1.The basics: finite differences, meshes 2.The Euler method.

Similar presentations


Presentation on theme: "1 Week 11 Numerical methods for ODEs 1.The basics: finite differences, meshes 2.The Euler method."— Presentation transcript:

1 1 Week 11 Numerical methods for ODEs 1.The basics: finite differences, meshes 2.The Euler method

2 2 1. The basics: finite differences, meshes The derivative of a function y(x) at a point x = x * is defined by Imagine that, while taking the limit x → x *, we’d stop a little short – at, say, x = x * + s. If calculated this way, y' will not be exact, but if s is small, the error wouldn’t be too large, i.e. The above is an example of a finite-difference representation of the derivative of a function. It provides a basis for the most common numerical method for ODEs.

3 3 Computers can’t handle infinite arrays of data – hence, they can’t work with functions defined on continuous intervals. ۞ A mesh {x n } on an interval [a, b] is a set of N + 1 numbers, such that Instead, we use discrete functions, defined on meshes. ۞ A uniform mesh {x n } with a step s is such that or, equivalently,

4 4 Q: Can we calculate the derivative of a continuous function using the corresponding discrete one? A: We can, but only approximately and provided | x n+1 – x n | << 1 : ۞ A discrete function {y n } defined on a mesh {x n } corresponds to a continuous function y(x) defined on an interval [a, b] if Q: How can we estimate the accuracy of the above expression? A: Using Taylor series. (1)

5 5 where y (m) (x) denotes the m -th derivative of y(x). ۞ Let a function y have all derivatives at a point x. Then, for a sufficiently small s, y(x + s) is related to y(x) through the Taylor series: Without the sigma notation, the above formula becomes Example 1: Use the general formula (2) to extend (3) by two extra terms. (3) (2)

6 6 If a Taylor series is truncated after the term involving s M, the error introduced by the truncation is of the order of s M+1 – or, using the proper notation, For example, where O(...) is pronounced “the big O of...”.

7 7 The algebra of big Os The rules of handling big Os become evident if “ O(s n ) ” is replaced with “ const × s n ”, where the const will be called a ‘hidden constant’, e.g. Since a constant or sign in front of a big O can be incorporated into its hidden constant, these can be omitted or arbitrarily changed, e.g. The hidden constants of different big Os are implied to be different – hence, big Os never cancel each other, e.g.

8 8 Sol n : Denote the r.h.s. of (1) by, say, E : Example 2: Estimate the error of the finite-difference representation (1). In (4), replace y n → y(x n ), y n+1 → y(x n+1 )... (4) where s = x n+1 – x n.

9 9 Cancel in this expression what can be cancelled, truncate the series after the first non-zero term and recall what E is, which yields then expand the r.-h.s. using the Taylor series of y(x) about x n, Cancel s where possible and ‘flip’ the r.-h. and l.-h. sides...

10 10 Move O(s) onto the r.-h.s., but don’t necessarily change its sign, as O(s) includes an undetermined factor, possibly negative), This formula provides a finite-difference representation for y'(x n ), and also an error estimate for it.

11 11 where x n are the nodes of a uniform mesh with a step s. Example 3: Estimate the error of the following finite-difference representation: (5) Sol n : Denote the r.-h.s. of (5) by E and express x n ±1 in terms of x n and s... Expand y(x n ± s) in Taylor series up to and including O(s 2 )...

12 12 Thus, the finite-difference expression given does represent y'(x n ), and its error is O(s 2 ). Next, recalling what E is, obtain Comment: Observe that, when expanding y(x n ± s) in Example 3, we retained the terms O(s 2 ) – whereas in Example 2, it was sufficient to retain O(s)... why?!

13 13 Example 4: Determine a, b and c, such that the finite-difference expression (6) Sol n : Rewrite (6) in the form Expand the 2 nd and 3 rd terms on the r.-h-s. in Taylor series... would represent y'(x n ) with the smallest error possible.

14 14 Q: Was it correct to replace O((–2s) 3 ) in the third term with O(s 3 ) ? Now, collect like terms involving y(x n ), y'(x n ) and y''(x n )... A: Yes it was, as O((–2s) 3 ) and O(s 3 ) differ by a numeric factor (not involving s ) – hence, they are of the same order or magnitude. y(x n ̶ 2s) y(x n ̶ s)

15 15 To make this expression represent y'(x n ), require The resulting expression will generally have the accuracy of O(s) [because of the term involving sy''(x n ) which we so far retained]. However, we can eliminate this term and thus improve the accuracy to O(s 2 ) – so, require = 1 = 0 (8a) (8b) = 0 (7)

16 16 (8a,b) form a set of equations for the unknowns a, b and c, which can be easily solved: and then (7) yields Recalling (6) and taking into account (9), we obtain (9) This is the answer to the question: we’ve found a, b and c that minimise the error.

17 17 1. The Euler method Consider an initial value problem for a 1 st -order ODE, Let’s calculate the solution in the interval [a, b] where b > a. To solve (10)-(11) numerically, replace the continuous function y(x) with a discrete one { y n }. For simplicity, let the mesh {x n } be uniform with a step s. (10) (11) Then, using the finite-difference representation (4), one can replace (10)-(11) with...

18 18 Rewrite (12) in a recurrent form (i.e. with y n+1 on the l.-h.s. and y n on the r.-h.s): With y 0 determined by (13), (14) yields all further y n – for example, (12) (13) (14)

19 19 Comment: The numerical method based on equality (14) is called the Euler method. Example 5: Use the Euler method with a uniform mesh ( s = 0.2 ), to solve (15) in the interval [0, 0.8]. Comment: The ODE in (15) is linear and 1 st -order – hence, (15) can be solved analytically:

20 20 Sol n : Given the values of a, b and s, the mesh consists of 5 points: The initial condition yields Substitute f(x, y) = x + y and s = 0.2 into (14), which yields Further y n can be found using (14), which needs to be adopted to the problem at hand. We’ll call this equation the recurrence relation.

21 21 and for n = 1, 2, 3... For n = 0, the recurrence relation yields

22 22 nxnxn y n (initial condition) 001 Summarising the results: Observe that individual step errors accumulate... y n (computed) 10.21.2 20.41.48 30.61.856 40.82.3472 y n (exact) error% error 1.24280.0428 3. 846% 1.58360.1036 6.542% 2.04420.1882 9.206% 2.65110.303911.463%


Download ppt "1 Week 11 Numerical methods for ODEs 1.The basics: finite differences, meshes 2.The Euler method."

Similar presentations


Ads by Google