Presentation is loading. Please wait.

Presentation is loading. Please wait.

Euler’s and Heun’s Methods

Similar presentations


Presentation on theme: "Euler’s and Heun’s Methods"— Presentation transcript:

1 Euler’s and Heun’s Methods
Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca © 2012 by Douglas Wilhelm Harder. Some rights reserved.

2 Outline This topic discusses numerical differentiation:
Euler's and Heun's Methods Outline This topic discusses numerical differentiation: Initial-value problems Euler’s method Heun’s method Multi-step methods

3 Outcomes Based Learning Objectives
Euler's and Heun's Methods Outcomes Based Learning Objectives By the end of this laboratory, you will: Understand how to approximate a solution to a 1st-order IVP using Euler’s method Understand the limitations of Euler’s method Be able to apply the same ideas from the trapezoidal rule to improve Euler’s method, i.e., Heun’s method

4 Initial-value Problems
Euler's and Heun's Methods Initial-value Problems Given the initial value problem Invariably, initial-value problems deal with time: We know the state y0 of a system at time t0 We understand how the system evolves (through the ODE) We want to approximate the state in the future

5 Ordinary Differential Equations
Euler's and Heun's Methods Ordinary Differential Equations Your first question should be: Can we always write a 1st-order ODE in the form: ? For example, the ODE could be implicitly defined as: Fortunately, the implicit function theorem says that, in almost all cases, “yes” We may end up using a truncated approximation similar to Taylor series

6 Ordinary Differential Equations
Euler's and Heun's Methods Ordinary Differential Equations What does the formula mean? Given any point (t*, y*), if a solution y(t) to the ODE passes through that point, the derivative of the solution must be:

7 Ordinary Differential Equations
Euler's and Heun's Methods Ordinary Differential Equations For example, the ODE suggests, for example, at the point (1, 2), the slope is approximately We could pick a few hundred points, determine the slopes at each of these lines, and plot that slope

8 Ordinary Differential Equations
Euler's and Heun's Methods Ordinary Differential Equations Doing this with the ODE yields

9 Ordinary Differential Equations
Euler's and Heun's Methods Ordinary Differential Equations The following are three solutions that satisfy these initial conditions

10 Ordinary Differential Equations
Euler's and Heun's Methods Ordinary Differential Equations The ODE was chosen because there is no explicit solution The next example does have explicit solutions

11 Ordinary Differential Equations
Euler's and Heun's Methods Ordinary Differential Equations Consider the ODE This has the following field plot:

12 Ordinary Differential Equations
Euler's and Heun's Methods Ordinary Differential Equations This clearly has y(t) = 1 as one solution; however, another solution is

13 Ordinary Differential Equations
Euler's and Heun's Methods Ordinary Differential Equations This clearly has y(t) = 1 as one solution; however, another solution is We can confirm this by substitution

14 Ordinary Differential Equations
Euler's and Heun's Methods Ordinary Differential Equations Calculating the derivative: Substituting the function into the equation Everything cancels in the numerator except the one

15 Ordinary Differential Equations
Euler's and Heun's Methods Ordinary Differential Equations Now, we see that y(1) = 1: The slope at this point should be: If we evaluate the calculated derivative at t = 1, we get:

16 Euler's and Heun's Methods
Euler’s Method Now, suppose we have an initial condition: y(t0) = y0 We want to approximate the solution at t0 + h; therefore, we can look at the Taylor series: where

17 Euler's and Heun's Methods
Euler’s Method We can replace the initial condition y(t0) = y0 into the Taylor series Next, we also know what the derivative is from the ODE: Thus,

18 Euler's and Heun's Methods
Euler’s Method Thus, we have a formula for approximating the next point together with an error term .

19 Euler's and Heun's Methods
Euler’s Method Using our example: we can implement both the right-hand side of the ODE and the solution: function [dy] = f2a(t, y) dy = (y - 1).^2 .* (t - 1).^2; end function [y] = y2a( t ) y = (t.^3 - 3*t.^2 + 3*t)./(t.^3 - 3*t.^2 + 3*t + 3);

20 Euler’s Method Using our example: we can therefore approximate y(0.1):
Euler's and Heun's Methods Euler’s Method Using our example: we can therefore approximate y(0.1): >> approx = *f2a(0,0) actual = >> actual = y2a(0.1) >> abs( actual - approx ) ans =

21 Euler's and Heun's Methods
Euler’s Method Now, if we halve h, the error should drop by a factor of 4 We will therefore approximate y(0.05): >> approx = *f2a(0,0) approx = >> actual = y2a(0.05) actual = >> abs( actual - approx ) ans = Previous error when h = 0.1:

22 Euler’s Method Lets consider what we are doing:
Euler's and Heun's Methods Euler’s Method Lets consider what we are doing: The actual solution is in red The two approximations are shown as circles We are following the same slope out from (0, 0)

23 Euler's and Heun's Methods
Euler’s Method The problem is, the second approximation does not approximate y(0.1)—it approximates the solution at the closer point t = 0.05 How can we proceed to approximate y(0.1)?

24 Euler's and Heun's Methods
Euler’s Method How about finding the slope at (0.05, 0.05) and following that out for another h = 0.05?

25 Euler's and Heun's Methods
Euler’s Method How about finding the slope at (0.05, 0.05) and following that out for another h = 0.05? >> *f2a(0.05, 0.05) ans =

26 Euler's and Heun's Methods
Euler’s Method We could repeat this process again, and approximate the solution at t = 0.15? >> *f2a( 0.1, ) ans =

27 Euler's and Heun's Methods
Euler’s Method As you can see, the three points are shadowing the actual solution

28 Euler’s Method Note that we require more work if we reduce h:
Euler's and Heun's Methods Euler’s Method Note that we require more work if we reduce h: Dividing h by 2 requires twice the work, and Dividing h by 10 requires ten times the work to approximate the same final point

29 Euler's and Heun's Methods
Euler’s Method In addition, we are using an approximation to approximate the next approximation, and so on… The error for approximating one point is O(h2) In the laboratory, you will attempt to determine how this affects the error

30 Euler's and Heun's Methods
Euler’s Method Thus, given an IVP and suppose we want to approximate y(tfinal) We could simply use h = tfinal – t0 and find y0 + h f(t0, y0) Problem: we have no control over the accuracy

31 Euler's and Heun's Methods
Euler’s Method Thus, given an IVP and suppose we want to approximate y(tfinal) Instead, divide the interval [t0, tfinal] into n points and now repeat Euler’s method n – 1 times

32 Euler's and Heun's Methods
Euler’s Method For example, if we chose n = 11, we would find approximations at 0 ? ? ? ? ? ? ? ? ? ? where y(0) = 0 and we want to approximate y(1)

33 Euler’s Method Use the initial points to approximate y(0.1):
Euler's and Heun's Methods Euler’s Method Use the initial points to approximate y(0.1): ? ? ? ? ? ? ? ? ?

34 Euler’s Method Use the next two points to approximate y(0.2):
Euler's and Heun's Methods Euler’s Method Use the next two points to approximate y(0.2): ? ? ? ? ? ? ? ?

35 Euler’s Method Use the next two points to approximate y(0.3):
Euler's and Heun's Methods Euler’s Method Use the next two points to approximate y(0.3): ? ? ? ? ? ? ?

36 Euler’s Method Use these two points to approximate y(0.4):
Euler's and Heun's Methods Euler’s Method Use these two points to approximate y(0.4): ? ? ? ? ? ?

37 Euler’s Method Use these two points to approximate y(0.5):
Euler's and Heun's Methods Euler’s Method Use these two points to approximate y(0.5): ? ? ? ? ?

38 Euler’s Method Use these two points to approximate y(0.6):
Euler's and Heun's Methods Euler’s Method Use these two points to approximate y(0.6): ? ? ? ?

39 Euler’s Method Use these two points to approximate y(0.7):
Euler's and Heun's Methods Euler’s Method Use these two points to approximate y(0.7): ? ? ?

40 Euler’s Method Use these two points to approximate y(0.8):
Euler's and Heun's Methods Euler’s Method Use these two points to approximate y(0.8): ? ?

41 Euler’s Method Use these two points to approximate y(0.9):
Euler's and Heun's Methods Euler’s Method Use these two points to approximate y(0.9): ?

42 Euler’s Method Finally, use these two to approximate y(1.0):
Euler's and Heun's Methods Euler’s Method Finally, use these two to approximate y(1.0): Our approximation is y(1.0) ≈

43 Euler’s Method You will implement Euler’s method: where
Euler's and Heun's Methods Euler’s Method You will implement Euler’s method: function [t_out, y_out] = euler( f, t_rng, y0, n ) where f a function handle to the bivariate function f(t, y) t_rng a row vector of two values [t0, tfinal] y0 the initial condition n the number of points that we will break the interval [t0, tfinal] into You will return two vectors: t_out a row vector of n equally spaced values from t0 to tfinal y_out a row vector of n values where y_out(1) equals y0 y_out(k) approximates y(t) at t_out(k) for k from 2 to n

44 Euler’s Method This function will: Determine Assign to
Euler's and Heun's Methods Euler’s Method This function will: Determine Assign to tout a vector of n equally spaced points going from t0 to tfinal, and yout a vector of n zeros where yout, 1 is assigned the initial value y0, For k going from 1 to n – 1, repeat the following: Using f, calculate the slope K1 at the point tout,k and yout,k, and Set

45 Euler’s Method For example, consider our initial-value problem
Euler's and Heun's Methods Euler’s Method For example, consider our initial-value problem Approximating the solution on [0, 1] with n = 11 points yields: >> [t2a, y2a] = [0, 1], 0, 11 ) t2a = y2a =

46 Euler’s Method The function ode45 is Matlab’s built-in ODE solver:
Euler's and Heun's Methods Euler’s Method The function ode45 is Matlab’s built-in ODE solver: [t2a, y2a] = [0, 1], 0, 11 ); plot( t2a, y2a, 'or' ); hold on [t2a, y2a] = [0, 1], 0 ); plot( t2a, y2a, 'b' )

47 Euler’s Method The function ode45 is Matlab’s built-in ODE solver:
Euler's and Heun's Methods Euler’s Method The function ode45 is Matlab’s built-in ODE solver: [t2a, y2a] = [0, 1], 0, 21 ); plot( t2a, y2a, 'or' ); hold on [t2a, y2a] = [0, 1], 0 ); plot( t2a, y2a, 'b' )

48 Euler’s Method For example, consider our initial-value problem
Euler's and Heun's Methods Euler’s Method For example, consider our initial-value problem Approximating the solution on [0, 1] with n = 11 points yields: >> [t2b, y2b] = [0, 1], 1, 11 ) t2b = y2b =

49 Euler’s Method In this case, Euler’s method does not fare so well:
Euler's and Heun's Methods Euler’s Method In this case, Euler’s method does not fare so well: hold on [t2b, y2b] = [0, 1], 1, 11 ); plot( t2b, y2b, 'or' ) [t2b, y2b] = [0, 1], 1 ); plot( t2b, y2b, 'b' )

50 Euler’s Method We can increase the number of points by a factor of 10:
Euler's and Heun's Methods Euler’s Method We can increase the number of points by a factor of 10: hold on [t2b, y2b] = [0, 1], 1, 101 ); plot( t2b, y2b, '.r' ) [t2b, y2b] = [0, 1], 1 ); plot( t2b, y2b, 'b' )

51 Error Analysis Now, we saw the error for Euler’s method was O(h2)
Euler's and Heun's Methods Error Analysis Now, we saw the error for Euler’s method was O(h2) However, except with the first point, we are using an approximation to find an approximation Thus, repeatedly applying Euler results in an error of O(h)

52 Improving on Euler’s Method
Euler's and Heun's Methods Improving on Euler’s Method In the lab, you will find that, for Euler’s method: Reducing the error by half requires twice as much effort and memory Reducing the error by a factor of 10 requires ten times the time and memory This is exceptionally inefficient and we will therefore take this lab and the next lab to see how we can improve on Euler’s method

53 Improving on Euler’s Method
Euler's and Heun's Methods Improving on Euler’s Method Suppose you are approximating the integral of a function over an interval:

54 Improving on Euler’s Method
Euler's and Heun's Methods Improving on Euler’s Method One of the worst approximations would be to simply use the value of the function at one end-point:

55 Improving on Euler’s Method
Euler's and Heun's Methods Improving on Euler’s Method At the very least, it would be better to approximate the integral by taking the average of the two end-points: This is the trapezoidal rule of integration

56 Improving on Euler’s Method
Euler's and Heun's Methods Improving on Euler’s Method When we are essentially integrating using information only at the initial value:

57 Improving on Euler’s Method
Euler's and Heun's Methods Improving on Euler’s Method The problem is, we would have to know the slope at t0 + h in order to approximate mimic the trapezoidal rule Note, however, that Euler’s method gives us an approximation of y(t0 + h) y(t0 + h) ≈ y0 + hK1 Therefore, we can approximate the the slope at t0 + h with

58 Improving on Euler’s Method
Euler's and Heun's Methods Improving on Euler’s Method Thus, we have one slope and one approximation of a slope: Applying the same principle as the trapezoidal rule, we would then approximate

59 Euler's and Heun's Methods
Graphically, Euler’s method follows the initial slope out a distance h We calculate only one slope: K1

60 Euler's and Heun's Methods
Heun’s method states that we determine the slope at the second point, too K2 K1

61 Euler's and Heun's Methods
Take the average of the two slopes and follow that new slope out a distance h: K2 K1

62 Euler's and Heun's Methods
Thus, you will write a second function, heun(), that has the same signature as euler(), where you will Determine Assign to tout a vector of n equally spaced points going from t0 to tfinal, and yout a vector of n zeros where yout, 1 is assigned the initial value y0, For k going from 1 to n – 1, repeat the following: Using f, calculate the slope K1 at the point tout,k and yout,k, Use K1 to find K2, and Set

63 Heun’s Method For example, consider our initial-value problem
Euler's and Heun's Methods Heun’s Method For example, consider our initial-value problem Approximating the solution on [0, 1] with n = 11 points yields: [t2a, y2a] = [0, 1], 0, 11 ) t2a = y2a =

64 Heun’s Method The function ode45 is Matlab’s built-in ODE solver:
Euler's and Heun's Methods Heun’s Method The function ode45 is Matlab’s built-in ODE solver: [t2a, y2a] = [0, 1], 0, 11 ); plot( t2a, y2a, 'or' ); hold on [t2a, y2a] = [0, 1], 0 ); plot( t2a, y2a, 'b' )

65 Heun’s Method For example, consider our initial-value problem
Euler's and Heun's Methods Heun’s Method For example, consider our initial-value problem Approximating the solution on [0, 1] with n = 11 points yields: >> [t2b, y2b] = [0, 1], 1, 11 ) t2b = y2b =

66 Heun’s Method Heun’s method is significant better than Euler:
Euler's and Heun's Methods Heun’s Method Heun’s method is significant better than Euler: [t2b, y2b] = [0, 1], 1, 11 ); plot( t2b, y2b, 'or' ); hold on [t2b, y2b] = [0, 1], 1 ); plot( t2b, y2b, 'b' ) Euler’s Method

67 Heun’s Method Comparing the accuracy of
Euler's and Heun's Methods Heun’s Method Comparing the accuracy of Euler’s method (11 and 41 points in magenta) , and Heun’s method (11 points in red) We see that Heun is significantly better

68 Heun’s Method The absolute errors are also revealing:
Euler's and Heun's Methods Heun’s Method The absolute errors are also revealing: A reduction by a factor of three 0.0239

69 Heun’s Method To be fair, we should count function evaluations:
Euler's and Heun's Methods Heun’s Method To be fair, we should count function evaluations: Euler’s method with n points has n – 1 function evaluations Heun’s method with n points has 2(n – 1) function evaluations Still, Heun’s method comes out ahead... 0.0239

70 Error Analysis Without proof, the error for Heun’s method is O(h3)
Euler's and Heun's Methods Error Analysis Without proof, the error for Heun’s method is O(h3) However, again, except with the first point, we are using an approximation to find an approximation As with Euler’s method, repeatedly applying Heun’s method will results in an error of O(h2)

71 Euler's and Heun's Methods
Summary We have looked at Euler’s and Heun’s methods for approximating 1st-order IVPs: Euler’s method is a direct application of Taylor’s series Heun’s method uses the ideas from the trapezoidal rule to improve on Euler’s method Heun’s method requires twice as many function evaluations as does Euler’s method and yet it is significantly more accurate

72 Euler's and Heun's Methods
References [1] Glyn James, Modern Engineering Mathematics, 4th Ed., Prentice Hall, 2007. [2] Glyn James, Advanced Modern Engineering Mathematics, 4th Ed., Prentice Hall, 2011.


Download ppt "Euler’s and Heun’s Methods"

Similar presentations


Ads by Google