Presentation is loading. Please wait.

Presentation is loading. Please wait.

Euler’s and Heun’s Methods Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario,

Similar presentations


Presentation on theme: "Euler’s and Heun’s Methods Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario,"— 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: –Initial-value problems –Euler’s method –Heun’s method –Multi-step methods 2 Euler's and Heun's Methods

3 Outcomes Based Learning Objectives By the end of this laboratory, you will: –Understand how to approximate a solution to a 1 st -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 3 Euler's and Heun's Methods

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

5 Ordinary Differential Equations Your first question should be: Can we always write a 1 st -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 5 Euler's and Heun's Methods

6 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: 6 Euler's and Heun's Methods

7 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 7 Euler's and Heun's Methods

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

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

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

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

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

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

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

15 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: 15 Euler's and Heun's Methods

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

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

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

19 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); end 19 Euler's and Heun's Methods

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

21 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 : Euler's and Heun's Methods

22 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) 22 Euler's and Heun's Methods

23 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) ? 23 Euler's and Heun's Methods

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

25 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 = Euler's and Heun's Methods

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

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

28 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 28 Euler's and Heun's Methods

29 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(h 2 ) –In the laboratory, you will attempt to determine how this affects the error 29 Euler's and Heun's Methods

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

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

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

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

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

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

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

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

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

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

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

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

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

43 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 [t 0, t final ] y0 the initial condition n the number of points that we will break the interval [t 0, t final ] into You will return two vectors: t_out a row vector of n equally spaced values from t 0 to t final y_out a row vector of n values where y_out(1) equals y 0 y_out(k) approximates y(t) at t_out(k) for k from 2 to n 43 Euler's and Heun's Methods

44 Euler’s Method This function will: 1.Determine 2.Assign to a. t out a vector of n equally spaced points going from t 0 to t final, and b. y out a vector of n zeros where y out, 1 is assigned the initial value y 0, 3.For k going from 1 to n – 1, repeat the following: a.Using f, calculate the slope K 1 at the point t out,k and y out,k, and b.Set. 44 Euler's and Heun's Methods

45 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 = Euler's and Heun's Methods

46 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' ) 46 Euler's and Heun's Methods

47 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' ) 47 Euler's and Heun's Methods

48 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 = Euler's and Heun's Methods

49 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' ) 49 Euler's and Heun's Methods

50 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' ) 50 Euler's and Heun's Methods

51 Error Analysis Now, we saw the error for Euler’s method was O(h 2 ) –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) 51 Euler's and Heun's Methods

52 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 52 Euler's and Heun's Methods

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

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

55 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 55 Euler's and Heun's Methods

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

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

58 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 58 Euler's and Heun's Methods

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

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

61 Heun’s Method Take the average of the two slopes and follow that new slope out a distance h : K1K1 K2K2 61 Euler's and Heun's Methods

62 Heun’s Method Thus, you will write a second function, heun(), that has the same signature as euler(), where you will 1.Determine 2.Assign to a. t out a vector of n equally spaced points going from t 0 to t final, and b. y out a vector of n zeros where y out, 1 is assigned the initial value y 0, 3.For k going from 1 to n – 1, repeat the following: a.Using f, calculate the slope K 1 at the point t out,k and y out,k, b.Use K 1 to find K 2, and c.Set. 62 Euler's and Heun's Methods

63 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 = Euler's and Heun's Methods

64 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' ) 64 Euler's and Heun's Methods

65 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 = Euler's and Heun's Methods

66 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 66 Euler's and Heun's Methods

67 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 67 Euler's and Heun's Methods

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

69 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 Euler's and Heun's Methods

70 Error Analysis Without proof, the error for Heun’s method is O(h 3 ) –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(h 2 ) 70 Euler's and Heun's Methods

71 Summary We have looked at Euler’s and Heun’s methods for approximating 1 st -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 71 Euler's and Heun's Methods

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


Download ppt "Euler’s and Heun’s Methods Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario,"

Similar presentations


Ads by Google