CL8 - Dynamical Systems

.pdf

School

University of Illinois, Urbana Champaign *

*We aren’t endorsed by this school

Course

257

Subject

Mathematics

Date

Apr 3, 2024

Type

pdf

Pages

14

Uploaded by CoachGuanaco4012 on coursehero.com

12/13/22, 12:11 AM Dynamical Systems https://www.prairielearn.org/pl/workspace/601856 1/14 Matplotlib created a temporary config/cache directory at /tmp/matplotlib-mmlk8p90 becaus e the default path (/tmp/cache/matplotlib) is not a writable directory; it is highly rec ommended to set the MPLCONFIGDIR environment variable to a writable directory, in partic ular to speed up the import of Matplotlib and to better support multiprocessing. Lesson 8: Dynamical Systems 1) Introduction to Dynamical Systems In physics and many other natural sciences, various phenomena are described by what are called dynamical systems , which describe how systems may change with respect to time. These can model things like the swinging of a clock pendulum, the path of a projectile in the air, or the population of predators and prey in an area. One important thing to note is that dynamical systems are modeled by differential equations , in that we know how the function changes with respect to time, but we do not know the function itself. We are therefore tasked with integrating the function to get a solution. Here we will start with a simple example function that constantly grows at a rate of $3$ units per period of time, and starts with a value of $0$ at $t=0$. Or more formally: $$ \begin{eqnarray} \frac{dx}{dt} = 3 \quad \Leftrightarrow \quad x^{\prime}(t) &=& 3 \\ x(0) &=& 0 \end{eqnarray} $$ With careful inspection, it may be obvious to you what the solution to this equation should look like — but we will explore how to calculate this numerically. Say we would like to see how the function looks from values $t=0$ to $t=5$. We will break this up into a series of $n$ time steps and compute each value sequentially: $$ x_i = x_{i-1} + x^\prime \Delta t $$ You can imagine this as taking small incremental steps at each time, with the direction of the step being given by the slope at that point. This is called Euler's method , and can be used with reasonable accuracy in simple equations. In [1]: import numpy as np import numpy.linalg as la import matplotlib.pyplot as plt import helper In [2]: n = 100 t = np . linspace ( 0 , 5 , n ) x = np . zeros ( n ) x [ 0 ] = 0 # Starting value for i in range ( 1 , n ): dt = t [ i ] - t [ i - 1 ] dx = 3 x [ i ] = x [ i - 1 ] + dx * dt
12/13/22, 12:11 AM Dynamical Systems https://www.prairielearn.org/pl/workspace/601856 2/14 As you may have expected, the above gives us a straight line with slope $3$ starting at the origin. We can get an infinite number of solutions by varying this starting point. Below is a plot of various solutions with different starting values as well as the slope at each point in space/time, this is called a phase portrait and we will examine some more interesting ones later. fig = plt . figure ( figsize = ( 5 , 8 )) ax = fig . add_subplot ( 111 ) ax . plot ( t , x , 'o' ) plt . xlabel ( "t" ) plt . ylabel ( "x" ) plt . xlim ( 0 , 10 ) plt . ylim ( 0 , 10 ) ax . set_aspect ( 'equal' ) In [3]: plt . figure ( figsize = ( 7 , 7 )) plt . xlim ( - 1 , 1 ) plt . ylim ( - 1 , 1 ) plt . xlabel ( "t" ) plt . ylabel ( "x" ) helper . plot_vecfield_constant ([ - 1 , 1 ], [ - 1 , 1 ], 30 , dx ) x = np . linspace ( - 1 , 1 , 2 ) for b in np . linspace ( - 3 , 3 , 10 ): plt . plot ( x , x * 3 + b , linewidth = 3 )
12/13/22, 12:11 AM Dynamical Systems https://www.prairielearn.org/pl/workspace/601856 3/14 Let's try a more interesting equation. Suppose the slope is related to the actual value of $x$ itself (remember that the independent variable is time $t$): $$ \begin{eqnarray} x^{\prime}(t) &=& -2 \,x(t) \\ x(0) &=& 1 \end{eqnarray} $$ Before you proceed, briefly discuss what you think this will look like with your group . What sort of function has a derivative that is proportional to function itself? We will use a helper function helper.solve_ode to integrate the function values, but it is essentially doing the same as what you have seen above. [<matplotlib.lines.Line2D at 0x7fa600923850>] In [4]: t = np . linspace ( 0 , 10 , 100 ) x = helper . solve_ode ( t , np . array ([ - 2.0 ]), np . array ([ 1.0 ]))[ 0 ] plt . figure ( figsize = ( 7 , 7 )) plt . xlabel ( "t" ) plt . ylabel ( "x" ) plt . plot ( t , x ) Out[4]:
12/13/22, 12:11 AM Dynamical Systems https://www.prairielearn.org/pl/workspace/601856 4/14 As you can see, this looks rather like an exponential function. We can try to fit an exponential to the values to confirm this. x = e^-1.9999671364359206t residual: 2.413698767852594e-28 <matplotlib.legend.Legend at 0x7fa60096cbe0> In [5]: soln , residuals , rank , s = la . lstsq ( t . reshape (( len ( t ), 1 )), np . log ( x ), rcond = None ) print ( f'x = e^{ soln [ 0 ] }t' ) print ( 'residual:' , residuals [ 0 ]) In [6]: plt . figure ( figsize = ( 7 , 7 )) plt . xlabel ( "t" ) plt . ylabel ( "x" ) plt . plot ( t , x , label = "Integrated Function" ) plt . plot ( t , np . exp ( t * soln [ 0 ]), 'o' , label = "Exponential Fit" ) plt . legend () Out[6]:
12/13/22, 12:11 AM Dynamical Systems https://www.prairielearn.org/pl/workspace/601856 5/14 Lets explore the phase portrait — this time we can see over time that solutions will converge to the same point $(0)$. In [7]: plt . figure ( figsize = ( 7 , 7 )) plt . xlim ( - 1 , 1 ) plt . ylim ( - 1 , 1 ) helper . plot_vecfield_linear ([ - 1 , 1 ], [ - 1 , 1 ], 30 , - 1 ) x = np . linspace ( - 1 , 1 , 100 ) for b in np . linspace ( - 1 , 1 , 7 ): y = helper . solve_ode ( x , np . array ([ - 1.0 ]), np . array ([ b ]))[ 0 ] plt . plot ( x , y , linewidth = 3 ) plt . xlabel ( "t" ) plt . ylabel ( "x" ) plt . show ()
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help