Engineering Science (MENG10004) Mechanics : Statics, Kinematics, Dynamics 2022/2023 Handout 10 – Numerical Integration (non-examinable) The equations of motion for dynamics problems involve differential equations, where the accelerations are functions of the displacements, velocities and time. For many classical mechanics problems, as taught in this unit, analytical solutions can be found. However, in general the differential equations are solved numerically. In numerical solution techniques the equations of motion are evaluated at discrete points in time, and the resulting velocities and positions are calculated at those intervals. Numerical methods are approximations and will always involve some error compared to the exact solution, but can address more complicated systems of equations. Different numerical methods exist for solving differential equations, and several will be introduced in your Engineering Mathematics unit. In this handout, we occasionally use selected numerical methods to solve problems in dynamics. 10.1 Forward Euler The simplest and most intuitive numerical integration method is Forward Euler. Let a first-order ODE be given by: dx = f (x, t) dt The aim is to solve for x(t). We evaluate the function at discrete points in time tn t = [t0 , t1 , t2 , . . . , tn ] (10.1) with step size h = ∆T . The derivative is written as: dx x(t + h) − x(t) x(tn+1 ) − x(tn ) xn+1 − xn ' = = dt h h h which gives xn+1 = xn + h dx dt = xn + hf (xn , tn ) The solution for the next time step, tn+1 , depends on the value of the function f (xn , tn ) and size of the time step, h. As h reduces to zero, we approach the true solution to the differential equation. NB: we shall see that the Forward Euler method can easily lead to inaccurate results, even for moderate step sizes h, so only ever use this for educational purposes, and not for real life applications! Example 10.1 – First-Order ODE Consider the following ODE (which you will see return in the Engineering Mathematics unit): dx = −x dt This has an exact solution of the form: x(t) = C e−t Applying the boundary condition x(0) = 1, constant C = 1. Let us explore effect of different step sizes, and how the numerical solution compares to the exact solution. 1 Mechanics: Numerical Integration MS - 2022/2023 1 1 h = 0.5 h = 0.25 0.75 x(t) x(t) 0.75 0.5 0.25 0 0.5 0.25 0 1 2 3 4 0 5 0 1 2 t 3 1 h = 0.05 0.75 x(t) 0.75 x(t) 5 1 h = 0.1 0.5 0.25 0 4 t 0.5 0.25 0 1 2 3 4 0 5 0 1 t 2 3 4 5 t The accuracy of the numerical solution increases for smaller step size. However, the increased accuracy comes at the expense of the increased number of calculations. In dynamic systems the equations of motion in dynamics will involve second-order ODEs. These can be solved numerically by using a state-space formulation, which in effect converts a second-order ODE into a set of two first-order differential equations. Consider a first-order differential equation: dp = f (p, t) dt with a state space vector such that dp = dt x ẋ p= p(2) f (x, ẋ, t) ẋ ẍ = The solution is then found using the standard Forward Euler integration: pn+1 = pn + h dpn dt For the specific case of acceleration, velocity and position this takes the form: xn+1 xn vn = +h vn+1 vn an 2 Mechanics: Numerical Integration MS - 2022/2023 Example 10.2 – Spring-Mass System Meriam & Kraige, Dynamics: 8/2 The classic spring-mass-damper system oscillates around its rest configuration. k m c x Its equation of motion is given by: mẍ = −c ẋ − k x where c is the damping coefficient and k is the spring stiffness. In second-year vibrations units you will solve these equations analytically, and study the response under an applied cyclical load. Here we solve the problem numerically. The state space equations are given as: x p= ẋ dp = dt ẋ k −m x− c m ẋ This is solved numerically with initial conditions: at t = 0, x = x0 and ẋ = 0. Here is shown a specific example with k/m = 0.5 and c/m = 0.2. The mass oscillates with a decaying amplitude due to the energy dissipated in the damper. 0.5 0.4 0.3 0.2 x (m) 0.1 0 −0.1 −0.2 −0.3 −0.4 −0.5 0 5 10 15 t (s) 3 20 25 Mechanics: Numerical Integration MS - 2022/2023 This example also demonstrates the pitfalls of using the Forward Euler numerical integration scheme. In the previous example, h = 0.1 gave a reasonable approximation compared to the analytical solution; for this system, the numerical solution will deviate significantly over time as the integration scheme becomes unstable. The step size has to be reduced by an order of magnitude to h = 0.01 to achieve a reasonable accuracy — this means that the numerical solution can be unreliable! Forward Euler - n = 2500 - h = 0.01 0.5 0.4 0.4 0.3 0.3 0.2 0.2 0.1 0.1 x (m) x (m) Forward Euler - n = 250 - h = 0.1 0.5 0 0 -0.1 -0.1 -0.2 -0.2 -0.3 -0.3 -0.4 -0.4 numerical analytical -0.5 numerical analytical -0.5 0 5 10 15 20 25 0 5 10 t (s) 20 25 20 25 RK4 - n = 50 - h = 0.5 0.5 0.4 0.4 0.3 0.3 0.2 0.2 0.1 0.1 x (m) x (m) Modified Euler - n = 126 - h = 0.2 0.5 0 0 -0.1 -0.1 -0.2 -0.2 -0.3 -0.3 -0.4 15 t (s) -0.4 numerical analytical -0.5 numerical analytical -0.5 0 5 10 15 20 25 0 t (s) 5 10 15 t (s) Alternative methods such as Modified Euler and Runge-Kutta 4 provide more accurate and stable solutions at larger step size, at the expense of more calculations per step. The Forward Euler scheme should therefore be avoided for any engineering calculations. 4 Mechanics: Numerical Integration 10.2 MS - 2022/2023 Modified Euler As seen in the previous analysis, the Forward Euler method requires a very small step size to achieve an acceptable result. As a first refinement, consider the trapezium rule used in numerical integration. f(a)·h f(a) + f(a+h) 2 f(x) f(x) h· x a x a+h a a+h For a function f (x) the integral between a and a + h is then approximated by: a+h Z f (x)dx ' h (f (a) + f (a + h)) 2 a Now consider an ODE where dx/dt = f (x, t) which is numerically integrated from t to t + h t+h Z x(t + h) = x(t) + f (x, t)dt t h ' x(t) + (f (x(t), t) + f (x(t + h), t + h)) 2 This is evaluated at discrete times tn and values xn , and therefore: xn+1 = xn + h (f (xn , tn ) + f (xn+1 , tn+1 )) 2 However, the value xn+1 is not known, and is therefore estimated as x̂n+1 using the Forward Euler method: h (f (xn , tn ) + f (xn+1 , tn+1 )) 2 h ' xn + (f (xn , tn ) + f (x̂n+1 , tn+1 )) 2 h ' xn + (f (xn , tn ) + f (xn + h f (xn , tn ), tn+1 ) 2 xn+1 = xn + This is known as the Modified Euler method. An alternative formulation is given by: xn+1 = xn + 1 (k1 + k2 ) 2 where k1 = h f (xn , tn ) k2 = h f (xn + k1 , tn+1 ) 5 Mechanics: Numerical Integration 10.3 MS - 2022/2023 Runge-Kutta Methods The Modified Euler integration is an example of a second-order Runge-Kutta scheme. These can be extended to higher orders and the most widely used version is the fourth-order Runge-Kutta (RK4) integrator: xn+1 = xn + 1 (k1 + 2 k2 + 2 k3 + k4 ) 6 where k1 = h f (xn , tn ) h k1 f (xn + , tn + h/2) 2 2 h k2 k3 = f (xn + , tn + h/2) 2 2 h k4 = f (xn + k3 , tn + h) 2 k2 = This numerical integration method gives a robust result for most ODEs (known as non-stiff equations), and is your first port of call for a numerical integrator. The Matlab implementation is known as ODE45 and is a variable time-step implementation of RK4. Example 10.3 – Projectile with Aerodynamic Drag Consider a projectile launched with a velocity v0 at an angle α to the horizontal. y x v Fdrag mg v0 α For the case of no aerodynamic drag, we can straightforwardly integrate the equations of motion: ẍ = 0 ẋ = cos α v0 x = cos α v0 t + x0 ÿ = −g ẏ = −g t + sin α vo 1 y = − g t2 + sin α v0 t + y0 2 The aerodynamic drag force is given by Fdrag as follows: Fdrag = 1 CD A ρ v 2 2 where CD is the drag coefficient, A the surface area, ρ the air density and v the velocity of the projectile. The drag force will be in opposite direction to the velocity vector and therefore: 1 v 2 Fdrag = − CD A ρ |v| 2 |v| 6 Mechanics: Numerical Integration MS - 2022/2023 which yields the following equations of motion: 1 1 ẍ 0 vx = CD A ρ |v| − ÿ −g vy 2 m To solve using our numerical integrators, rewrite in state space formulation: x y p= ẋ ẏ with ẋ dp ẏ = 1 − 2m CD Aρ |v| ẋ dt 1 −g − 2m CD Aρ |v| ẏ This can be solved for a given set of parameters, for instance a spherical projectile with m = 0.5 kg, radius r = 0.1 m, drag coefficient CD = 0.47, and initial velocity v0 = 15 m/s at 60◦ . 10 8 y 6 4 2 0 simulation analytical − no drag 0 2 4 6 8 10 x 12 14 16 18 Revision Objectives Handout 10: appreciate the need for numerical integration of ODEs recognise the impact of time step size for different numerical integration schemes avoid using Forward Euler where possible! 7 20