dynamical systems tutorial 20

22
DYNAMICAL SYSTEMS Tutorial 20 Choosing a Time Step sysid Mathematica 6.0.3, DynPac 11.01, 1ê13ê2009 intreset; plotreset; imsize = 250; In this tutorial, we take a practical look at choosing a time step for integration. ü Functions and Variables Used in This Tutorial asprat, boxrat, classify2D, eigsys, integrate, intreset, largeig, lastx, parmval, parmvec, phaser, phaser3D, plotreset, plrange, rangeflag, ranger, setcolor, setparm, setstate, slopevec, statevec, sugtimestep, sysid, sysname and timeplot. ü Description of Systems Used in This Tutorial We begin with an undamped linear oscillator for which all solutions are periodic. We look at the consequences of using too large a time step. We keep halving the time step until the results look reasonable. We show that the function sugtimestep suggests a time step comparable to the one we arrive at by repeated halving. Our next example is a linear system with two negative real eigenvalues. We show that the choice of time step is governed by the eigenvalue of largest modulus, corresponding to the shorter of the two intrinsic system time scales. We see that the situation is somewhat more critical when the two eigenvalues are very different in size. After that we look at a nonlinear system for which linearization predicts a center at the origin. We use numerical work to determine whether the nearby orbits are indeed periodic, or spirals. Again we see that a time step too large gives results which are ambiguous. With a small enough time step, the numerical work shows clearly periodic solutions. As our final example, we look at the Lorenz equations and a limit cycle solution which occurs for certain parameter values. For a time step too large, the limit cycle is blurred, and, given that the Lorenz equations have chaotic solutions for other parameter values, we might erroneously conclude that we had found a chaotic attractor. With a sufficiently small time step, the limit cycle emerges clearly from the numerical work. ü Linear Oscillator We start with an autonomous linear oscillator with natural frequency w. The differential equations are x ° = y, y ° = -w 2 x . step.nb 1

Upload: others

Post on 25-Apr-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DYNAMICAL SYSTEMS Tutorial 20

DYNAMICAL SYSTEMSTutorial 20

Choosing a Time Step

sysid

Mathematica 6.0.3, DynPac 11.01, 1ê13ê2009

intreset; plotreset; imsize = 250;

In this tutorial, we take a practical look at choosing a time step for integration.

ü Functions and Variables Used in This Tutorial

asprat, boxrat, classify2D, eigsys, integrate, intreset, largeig, lastx, parmval, parmvec, phaser, phaser3D, plotreset, plrange, rangeflag, ranger, setcolor, setparm, setstate, slopevec, statevec, sugtimestep, sysid, sysname and timeplot.

ü Description of Systems Used in This Tutorial

We begin with an undamped linear oscillator for which all solutions are periodic. We look at the consequences of using too large a time step. We keep halving the time step until the results look reasonable. We show that the function sugtimestep suggests a time step comparable to the one we arrive at by repeated halving. Our next example is a linear system with two negative real eigenvalues. We show that the choice of time step is governed by the eigenvalue of largest modulus, corresponding to the shorter of the two intrinsic system time scales. We see that the situation is somewhat more critical when the two eigenvalues are very different in size. After that we look at a nonlinear system for which linearization predicts a center at the origin. We use numerical work to determine whether the nearby orbits are indeed periodic, or spirals. Again we see that a time step too large gives results which are ambiguous. With a small enough time step, the numerical work shows clearly periodic solutions. As our final example, we look at the Lorenz equations and a limit cycle solution which occurs for certain parameter values. For a time step too large, the limit cycle is blurred, and, given that the Lorenz equations have chaotic solutions for other parameter values, we might erroneously conclude that we had found a chaotic attractor. With a sufficiently small time step, the limit cycle emerges clearly from the numerical work.

ü Linear Oscillator

We start with an autonomous linear oscillator with natural frequency w. The differential equations are

x° = y, y° = -w2x .

We define this for DynPac and name it LinearOsc.

step.nb 1

Page 2: DYNAMICAL SYSTEMS Tutorial 20

We define this for DynPac and name it LinearOsc.

setstate@8x, y<D; setparm@8w<D; slopevec = 9y, -w2 x=; sysname = "LinearOsc";

From our previous study of this system, we know that all solutions are periodic with angular frequency w, and the orbits in phase space are the contours of constant energy, namely

12 y2+ 12w

2x2= constant .

We set the value of w to 3 and do a numerical integration, starting from the initial point x = 1, y = 0 at the initial time zero.

parmval = 83<;

init = 81, 0<; t0 = 0;

We use a time step of 0.5, and we integrate for 100 steps.

h = 0.5; nsteps = 100;

sol1 = integrate@init, t0, h, nstepsD;

We plot the solution.

plrange = 88-1.5, 1.5<, 8-3.5, 3.5<<;

asprat = 1.0;

phaser@sol1D

-1.5 -1.0 -0.5 0.5 1.0 1.5x

-3

-2

-1

1

2

3

yLinearOsc 8w< = 83<

This doesn't look like anything we wanted to get! The picture is telling us in several ways that our time step is too large. First by the very prominent discontinuities in slope, and second by the fact that the orbit does not close, and instead spirals in to the origin. We can fix the problem by reducing the time step until the undesirable features go away. Because the error in the fourth order Runge-Kutta used varies with the time step h like h4, we can get considerable improvement with modest decreases in step. We try the integration again, this time with a step of 0.25 instead of 0.5. We also double the number of steps, so that we are continuing to integrate over the same time period.

step.nb 2

Page 3: DYNAMICAL SYSTEMS Tutorial 20

This doesn't look like anything we wanted to get! The picture is telling us in several ways that our time step is too large. First by the very prominent discontinuities in slope, and second by the fact that the orbit does not close, and instead spirals in to the origin. We can fix the problem by reducing the time step until the undesirable features go away. Because the error in the fourth order Runge-Kutta used varies with the time step h like h4, we can get considerable improvement with modest decreases in step. We try the integration again, this time with a step of 0.25 instead of 0.5. We also double the number of steps, so that we are continuing to integrate over the same time period.

h = hê20.25

nsteps = 2*nsteps

200

sol2 = integrate@init, t0, h, nstepsD;

phaser@sol2D

-1.5 -1.0 -0.5 0.5 1.0 1.5x

-3

-2

-1

1

2

3

yLinearOsc 8w< = 83<

This is an improvement, but it is still not good. We halve the time step again.

h = hê20.125

nsteps = 2*nsteps

400

sol3 = integrate@init, t0, h, nstepsD;

step.nb 3

Page 4: DYNAMICAL SYSTEMS Tutorial 20

phaser@sol3D

-1.5 -1.0 -0.5 0.5 1.0 1.5x

-3

-2

-1

1

2

3

yLinearOsc 8w< = 83<

Again an improvement, but the thickening of the curve suggests that there is still a problem. We look at the first and last points, and check to see whether they are on the same energy curve. We first define the energy function so that we may evaluate it.

energy@vec_D := Module@8vel, dis, freq<, freq = First@parmvalD;dis = First@vecD; vel = Last@vecD; 0.5*Hvel^2 + Hfreq*disL^2LD

first = init

81, 0<

[email protected]

last = lastx

80.677109, 2.17673<

[email protected]

We see that there is between 1% and 2% loss of energy in a time equal to

nsteps*h

50.

One more halving of the time step should do it.

step.nb 4

Page 5: DYNAMICAL SYSTEMS Tutorial 20

h = hê20.0625

nsteps = 2*nsteps

800

sol4 = integrate@init, t0, h, nstepsD;

phaser@sol4D

-1.5 -1.0 -0.5 0.5 1.0 1.5x

-3

-2

-1

1

2

3

yLinearOsc 8w< = 83<

This is still slightly thick, so we check the energy.

[email protected]

[email protected]

Now the error is quite small. We continue our experiment by once more halving the time step and doubling the number of steps.

h = hê20.03125

nsteps = 2*nsteps

1600

step.nb 5

Page 6: DYNAMICAL SYSTEMS Tutorial 20

sol5 = integrate@init, t0, h, nstepsD;

phaser@sol5D

-1.5 -1.0 -0.5 0.5 1.0 1.5x

-3

-2

-1

1

2

3

yLinearOsc 8w< = 83<

[email protected]

[email protected]

While we have solved the problem, we haven't used much insight to do it. By using what we know about the solution, we can make a much better guess for the time step before doing any integrations. The motion is periodic with angular frequency w, so that the period is 2p/w. Because x goes through its full range of variation in a half period, we may take a half period to be the characteristic time scale for the system -- namely

N@p êw ê. Thread@parmvec -> parmvalDD1.0472

The basic question: In order to resolve the variations of x with t, what fraction of this basic scale should the time step be? Of course the answer will depend on the accuracy we seek in the calculation. As a general rule, though, we can expect excellent graphical accuracy if we use a step that is 1/100 of the basic time scale of the system. Thus we choose

h = p êH100. wL0.0314159

w

step.nb 6

Page 7: DYNAMICAL SYSTEMS Tutorial 20

% ê. Thread@parmvec -> parmvalD0.010472

Our previous integrations were for a total time of 50, so we continue with that interval.

nsteps = 50êh1591.55 w

We do one last integration with this choice.

sol6 = integrate@init, t0, h, nstepsD;

phaser@sol6D

-1.5 -1.0 -0.5 0.5 1.0 1.5x

-3

-2

-1

1

2

3

yLinearOsc 8w< = 83<

Now even the slight thickening that we saw before is totally gone. We check the energy.

[email protected]

[email protected]

This is good empirical support for our rule of dividing the basic time scale of the system by 100. We may compare all of this with the result from the DynPac function sugtimestep, which takes as an argument a window in the phase plane, and returns a suggested time step.

step.nb 7

Page 8: DYNAMICAL SYSTEMS Tutorial 20

plrange

88-1.5, 1.5<, 8-3.5, 3.5<<

[email protected]

The suggested time step, although a little conservative (it is about 2/3 as large as the step we finally used), could have saved us a lot of work! The suggested step is calculated as 1/(50 * lam), where lam is the estimated rms value over the window of the modulus of the largest eigenvalue.

ü A Non-Oscillating Linear System

We start with a new system, a linear system with two real eigenvalues. The system is

x° = y , y° = -ax -(a + 1) y .

We define the system for DynPac.

setstate@8x, y<D; setparm@8a<D; sysname = "Linear2"; slopevec = 8y, -a x - Ha + 1L y<;

We find the eigenvalues for general a.

eigsys@80, 0<D

:8-1, -a<, :8-1, 1<, :- 1a, 1>>>

The eigenvalues are -1 and -a, so for positive a this is a stable node. Let's look at the analytical solution for x[0]=1,y[0]=1. We get

8anx@t_D, any@t_D< = FullSimplify@8x@tD, y@tD< ê. Flatten@DSolve@8x'@tD == y@tD, y'@tD == -a x@tD - Ha + 1L y@tD, x@0D == 1, y@0D == 1<, 8x, y<, tDDD

:‰-H1+aL t I-2 ‰t + H1 + aL ‰a tM

-1 + a,

H1 + aL ‰-t - 2 a ‰-a t

1 - a>

If 0 < a < 1, the shortest characteristic time for this system is the reciprocal of the largest eigenvalue, and hence is 1. For a > 1, the shortest characteristic time is 1/a. We begin by deliberately choosing a time step too large, just to see what the effect is on the solution. First we set a = 2.

parmval = 82<;

Now we set the time step to 0.5, which is the characteristic time. We integrate out to t = 6.

init = 81, 1<; t0 = 0.0; h = 0.5; nsteps = 12;

sol7 = integrate@init, t0, h, nstepsD;

plrange = 88-1.5, 1.5<, 8-1.5, 1.5<<;

step.nb 8

Page 9: DYNAMICAL SYSTEMS Tutorial 20

phaser@sol7D

-1.5 -1.0 -0.5 0.5 1.0 1.5x

-1.5

-1.0

-0.5

0.5

1.0

1.5y

Linear2 8a< = 82<

The curve is jagged, indicating a time step that is too large. We look at the solution at t = 1:

sol7@@3DD81., 0.823263, -0.542013<

Now we look at the analytical solution for the same value of t.

8anx@tD, any@tD< ê. a -> 2 ê. t -> 1.0

80.832968, -0.562297<

The error is about 4% in y. From the graph, one might have expected an even larger error. A reasonable time step for this system would be on the order of 1/50 times the characteristic time, or

h = 1êH50.*aL0.02

a

% ê. Thread@parmvec -> parmvalD0.01

We integrate out to t = 4 by choosing nsteps = 4/h.

nsteps = 4êh200. a

step.nb 9

Page 10: DYNAMICAL SYSTEMS Tutorial 20

% ê. Thread@parmvec -> parmvalD400.

sol8 = integrate@init, t0, h, nstepsD;

phaser@sol8D

-1.5 -1.0 -0.5 0.5 1.0 1.5x

-1.5

-1.0

-0.5

0.5

1.0

1.5y

Linear2 8a< = 82<

We check the value at t = 1.

sol8@@101DD81., 0.832968, -0.562297<

8anx@tD, any@tD< ê. a -> 2 ê. t -> 1.0

80.832968, -0.562297<

Complete agreement to six places. Now we ask DynPac to suggest a timestep:

[email protected]

Exactly what we used.

Now we make the situation more difficult numerically by taking a to be large. We will take a = 10, and we will look at the time plots of the solution in a range near the initial value.

parmval = 810<;

We begin by deliberately choosing an inappropriately large time step, equal to the characteristic time 1/a = 0.1. We again integrate out to t = 4.

step.nb 10

Page 11: DYNAMICAL SYSTEMS Tutorial 20

We begin by deliberately choosing an inappropriately large time step, equal to the characteristic time 1/a = 0.1. We again integrate out to t = 4.

h = 0.1; nsteps = 40;

sol9 = integrate@init, t0, h, nstepsD;

phaser@sol9D

-1.5 -1.0 -0.5 0.5 1.0 1.5x

-1.5

-1.0

-0.5

0.5

1.0

1.5y

Linear2 8a< = 810<

The curve is a little jagged. We do a time plot for the first 0.5 seconds.

plrange = 880, 0.5<, 8-1.5, 1.5<<;

step.nb 11

Page 12: DYNAMICAL SYSTEMS Tutorial 20

timeplot@sol9, 81, 2<D

0.1 0.2 0.3 0.4 0.5t

-1.5

-1.0

-0.5

0.0

0.5

1.0

1.58x, y<

Linear2 8a< = 810<

The result is particularly poor for y (the lower curve). An appropriate time step would be more like 1/(50*a) = 0.002. DynPac says the same:

[email protected], 1.5<, 8-1, 5, 1.5<<D0.002

h = 0.002; nsteps = 2000;

sol10 = integrate@init, t0, h, nstepsD;

step.nb 12

Page 13: DYNAMICAL SYSTEMS Tutorial 20

timeplot@sol10, 81, 2<D

0.1 0.2 0.3 0.4 0.5t

-1.5

-1.0

-0.5

0.0

0.5

1.0

1.58x, y<

Linear2 8a< = 810<

This is much better. Let's look at the full range of time out to t = 4.

plrange = 880, 4<, 8-1.5, 1.5<<;

timeplot@sol10, 81, 2<D

1 2 3 4t

-1.5

-1.0

-0.5

0.0

0.5

1.0

1.58x, y<

Linear2 8a< = 810<

The shortest time scale of the system is associated with the modulus of the largest eigenvalue. The function largeig[point] returns that value at the location point.

step.nb 13

Page 14: DYNAMICAL SYSTEMS Tutorial 20

largeig@80, 0<D10

This function is used by the function sugtimstep to find a suggested time step.

This system has two very different time scales: 1, and 1/a = 0.1. If we make a even larger, the scales become even more disparate. Such equations are called stiff equations, and they present numerical difficulties. Basically in order to resolve the events on the short time scale, we need to choose h to be of the order of (1/50)*(short scale) = 1/(50*a). In order to run the integration long enough to see the final decay come close to the equilibrium at the origin, we need to integrate out to say t = 5. Thus the number of steps required is 5/(1/(50*a)) = 250 a. The larger a is, the more steps we require to see the whole process. From the graph, it appears that the short time scale is important only near the initial time. A full exploitation of that remark leads to two-scale perturbation techniques. Alternatively, one may use special numerical integration routines designed just for such stiff equations.

ü Center or Spiral?

We consider now a nonlinear system in which the origin is a linear center, and our objective is to determine whether the nearby orbits are nonlinear spirals which head for the origin or are periodic. This example is a modified version of a problem from Nonlinear Ordinary Differential Equations, 2nd edition, Jordan and Smith, Oxford Press, 1987, p. 305. The equation is

x– + x° x + x + x3= 0 .

We introduce y = x° and define this as a system for DynPac.

setstate@8x, y<D; setparm@8<D; slopevec = 8y, -x y - x - x^3<; sysname = "Nonlinear1";

We begin by classifying the equilibrium at the origin.

classify2D@80, 0<DAbbreviations used in classify2D.

L = linear, NL = nonlinear, R2 = repeated root.

Z1 = one zero root, Z2 = two zero roots.

This message printed once.

stable HLL, indeterminate HNLL - center

We try an integration starting at x = 1, y = 0. We try a time step of 1.0 and we integrate out to t = 50.

init = 81, 0<; t0 = 0.0; h = 1; nsteps = 50;

sol11 = integrate@init, t0, h, nstepsD;

plrange = 88-1.75, 1.75<, 8-1.75, 1.75<<;

asprat = 1.0;

step.nb 14

Page 15: DYNAMICAL SYSTEMS Tutorial 20

phaser@sol11D

-1.5 -1.0 -0.5 0.5 1.0 1.5x

-1.5

-1.0

-0.5

0.5

1.0

1.5

yNonlinear1

Although the orbit appears to be spiraling in to the origin, the curve is so jagged that we cannot trust that conclusion. We have used far too large a time step. Let's halve the step and try again.

h = hê2; nsteps = 2*nsteps;

sol12 = integrate@init, t0, h, nstepsD;

step.nb 15

Page 16: DYNAMICAL SYSTEMS Tutorial 20

phaser@sol12D

-1.5 -1.0 -0.5 0.5 1.0 1.5x

-1.5

-1.0

-0.5

0.5

1.0

1.5

yNonlinear1

This is somewhat better, although there are still problems. It is starting to look more like a periodic solution than a spiral, but we really need a smaller step. We let DynPac suggest a step.

[email protected]

We round this to 0.01 and integrate out to t = 50.

h = 0.01; nsteps = 5000;

sol13 = integrate@init, t0, h, nstepsD;

step.nb 16

Page 17: DYNAMICAL SYSTEMS Tutorial 20

phaser@sol13D

-1.5 -1.0 -0.5 0.5 1.0 1.5x

-1.5

-1.0

-0.5

0.5

1.0

1.5

yNonlinear1

This is pretty clearly periodic, in spite of the false impression from the earlier integration. Indeed using Theorem 11.3 in Jordan and Smith, one may prove that all of the solutions of this equation are periodic.

ü A Limit Cycle in 3D

As our final example, we consider the Lorenz equations. They are given by

x° = s(y - x) , y° = rx - y -xz , z° = xy - bz .

Here the parameters are s, b, and r. We define the system for DynPac.

setstate@8x, y, z<D; setparm@8s, b, r<D;slopevec = 8s *Hy - xL, r*x - y - x*z, x*y - b*z<; sysname = "Lorenz";

These equations are known to have chaotic solutions for certain parameter values -- for example, for s = 10, b = 8/3, and r = 28. We will look at a different part of parameter space. We choose s=10, b = 8/3, and r = 100:

parmval = 810, 8ê3, 100<;

We carry out an integration.

initvec = 810.0, 10.0, 10.0<; t0 = 0.0; h = 0.1; nsteps = 100;

step.nb 17

Page 18: DYNAMICAL SYSTEMS Tutorial 20

lorsol1 = integrate@initvec, t0, h, nstepsD;

General::ovfl : Overflow occurred in computation. à

General::ovfl : Overflow occurred in computation. à

We get an overflow, suggesting that the solution may not be bounded (this turns out to be a false conclusion). We set range checking to prevent the overflow.

rangeflag = True; ranger = 88-100, 100<, 8-100, 100<, 80, 200<<;

We try the integration again.

lorsol2 = integrate@initvec, t0, h, nstepsD;

We plot the solution.

boxrat = 81, 1, 1<; setcolor@8TurquoiseBlue<D;

phaser3D@lorsol2D

Lorenz 8s, b, r< = :10, 83, 100>

2040

60x

20

40

60

80

y

50

100

150

z

The solution left the box, suggesting again that it might be unbounded (same false conclusion). Let's reduce the time step and see what happens.

h = hê2; nsteps = 2*nsteps;

lorsol3 = integrate@initvec, t0, h, nstepsD;

step.nb 18

Page 19: DYNAMICAL SYSTEMS Tutorial 20

phaser3D@lorsol3D

Lorenz 8s, b, r< = :10, 83, 100>

-200

2040x

-50

0

50

y

50

100

150

z

A VERY different result. Knowing that the Lorenz equation has chaotic solutions, we might suspect that we are seeing one (another false conclusion). The jagged nature of the orbit tells us that the time step is still too large, and we shouldn't trust the conclusions. We halve the time step again. We also turn off range checking to speed up the integration.

rangeflag = False;

h = hê20.025

nsteps = 2*nsteps

400

lorsol4 = integrate@initvec, t0, h, nstepsD;

step.nb 19

Page 20: DYNAMICAL SYSTEMS Tutorial 20

phaser3D@lorsol4D

Lorenz 8s, b, r< = :10, 83, 100>

-200

2040x -50

0

50

y

50

100

150

z

It is looking less chaotic. There might even be a limit cycle in there somewhere. We ask DynPac for a suggested time step, estimating a window on what we see above.

window = 88-40, 40<, 8-60, 60<, 80, 175<<;

sugtimestep@windowD

N::meprec :Internal precision limit $MaxExtraPrecision = 50.` reached while evaluating

-Abs@Root@112080+ 1918 Slot@á1àD+ 41 Power@á2àD+ 3 Power@á2àD &, 2,0DD+AbsARootA112080+ 1918Ò1+ 41á1à2 + 3 Slot@á1àD3 &, 3, 0EE.

à

N::meprec :Internal precision limit $MaxExtraPrecision = 50.` reached while evaluating

-Abs@Root@112080+ 1918 Slot@á1àD+ 41 Power@á2àD+ 3 Power@á2àD &, 2,0DD+AbsARootA112080+ 1918Ò1+ 41á1à2 + 3 Slot@á1àD3 &, 3, 0EE.

à

0.000648477

That looks fairly small. We try h = 0.001.

step.nb 20

Page 21: DYNAMICAL SYSTEMS Tutorial 20

That looks fairly small. We try h = 0.001.

h = 0.001; nsteps = 10000;

lorsol5 = integrate@initvec, t0, h, nstepsD;

phaser3D@lorsol5D

Lorenz 8s, b, r< = :10, 83, 100>

-200

2040x -50

0

50

y

50

100

150

z

To see is we are on a limit cycle, we begin the integration again, starting with the last point calculated. We reduce the number of steps to 5000.

lorsol6 = integrate@lastx, t0, h, 5000D;

step.nb 21

Page 22: DYNAMICAL SYSTEMS Tutorial 20

phaser3D@lorsol6D

Lorenz 8s, b, r< = :10, 83, 100>

-200

20x-2002040

y

60

80

100

120

140

z

There is our limit cycle -- exotic but cleanly represented with the time step 0.001. This is a known attractor for the Lorenz equations for these parameter values.

step.nb 22