ENGN2219/COMP6719 Computer Architecture & Simulation Ramesh Sankaranarayana Semester 1, 2020 1
Week 8: Simulation - Dynamical Systems 2
An informal de�nition A dynamical system is one whose state changes over time The state space may be discrete or continuous, �nite or in�nite Time may be continuous or discrete Represented using di�ferential equations or di�ference equations Use analytical or numerical simulation approaches to study the system 3
Simple Pendulum 4
Lorenz Attractor 5
Bouncing Ball 6
Magnetohydrodynamic Flow Simulation 7
Random Walk A random walk describes a walk in which each step is chosen by a random mechanism, independent of any previous step. Used to model a number of di�ferent phenomena, such as: Random movement of molecules in liquids and gases Share prices Exchange rates Search path of bacteria The size of the Web Gambling 8
One dimensional random walk Walk along one axis Steps of equal size, forwards or backwards 9
Two dimensional random walk Walk along two axes Steps of equal size, in one of four directions 10
Interpolation Given a set of discrete data points, (which might represent some unknown function), determine the value at some point between already existing data points. linear interpolation spline interpolation 11
Linear interpolation 12
Cubic spline interpolation 13
Extrapolation In extrapolation, we would like to estimate the value at a point outside of the existing data points. This comes with a certain amount of uncertainty and the results are less meaningful. Keep in mind that the resulting value may not be accurate. 14
Linear extrapolation 15
Cubic spline extrapolation 16
Regression Assume that we are given a set of data points and we want to determine the value of the unknown function that these points represent, at a point within the range de�ned by these points. However, the measurement of the data has introduced some noise and therefore the date is imprecise. In such cases, we try and �t a polynomial function to the data. The choice of the polynomial must be made by the user. This approach is called regression. 17
Linear Regression In linear regression, we try and �t a linear equation to a set of data points. Given a bunch of data points, let’s estimate a best �t linear estimate. 18
Let’s now use the polyfit function to do a best �t for us. 19
So how do we decide which one is a better �t? Use some error measure A commonly used one is to minimize the error de�ned by the sum of the squared di�ferences between the actual data points and the estimates. ¯) 2 Σ 𝑜 𝑇𝑇𝐹 = 𝑗 =1 𝑦 𝑗 ( − 𝑦 If we do this in the above two cases, we get: Manual �t: 573 Poly�t: 356.82 20
Polynomial Regression Linear regression is a special case of polynomial regression, where you try and �t a polynomial of degree to the data, minimizing the error as computed by an 𝑜 appropriate method. 21
talk So which order polynomial would you use? Why? 22
Questions? 23
Numerical Integration Numerical integration generally involves evaluating the value of the integrand at a �nite set of points and using a weighted sum of these values to approximate the integral. The intent is to calculate the area under the curve. Two common techniques estimate the function with a set of piecewise linear or parabolic functions. 24
Using the trapz function Find the area of between and . 𝑧 = 𝑡𝑗𝑜 ( 𝑦 ) 0 2 𝜌 25
Using the quad function quad is a general purpose integration function. You can use dblquad and tplquad for double and triple integration, respectively. 26
Numerical Di�ferentiation Numerical di�ferentiation techniques estimate the derivative of a function at a point by approximating the slope of the tangent at , using values of the 𝑦 𝑙 𝑦 𝑙 function at points near . The approximation of the slope can be done in several 𝑦 𝑙 ways. Plot the derivate of between and . 𝑧 = 𝑡𝑗𝑜 ( 𝑦 ) 0 2 𝜌 27
Forward di�ference approximation The derivate at is estimated by computing the slope of the line between and 𝑦 𝑙 𝑦 𝑙 . 𝑦 𝑙 +1 28
Backward di�ference approximation The derivate at is estimated by computing the slope of the line between 𝑦 𝑙 𝑦 𝑙 − 1 and . 𝑦 𝑙 29
Central di�ference approximation The derivate at is estimated by computing the slope of the line between 𝑦 𝑙 𝑦 𝑙 − 1 and . In practice, you use the mid-points of each interval, to maximize usage of 𝑦 𝑙 +1 the given data. 30
Solving ordinary di�ferential equations You can use ode or odeint . odeint has a simpler interface and uses the lsoda algorithm. Note the the �rst two arguments for these are in opposite order. Use ode to select one of the many available integrators, not just lsoda . We will use odeint . scipy.integrate.odeint(func,y0,t,args) where func computes the derivative of at a speci�c time 𝑧 𝑢 0 y0 is the initial condition on 𝑧 t is an array. A sequence of time points to solve for 𝑧 args : tuple, optional. Extra arguments to pass to the function. 31
Example 1 Consider the system represented by the following ODE: 𝑒𝑧 = 2 𝑢 𝑒𝑢 or 𝑧 ′ = 2 𝑢 Solve for this in the interval . The initial boundary condition is . [ − 1, 1] 𝑧 ( − 1) = 1 32
33
Example 2 Consider the system represented by the following ODEs: 𝑒𝑧 = 𝑦 𝑒𝑢 𝑒𝑦 = − 𝑧 𝑒𝑢 If and , this simpli�es to: 𝑧 1 ≡ 𝑧 𝑧 2 ≡ 𝑦 𝑧 ′ = 𝑧 2 1 𝑧 ′ = − 𝑧 1 2 The interval is and the boundary condition is and . [ − 1, 1] 𝑧 1 ( − 1) = 1 𝑧 2 ( − 1) = 1 34
35
Example 3 - Damped simple harmonic motion Consider the second-order ODE representing a damped simple harmonic motion: 𝑧 ″ +2 𝑧 ′ + 2 𝑧 = 𝑑𝑝𝑡 (2 𝑦 ) with 𝑧 (0) = 0, 𝑧 ′ (0) = 0 We can turn this into two �rst-order equations by de�ning a variable . We 𝑨 ≡ 𝑧 ′ now have: 𝑨 ′ + 2 𝑨 + 2 𝑧 = 𝑑𝑝𝑡 (2 𝑦 ) with . 𝑧 (0) = 0, 𝑨 (0) = 0 36
37
Example 4 - Predator Prey System The following Lotka-Volterra equations describe the evolution of a predator-prey system: 𝑒𝑦 = 𝑦 ( 𝑏 − 𝑐𝑧 ) 𝑒𝑢 𝑒𝑧 = − 𝑧 ( 𝑑 − 𝑒𝑦 ) 𝑒𝑢 where represents the prey population (say rabbits ) and represents the predator 𝑦 𝑧 population (say foxes ). The derivatives are the growth rates. , , and are positive 𝑏 𝑐 𝑑 𝑒 constants describing the interaction of the predators and prey (assume these are all ). Let’s assume that there are 1.5 rabbits and 1 fox initially. 1 38
39
40
Questions? 41
Recommend
More recommend