+ All Categories
Home > Documents > Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions...

Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions...

Date post: 18-Sep-2020
Category:
Upload: others
View: 5 times
Download: 0 times
Share this document with a friend
58
Dieter Graß Numerical basics Solving IVPs Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions Continuation Solver Turning points Adaptive continuation Graphics Phase space Native MATLAB Overloaded OCMat Time path Native MATLAB Overloaded OCMat References OCMat functions 2.44 Lecture 2 : Basic numerical methods Doing the numerics! OCMat: A MATLAB package for the analysis of optimal control problems Dieter Graß: [email protected] Operations Research and Control Systems (ORCOS)
Transcript
Page 1: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.44

Lecture 2:

Basic numerical methodsDoing the numerics!

OCMat: A MATLAB package for the analysis ofoptimal control problems

Dieter Graß: [email protected] Research and Control Systems (ORCOS)

Page 2: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.45

Repeating of the model MoM

The model

maxν(·)

∫ ∞

0

e−rt(

x(t)2 + cν(t)2)

dt

x(t) = x(t)− x(t)3 + ν(t)

The canonical system

x(t) = x(t)− x(t)3 − ν◦(t)

ν◦(t) = maxν

H(x(t), u, λ(t)) ⇔ ν◦(t) = −1

2cλ(t)

λ(t) = rλ(t)− 2x(t)− λ(t)(

1− 3x(t)2)

Page 3: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.46

Solving initial value problems (IVPs)

We consider the following ODE

x(t) = f (x(t)), t ∈ [0,∞)

x(0) = x0 ∈ Rn

Remark

To numerically solve such an ODE there exist different solvers forMATLAB. I present the most general one ode45.

Example: The canonical system

f u n c t i o n out = momCanonicalSystem ( t , dynVar , para rg , a r c a r g )

r=pa r a r g (1 ) ;c=pa r a r g (2 ) ;T=pa ra r g (3 ) ;

u=momOptimalControl ( t , dynVar , para rg , a r c a r g ) ;out =[ [ dynVar ( 1 , : )−dynVar ( 1 , : ) .ˆ3+u ( 1 , : ) ] ; . . .

[ r .∗ dynVar ( 2 , : ) −2.∗dynVar ( 1 , : )−dynVar ( 2 , : ) .∗(1−3.∗ dynVar( 1 , : ) . ˆ 2 ) ] ] ;

Page 4: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.47

Solving IVPs (2)

Native MATLAB Syntax

>> par=m. o c I n s t a n t . ParameterValue ;>> opt=ode s e t ( ’ AbsTol ’ ,1 e−5, ’ R e l t o l ’ ,1 e−8) ;>> s o l=ode45 ( @momCanonicalSystem , [ 0 2 ] , [ 1 1 ] , opt , par , 1 )s o l =

s o l v e r : ’ ode45 ’x : [ 0 0 .2000 0 .3802 0 .5802 0 .7802 0 .9802 1 .1802

1 .3802 1 .5802 1 .7802 1 .9086 2 ]y : [ 2 x12 doub le ]

OCMat Syntax

>> opt=d e f a u l t o c o p t i o n s ;>> ocTr j=od e s o l v e (m, 2 , occu rve ( [ 1 1 ] ’ , 1 ) , opt )ocTr j =

o c t r a j e c t o r y o b j e c t :dynVar : [ 2 x12 doub le ]t : [ 1 x12 doub le ]a r c i d : 1

t i m e i n t e r v a l s : 2

Page 5: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.48

Solving boundary value problems (BVPs)

We consider the following ODE

x(t) = f (x(t)), t ∈ [0,T ]

b(x(0), x(T )) = 0 ∈ Rn

Remark

To numerically solve a BVP there exist different solvers forMATLAB (bvp4c,bvp5c).

Remark

For the numeric computation an initial solution has to be provided.

Remark

For an IVP the solution (at T = 0) is already given by the initialcondition.

Page 6: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.49

A boundary value problem (BVP)

Nerve impulse model

x1(t) = 3(

x1 + x2 − ax31 − b)

x2(t) = −c(x1(t) + d − ex2(t) t ∈ [0,T ]

x1(0) = x1(T ), x2(0) = x2(T )

The problem is now to find a periodic solution and the period T .

Time scaling and phase condition

x ′1(τ ) = 3T(

x1(τ ) + x2(τ ) + ax31 + b)

x ′2(τ ) = cT (x1(τ ) + d + ex2(τ ) t ∈ [0, 1]

x1(0) = x1(1), x2(0) = x2(1)

x ′2(0) = T (x1(0) + d + ex2(0)) = −1. (1)

Phase condition (1) necessary to specify a single point.

Page 7: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.50

Solving the BVP using MATLAB

MATLAB Syntax

f u n c t i o n dxdt=func ( t , x ,T, par )% ODE fun c i o ndxdt =[3∗T∗( x (1 )+x (2 )+par (1 ) ∗( x (1 ) ˆ3)+par (2 ) ) ;

par (3 )∗T∗( x (1 )+par (4 )+par (5 )∗x (2 ) ) ] ;

f u n c t i o n r e s=bc ( xa , xb ,T, par )% Boundary c o n d i t i o n sr e s =[xa (1 )−xb (1 )

xa (2 )−xb (2 )T∗par (3 ) ∗( xa (1 )+par (4 )+par (5 )∗xa (2 ) ) +1] ;

f u n c t i o n v=gues s ( t )% Guess f u n c t i o nv=[ s i n (2∗ p i ∗ t ) ; . . .

co s (2∗ p i ∗ t ) ] ;

>> s o l i n i t=b v p i n i t ( l i n s p a c e (0 , 1 , 5 ) , @guess ,2∗ p i ) ;>> opt=bvpse t ( ’ AbsTol ’ ,1 e−5) ;>> par=[−1/3 −1.3 −1/3 −0.7 0 . 8 ] ;>> s o l=bvp4c ( @func , @bc , s o l i n i t , opt , par ) ;

Page 8: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.51

Nerve impulse model

Page 9: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.52

Initialization of a boundary value solver

0 2 4 6 8 10−2

−1

0

1

2Solution detected

t

x1

0 2 4 6 8 10−1

−0.5

0

0.5

1

1.5

2

2.5Solution detected

t

x2

0 2 4 6 8 10−2

−1

0

1

2No solution detected

t

x1

0 2 4 6 8 10−0.5

0

0.5

1

1.5

2

2.5No solution detected

t

x2

Page 10: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.53

Implicit functions: Lemniscate I

Page 11: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.54

Implicit functions: Lemniscate II

Page 12: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.55

Theoretical background

Definition (Stable Manifolds)

Let x be an equilibrium, then the set

Wsloc (x) = {x ∈ Bε(x) : lim

t→∞x(t) = x , and x(0) = x ∈ Bε(x)}

is called the stable local manifold of x .The set

Ws(x) =⋃

t≥0

{x(−t) : x(0) ∈ Wsloc (x), t ≥ 0}

is called the global stable manifold.A trajectory x(·) with x(0) ∈ Ws(x) is called a stable path.

Theorem (Manifold Theorem)

Let J = fx (x), and the corresponding stable eigenspace

Es(x) := span{vj ∈ Rn : Re(ξj ) < 0, j = 1, . . . , n−}.

Then the local stable manifold Wsloc (x) exists with

TWsloc (x) = Es(x)

and

dimWsloc(x) = n−.

Page 13: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.56

Calculating a stable path by the IVP approach

Remark

For the initial value approach we use:

1 An initial point x0 ∈ Es(x) ∩ Bε is chosen.

2 The trajectory x(−t), 0 ≤ t ≤ T is calculated.

Page 14: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.57

Calculating a stable path by an IVP approach (2)

Native MATLAB Syntax

>> [ e i g v e c e i g v a l ]= e i g ( ocEP{1})e i g v e c =

0.6290 0.2952−0.7774 0.9554

e i g v a l =1.6180 0

0 −0.6180>> x0=1e−6∗e i g v e c ( : , 2 )+ocEP{1}. dynVar ( : , 1 ) ;>> opt=ode s e t ( ’ AbsTol ’ ,1 e−5, ’ Re lTo l ’ ,1 e−8, ’ Even t s ’ , ’ momEvents ’ ) ;>> s o l=ode45 ( @momCanonicalSystem , [ 0 −50] ,x0 , opt , par , 1 )s o l =

s o l v e r : ’ ode45 ’x : [ 1 x37 doub l e ]y : [ 2 x37 doub l e ]

xe : −26.7580

OCMat Syntax

>> opt=s e t o c o p t i o n s ( ’OC ’ , ’ L i n e a r i z a t i o nD i s t a n c e ’ ,1 e−6) ;>> opt=s e t o c o p t i o n s ( opt , ’ODE ’ , ’ AbsTol ’ ,1 e−5, ’ Re lTo l ’ ,1 e−8, ’ Even t s ’ , ’ on ’ ) ;>> ocAsym1=s t a b l e p a t h (m, ocEP{1} ,50 , opt )ocAsym1 =

oca s ymp to t i c o b j e c t :l i m i t s e t : [ d y n p r im i t i v e o b j e c t ]dynVar : [ 2 x37 doub l e ]t : [ 1 x37 doub l e ]a r c i d : 1t i m e i n t e r v a l s : 26 .758

>> opt=s e t o c o p t i o n s ( opt , ’OCCONT ’ , ’ Backward ’ , 1 ) ;>> ocAsym2=s t a b l e p a t h (m, ocEP{1} ,50 , opt ) ;>> m=s t o r e (m,{ocAsym1 , ocAsym2} , ’ Fie ldName ’ , ’ Stab lePath IVP ’ ) ;

Page 15: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.58

Calculating a stable path using the BVP approach

Remark

For the boundary value approach with x ∈ R2 we use.

1 The first coordinate of x0 is fixed.

2 The trajectory has to satisfy limt→∞ x(t) = x .

Page 16: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.59

Problems of the BVP approach

Remark

Two principle problems have to be resolved for a numericalcomputation

1 The convergence property limt→∞ x(t) = x has to bereplaced by some “finite” approximation.

2 An initial solution has to be provided.

Convergence condition

limt→∞

x(t) = x

Using continuation

First step:x (0)(·) ≡ x , t ∈ [0, T ]

Page 17: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.59

Problems of the BVP approach

Remark

Two principle problems have to be resolved for a numericalcomputation

1 The convergence property limt→∞ x(t) = x has to bereplaced by some “finite” approximation.

2 An initial solution has to be provided.

Asymptotic transversality condition

x(T ) ∈ TWsloc(x) = Es(x), T > 0 fixed

Using continuation

First step:x (0)(·) ≡ x , t ∈ [0, T ]

Page 18: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.59

Problems of the BVP approach

Remark

Two principle problems have to be resolved for a numericalcomputation

1 The convergence property limt→∞ x(t) = x has to bereplaced by some “finite” approximation.

2 An initial solution has to be provided.

Asymptotic transversality condition

Π (x(T )− x) = 0, Π ∈ R1×2

Using continuation

First step:x (0)(·) ≡ x , t ∈ [0, T ]

Page 19: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.59

Problems of the BVP approach

Remark

Two principle problems have to be resolved for a numericalcomputation

1 The convergence property limt→∞ x(t) = x has to bereplaced by some “finite” approximation.

2 An initial solution has to be provided.

Asymptotic transversality condition

Π (x(T )− x) = 0, Π ∈ R1×2

Using continuation

Step n: x (n)(·) solves

x(n)1 (0) = x1 + n

x0,1 − x1N

Π(

x (n)(T )− x)

= 0

Page 20: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.59

Problems of the BVP approach

Remark

Two principle problems have to be resolved for a numericalcomputation

1 The convergence property limt→∞ x(t) = x has to bereplaced by some “finite” approximation.

2 An initial solution has to be provided.

Asymptotic transversality condition

Π (x(T )− x) = 0, Π ∈ R1×2

Using continuation

Last step: x (N)(·) solves

x(N)1 (0) = x0

Π(

x (N)(T )− x)

= 0

Page 21: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.60

Calculating a stable path with OCMat

Main tool: occont

>> opt=s e t o c o p t i o n s ( ’OCCONT ’ , ’ I n i t S t epWid th ’ , 0 . 002 , ’ MaxStepWidth ’ , 0 . 0 1 ) ;>> i n i t S t r u c t=i n i t o c c o n t ( ’ e x t r ema l ’ ,m, ’ i n i t p o i n t ’ , 1 , 5 , ocEP{1}, ’ I n t e g r a t i o nT ime ’ , 500) ;>> s o l=occon t (m, i n i t S t r u c t , opt )

Con t i n ua t i on s t ep No . : 0s t epw id th : 0 . 002Mesh s i z e : 10I t e r a t i o n number : 12Residuum norm : 0R e l a t i v e d i s t a n c e : 1...Con t i n ua t i on s t ep No . : 104s t epw id th : 0 . 002Mesh s i z e : 32I t e r a t i o n number : 9Residuum norm : 3.97047 e−023R e l a t i v e d i s t a n c e : 0

s o l =oca s ymp to t i c o b j e c t :

l i m i t s e t : [ d y n p r im i t i v e o b j e c t ]o c t r a j e c t o r y :

dynVar : [ 2 x32 doub l e ]t : [ 1 x32 doub l e ]t i m e i n t e r v a l s : 500

>> m=s t o r e (m) ;>> m. o cRe s u l t sans =

Ex t r ema l S o l u t i o n : {[1 x1 oca s ymp to t i c ]}>> ocEx=e x t r ema l s o l (m)ocEx =

[1 x1 oca s ymp to t i c ]

Page 22: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.61

Calculating a stable path with OCMat (2)

Page 23: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.62

Implementation

Initialization

>> i n i t S t r u c t=i n i t o c c o n t ( ’ e x t r ema l ’ ,m, ’ i n i t p o i n t ’ , 1 , 5 , ocEP{1}, ’ I n t e g r a t i o nT ime ’ , 500)i n i t S t r u c t =

Asymptot icBCMatr ix : [ 0 . 9952 −0.0978]BVPFunction : [ 1 x1 s t r u c t ]

Cont inuat ionArgument : ’ e x t r ema l ’Con t inuat i onType : ’ f ’

Con t i n u a t i o nVa r i a b l e : ’ i n i t p o i n t ’I n i tBVSo l u t i o n : [ 1 x1 s t r u c t ]

I n t e g r a t i o nT ime : 500Mode lParameterValue : [ 0 . 5000 3 I n f ]

ODEFunction : [ 1 x1 s t r u c t ]PathType : ’ s ’

Targe tVa lue : 5>> i n i t S t r u c t . ODEFunctionans =

Dynamics : ’mom4Cont ’Gen e r a l : ’ d yn4 i 2 ep s ’

>> i n i t S t r u c t . BVPFunctionans =

Con t i nua t i on : ’ momBVP4Continuation ’A s ymp to t i cT ran s v e r s a l i t yCond : ’momBVP4AsymTransCond ’

Gene r a l : ’ b c4 i2ep ’>> i n i t S t r u c t . I n i tBVSo l u t i o nans =

y : [ 2 x10 doub l e ]x : [ 0 0 .1111 0.2222 0.3333 0.4444 0.5556 0.6667 0.7778 0.8889 1 ]

Page 24: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.63

Implementation: Files describing dynamics

General dynamics

f u n c t i o n out = dyn4 i 2 ep s ( t , dynVar , v a r a r g i n )%% DYN4I2EPS dynamics f o r BVP%i n t e r n a l p a r a r g = [ ] ;i f n a r g i n==3

i n t e r n a l p a r a r g=v a r a r g i n {1};end

% g l o b a l v a r i a b l eg l o b a l OcBVPVar

out=f e v a l (OcBVPVar . ODEFunction . Dynamics , t , dynVar , OcBVPVar . ModelParameterValue ,#,#,#,OcBVPVar . I n t eg r a t i onT ime ,#) ;

Model specific dynamics

f u n c t i o n out = mom4Cont ( t , dynVar , pararg , a r ca rg , sw i t c h i d , sw i t chcoord , t r un c t ,#)%% the dynamics f i l e f o r model mom f o r the BVP

% Dynamical sys tem

sw i t ch l e n g t h ( sw i t c h i d )case 0

out ( 1 : 2 , : )=t r u n c t∗momCanonicalSystem( t , dynVar ( 1 : 2 , : ) , pararg , a r c a r g (1 ) ) ;%ADDCASE

o t h e rw i s ee r r o r ( [ num2str ( l e n g t h ( sw i t c h i d ) ) ’ %s not d e f i n e d ’ ] , s w i t c h i d )

end

Page 25: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.64

Implementation: Files describing boundary conditions

General boundary condition file

f u n c t i o n out = bc4 i2ep ( dynVara , dynVarb , v a r a r g i n )%% BC4I2EP boundary c o n d i t i o n s f o r s o l u t i o n conv e r g i n g to an e q u i l i b r i um%

i n t e r n a l p a r a r g = [ ] ;i f n a r g i n==3

i n t e r n a l p a r a r g=v a r a r g i n {1};end

% g l o b a l v a r i a b l eg l o b a l OcBVPVarout=[ f e v a l (OcBVPVar . BVPFunction . Cont inuat i on , dynVara , OcBVPVar . Con t i n u a t i o nVa r i a b l e ,# ,

OcBVPVar . ActCont inuat ionType , OcBVPVar . Targe tCoord inate ,# ,OcBVPVar . I n i t i a l V a l u e ,#); . . .

f e v a l (OcBVPVar . BVPFunction . WeierstrassErdmannCond ,# , . . . ) ; . . .f e v a l (OcBVPVar . BVPFunction . Im p l i c i t C o n t r o l , # , . . . ) ; . . .f e v a l (OcBVPVar . BVPFunction . A s ymp to t i cT ran s v e r s a l i t yCond ,# , ’EP ’ ,OcBVPVar .

Asymptot icBCMatr ix , OcBVPVar . Sadd l ePo i n t ) ] ;

File for asymptotic boundary condition

f u n c t i o n out = momBVP4AsymTransCond ( dynVar , L im i t s e tType , Asymptot icBCMatr ix , Sadd l ePo i n t )%% the BC f i l e f o r the a s ymp to t i c t r a n s v e r s a l i t y c o n d i t i o ng l o b a l OcBVPVar

out = [ ] ;sw i t ch L im i t s e tType

case ’EP ’out=Asymptot icBCMatr ix ∗(dynVar−Sadd l ePo i n t ) ;

end

Page 26: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.65

Implementation: Files describing boundary conditions (2)

Model specific boundary condition for continuation

f u n c t i o n out = momBVP4Continuation ( dynVara , Cont inuat ionArgument , ActCont inuat ionType ,Targe tCoord inate ,#,#,#,#, I n i t i a l V a l u e ,#)

%% the BC f i l e r e t u r n i n g the c o n t i n u a t i o n res iduum

% g l o b a l v a r i a b l eg l o b a l OcBVPVar

out = [ ] ;

sw i t ch Cont inuat ionArgument (1 )case ’ i ’

c o n t r e s = [ ] ; % con t i n u a t i o n res iduump o s r e s = [ ] ; % po s i t i o n res iduumi f Ac tCont inuat i onType==0

po s r e s=dynVara ( Ta rge tCoo rd i n a t e )−I n i t i a l V a l u e ;end

endout=[ p o s r e s ; c o n t r e s ] ;

ContinuationArgument: continuation of the initial point, otherpossibilities are the time horizon, parameter, etc.

ActContinuationType: Fixed step width (0), linear adaptive (1),quadratic adaptive (2).

InitialValue: This value returns the actual initial state value and isgenerated during continuation in the function occont.

TargetCoordinate: Actual coordinate of the canonical system forcontinuation.

Page 27: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.66

Implementation: Continuation

Continuation command

f u n c t i o n v a r a r g ou t=occon t ( ocObj , I n i tBVSt ru c t , v a r a r g i n )% OCCONT con t i n u e an ex t r ema l s o l u t i o n o f an oc model% g l o b a l v a r i a b l eg l o b a l OcBVPVarOcBVPVar=I n i tBVS t r u c t ;b v s o l{3}= i n i t b v s o l ; % bv s o l{3} denotes the a c t u a l s o l u t i o nwh i l e c o n t f l a g

i f contnum > 1t r y

b v s o l p r e d i c t . x=b v s o l{3}. x ;b v s o l p r e d i c t . y = b v s o l{3}. y∗(1+a l p h a f a c ) − a l p h a f a c∗d e v a l ( b v s o l{2}, b v s o l{3}. x )

; % p r e d i c t e d s o l u t i o ne l s e

b v s o l p r e d i c t = b v s o l{3};s t epw id th=i n i t s t e p w i d t h ;

endt r y

s o l v e r% VIOLATION CHECK

endi f ˜ e r r o r f l a g % s o l u t i o n found

% SETTING STEP WIDTH, DETERMINE RELATIVE DISTANCE% STORING PREVIOUS RESULTS , HANDLE LAST STEPi n i t i a l p o i n t s =[ i n i t i a l p o i n t s [ b v s o l a c t u a l . y ( : , 1 ) ; a c t u a l r e l d i s t a n c e ] ] ;e ndpo i n t s =[ endpo i n t s [ b v s o l a c t u a l . y ( : , end ) ; a c t u a l r e l d i s t a n c e ] ] ;% PLOT ACTUAL SOLUTIONOcBVPVar . I n i t i a l V a l u e=OcBVPVar . S t a r tVa l u e+a c t u a l r e l d i s t a n c e∗OcBVPVar .

Con t i n ua t i onVec t o r ;% update c e l l o f s o l u t i o n sb v s o l ( 1 ) = [ ] ;b v s o l{3}=b v s o l a c t u a l ;

endcontnum=contnum+1;

end

Page 28: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.67

Implementation: Solver

Solver section

f u n c t i o n s o l v e r

bc func=s t r 2 f u n c (OcBVPVar . BVPFunction . Gen e r a l ) ;ode func=s t r 2 f u n c (OcBVPVar . ODEFunction . Gen e r a l ) ;

sw i t ch b v p s o l v e rcase ’ bvp4c ’

b v s o l a c t u a l= bvp4c ( odefunc , bcfunc , b v s o l p r e d i c t , opt .BVP) ;case ’ bvp5c ’

b v s o l a c t u a l= bvp5c ( odefunc , bcfunc , b v s o l p r e d i c t , opt .BVP) ;o t h e rw i s e

e r r o r ( ’BV s o l v e ’ ’%s ’ ’ not implemented . ’ , b v p s o l v e r )endi f ˜OcBVPVar . ActCont inuat i onType

b v s o l a c t u a l . con tparamete r=a c t u a l r e l d i s t a n c e ;end% r e t u r n s the res iduum o f the a c t u a l s o l u t i o nb c r e s=f e v a l ( bcfunc , b v s o l a c t u a l . y ( : , 1 ) , b v s o l a c t u a l . y ( : , end ) ) ;

f p r i n t f (1 , ’ Mesh s i z e : %d\n ’ , s i z e ( b v s o l a c t u a l . y , 2 ) ) ;f p r i n t f (1 , ’ I t e r a t i o n number : %d\n ’ , b v s o l a c t u a l . b c e v a l s ) ;i t e rnum=b v s o l a c t u a l . b c e v a l s ;f p r i n t f (1 , ’ Residuum norm : %g\n ’ , norm ( b c r e s ( 1 : end ) ) )i f norm ( b c r e s ( 1 : end ) ) > maxresnorm

e r r o r f l a g = 1 ;e l s e

e r r o r f l a g = 0 ;end

Page 29: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.68

Turning point of a stable manifold

Default continuation: fixed continuation direction

>> m=changeparameter (m, ’ r , c ’ , [ 0 . 5 3 ] ) ;>> ocEP=ca l c e p (m) ; b=i s a dm i s s i b l e (m, ocEP) ; ocEP(˜ b ) = [ ] ; i s s a d d l e ( ocEP{:})ans =

1 0 0 1 1>> opt=s e t o c o p t i o n s ( ’OCCONT ’ , ’ I n i t S t epWid th ’ , 0 . 002 , ’ MaxStepWidth ’ , 0 . 0 1 ) ;>> i n i t S t r u c t=i n i t o c c o n t ( ’ e x t r ema l ’ ,m, ’ i n i t p o i n t ’ , 1 , 0 , ocEP{4}, ’ I n t e g r a t i o nT ime ’ , 500) ;>> s o l=occon t (m, i n i t S t r u c t , opt ) ;

Page 30: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.69

Adaptive continuation: Initialization

>> opt=s e t o c o p t i o n s ( opt , ’OCCONT ’ , ’ I n i t S t epWid th ’ , 0 . 01 , ’ StepWidth ’ , 0 . 01 , ’MaxStepWidth ’, 0 . 0 1 ) ;

>> i n i t S t r u c t=i n i t o c c o n t ( ’ e x t r ema l ’ ,m, ’ i n i t p o i n t ’ , 1 , 0 , ocEP{4}, ’ I n t e g r a t i o nT ime ’ ,500 , ’Con t inuat i onType ’ , ’ a l ’ ) ;

>> s o l=occon t (m, i n i t S t r u c t , opt ) ;

Page 31: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.70

Adaptive continuation: Additional boundary condition

Model specific boundary condition for adaptive continuation

f u n c t i o n out = momBVP4Continuation ( dynVara , Cont inuat ionArgument , ActCont inuat ionType ,Targe tCoord inate ,#,#,#,#, I n i t i a l V a l u e , i n t e r n a l p a r a r g )

% the BC f i l e r e t u r n i n g the c o n t i n u a t i o n res iduum

% g l o b a l v a r i a b l eg l o b a l OcBVPVarsw i t ch Cont inuat ionArgument (1 )

case ’ i ’c o n t r e s = [ ] ; % con t i n u a t i o n res iduump o s r e s = [ ] ; % po s i t i o n res iduumi f Ac tCont inuat i onType==0

po s r e s=dynVara ( Ta rge tCoo rd i n a t e )−I n i t i a l V a l u e ;e l s e i f ActCont inuat i onType==2

nv=d i f f ( Prev iousBVSol ( [ 1 : 2 ] , : ) , 1 , 2 ) /norm ( d i f f ( Prev iousBVSol ( [ 1 : 2 ] , : ) , 1 , 2 ) ) ;c o n t r e s=sum ( [ Prev iousBVSol ( [ 1 : 2 ] , 2 )−dynVara ( 1 : 2 ) ] . ’∗ nv )+StepWidth ;p o s r e s=dynVara ( Ta rge tCoo rd i n a t e )−Star tVa lue−i n t e r n a l p a r a r g ( end )∗

Con t i nua t i onVec t o r ;end

endout=[ p o s r e s ; c o n t r e s ] ;

Page 32: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.71

Plotting the calculated solutions

Load stored model into the MATLAB workspace

>> l o ad ( ’ F i r s tMode l . mat ’ ) ;m=ocObj>> ocEx=e x t r ema l s o l (m) ;>> ocEP=e q u i l i b r i um (m) ;>> m. o cRe su l t s . Stab lePath IVP ; ocAsym=ans ;

Native MATLAB syntax

>> p l o t ( ocEx {1} . t , ocEx {1} . dynVar ( 1 , : ) )>> s e t ( gca , ’ Xlim ’ , [ 0 0 . 0 5 ] , ’YLim ’ , [−0.05 5 ] )

−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1−1

−0.8

−0.6

−0.4

−0.2

0

0.2

0.4

0.6

0.8

1

Page 33: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.71

Plotting the calculated solutions

Load stored model into the MATLAB workspace

>> l o ad ( ’ F i r s tMode l . mat ’ ) ;m=ocObj>> ocEx=e x t r ema l s o l (m) ;>> ocEP=e q u i l i b r i um (m) ;>> m. o cRe su l t s . Stab lePath IVP ; ocAsym=ans ;

Native MATLAB syntax

>> p l o t ( ocEx {1} . t , ocEx {1} . dynVar ( 1 , : ) )>> s e t ( gca , ’ Xlim ’ , [ 0 0 . 0 5 ] , ’YLim ’ , [−0.05 5 ] )

>> ho ld on , p l o t ( ocEx {1} . dynVar ( 1 , : ) , ocEx {1} . dynVar ( 2 , : ) )

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 50

0.2

0.4

0.6

0.8

1

1.2

1.4

Page 34: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.71

Plotting the calculated solutions

Load stored model into the MATLAB workspace

>> l o ad ( ’ F i r s tMode l . mat ’ ) ;m=ocObj>> ocEx=e x t r ema l s o l (m) ;>> ocEP=e q u i l i b r i um (m) ;>> m. o cRe su l t s . Stab lePath IVP ; ocAsym=ans ;

Native MATLAB syntax

>> p l o t ( ocEx {1} . t , ocEx {1} . dynVar ( 1 , : ) )>> s e t ( gca , ’ Xlim ’ , [ 0 0 . 0 5 ] , ’YLim ’ , [−0.05 5 ] )

>> ho ld on , p l o t ( ocEx {1} . dynVar ( 1 , : ) , ocEx {1} . dynVar ( 2 , : ) )

>> p l o t ( ocAsym{2} . dynVar ( 1 , : ) , ocAsym {2} . dynVar ( 2 , : ) , ’ Co lo r ’ , [ 00 .5 0 ] )

−6 −4 −2 0 2 4 6−1.5

−1

−0.5

0

0.5

1

1.5

Page 35: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.72

Plotting the calculated solutions (2)

Overloaded OCMat syntax

>> p l o t p h a s e (m, ocEP{1} , ’ c o s t a t e ’ , 1 , 1 , ’ Co lo r ’ , [ 1 0 0 ] )>> ho ld on , p l o t p h a s e (m, ocEx{1} , ’ c o s t a t e ’ , 1 , 1 )>> p l o t p h a s e (m, ocAsym{2} , ’ c o s t a t e ’ , 1 , 1 , ’ Co lo r ’ , [ 0 0 .5 0 ] )

−6 −4 −2 0 2 4 6−1.5

−1

−0.5

0

0.5

1

1.5

Page 36: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.72

Plotting the calculated solutions (2)

Overloaded OCMat syntax

>> p l o t p h a s e o c r e s u l t (m, ’ c o s t a t e ’ , 1 , 1 )

−6 −4 −2 0 2 4 6−1.5

−1

−0.5

0

0.5

1

1.5

Page 37: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.72

Plotting the calculated solutions (2)

Overloaded OCMat syntax

>> p l o t p h a s e o c r e s u l t (m, ’ c o s t a t e ’ , 1 , 1 , ’ e x c l u d e ’ ,{ ’E x t r ema l S o l u t i o n ’ , ’ S l i c eMan i f o l d ’ })

−6 −4 −2 0 2 4 6−1.5

−1

−0.5

0

0.5

1

1.5

Page 38: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.72

Plotting the calculated solutions (2)

Overloaded OCMat syntax

>> p l o t p h a s e o c r e s u l t (m, ’ c o s t a t e ’ , 1 , 1 , ’ o n l y ’ ,{ ’ E q u i l i b r i um ’ } , ’Marker ’ , ’ . ’ , ’ Marke rS ize ’ ,16)

−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1−1

−0.8

−0.6

−0.4

−0.2

0

0.2

0.4

0.6

0.8

1

Page 39: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.72

Plotting the calculated solutions (2)

Overloaded OCMat syntax

>> p l o t p h a s e o c r e s u l t (m, ’ c o s t a t e ’ , 1 , 1 , ’ o n l y ’ ,{ ’ S tab lePath IVP ’ } ,’ l i m i t s e t ’ , ’ on ’ , ’ l i m i t s e tMa r k e r ’ , ’ . ’ , ’ l i m i t s e tMa r k e r S i z e ’,16)

−6 −4 −2 0 2 4 6−1.5

−1

−0.5

0

0.5

1

1.5

Page 40: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.72

Plotting the calculated solutions (2)

Overloaded OCMat syntax

>> p l o t p h a s e o c r e s u l t (m, ’ c o s t a t e ’ , 1 , 1 , ’ o n l y ’ ,{ ’ S tab lePath IVP ’ } ,’ l i m i t s e t ’ , ’ o f f ’ )

−6 −4 −2 0 2 4 6−1.5

−1

−0.5

0

0.5

1

1.5

Page 41: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.72

Plotting the calculated solutions (2)

Overloaded OCMat syntax

>> p l o t p h a s e o c r e s u l t (m, ’ c o s t a t e ’ , 1 , 1 , ’ o n l y ’ ,{ ’ S tab lePath IVP ’ } ,’ o n l y i n d e x ’ ,{2})

−6 −5 −4 −3 −2 −1 0−1.4

−1.2

−1

−0.8

−0.6

−0.4

−0.2

0

Page 42: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.72

Plotting the calculated solutions (2)

Overloaded OCMat syntax

>> p l o t p h a s e o c r e s u l t (m, ’ c o n t r o l ’ , 1 , 1 , ’ o n l y ’ ,{ ’ S tab lePath IVP ’ })

−6 −4 −2 0 2 4 6−0.8

−0.6

−0.4

−0.2

0

0.2

0.4

0.6

Page 43: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.72

Plotting the calculated solutions (2)

Overloaded OCMat syntax

>> p l o t p h a s e o c r e s u l t (m, ’ h am i l t o n i a n ’ , 1 , 1 , ’ o n l y ’ ,{ ’S tab lePath IVP ’ })

−6 −4 −2 0 2 4 60

0.5

1

1.5

2

2.5

Page 44: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.73

Plotting in the time domain

Load stored model into the MATLAB workspace

>> l o ad ( ’ F i r s tMode l . mat ’ ) ;m=ocObj>> ocEx=e x t r ema l s o l (m) ;>> ocEP=e q u i l i b r i um (m) ;>> m. o cRe su l t s . Stab lePath IVP ; ocAsym=ans ;

Native MATLAB syntax

>> p l o t ( ocEx {1} . t , ocEx {1} . dynVar ( 1 , : ) )>> s e t ( gca , ’ Xlim ’ , [ 0 0 . 0 5 ] , ’YLim ’ , [−0.05 5 ] )

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10

0.5

1

1.5

2

2.5

3

3.5

4

4.5

5

Page 45: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.73

Plotting in the time domain

Load stored model into the MATLAB workspace

>> l o ad ( ’ F i r s tMode l . mat ’ ) ;m=ocObj>> ocEx=e x t r ema l s o l (m) ;>> ocEP=e q u i l i b r i um (m) ;>> m. o cRe su l t s . Stab lePath IVP ; ocAsym=ans ;

Native MATLAB syntax

>> p l o t ( ocEx {1} . t , ocEx {1} . dynVar ( 1 , : ) )>> s e t ( gca , ’ Xlim ’ , [ 0 0 . 0 5 ] , ’YLim ’ , [−0.05 5 ] )

0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.050

0.5

1

1.5

2

2.5

3

3.5

4

4.5

5

Page 46: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.74

Plotting in the time domain (2)

Overloaded OCMat syntax

>> p l o t t imep a t h (m, ocEx{1} , ’ c o s t a t e ’ , 1 )>> s e t ( gca , ’ Xlim ’ , [ 0 50 ] , ’YLim ’ , [−0.05 1 . 5 ] )

0 5 10 15 20 25 30 35 40 45 50

0

0.5

1

1.5

Page 47: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

Numerical basics

Solving IVPs

Solving BVPs

Continuation

Stable paths

Definitions

IVP approach

BVP approach

BVP Implementation

Initialization

Dynamics

Boundary conditions

Continuation

Solver

Turning points

Adaptive continuation

Graphics

Phase space

Native MATLAB

Overloaded OCMat

Time path

Native MATLAB

Overloaded OCMat

References

OCMat functions

2.75

References

P. Kunkel and O. von dem Hagen. Numerical solution of infinite-horizonoptimal-control problems. Computational Economics, 16(3):189–205,2000

T. Pampel. Numerical approximation of generalized connecting orbits.PhD thesis, University of Bielefeld, 2000

R. Winkler. Path-following for two-point boundary value problems.Seminarbericht 78, Department of Mathematics, Humboldt-UniversityBerlin, Germany, 1985

D. Grass. Numerical computation of the optimal vector field in a fisherymodel. The Beijer Discussion Paper 224, The Beijer Institute, TheRoyal Swedish Academy of Sciences, 2010

Y.A. Kuznetsov. Elements of Applied Bifurcation Theory. SpringerVerlag, New York, 2nd edition, 1998

A.S. Dontchev and R.T. Rockafellar. Implicit Functions and Solution

Mappings: A View from Variational Analysis. Springer Verlag, 2009

B. Krauskopf, H.M. Osinga, and J. Galan-Vioque, editors. Numerical

Continuation Methods for Dynamical Systems: Path following and

boundary value problems. Springer Verlag, Dordrecht, 2007

Page 48: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

IVPs

Solve ODEs

Stable path

Manage results

BVPs

Stable path

Store paths

Plotting

Phase space

Time path

2.76

Solving IVPs

odesolve

f u n c t i o n [ s o l , s o l n ]= od e s o l v e ( ocObj , t ime l eng th , ocTr j , v a r a r g i n )%% ODESOLVE s o l v e s an i n i t i a l v a l u e problem o f ocmodel .%% SOL=ODESOLVE(OCOBJ,TIMELENGTH,OCELEMENT) OCOBJ op t ima l c o n t r o l model TIMELENGTH% denotes % the l e n g t h o f the i n t e g r a t i o n i n t e r v a l . With OCELEMENT the i n i t i a l% s t a t e and a c t u a l a r c % i d e n t i f i e r i s p r ov i d ed . OCELEMENT can be e i t h e r an% o c t r a j e c t o r y o r an occu r v e . By d e f a u l t the f i r s t column o f the dynVar f i e l d i s% taken as the i n t i a l p o i n t and the c o r r e s p on d i n g a r c i d as the a r c i d e n t i f i e r .%% SOL=ODESOLVE(OCOBJ,TIMELENGTH, X0 , ARCID) Here X0 i s the i n i t i a l p o i n t and ARCID% the v a l u e o f the a r c i d e n t i f i e r . I f ARCID i s omi t t ed i t i s assumed as one .%% SOL=ODESOLVE(OCOBJ,TIMESPAN , . . . , OPT) op t i on s t r u c t u r e OPT ( s e e d e f a u l t o c o p t i o n s ) .% For the u s u a l ode op t i o n s s e e MATLAB r e f e r e n c e f o r od e s e t . I n the o c op t i o n s% s t r u c t u r e t h e s e can be ad r e s s e d by :% opt=s e t o c o p t i o n s ( ’ODE’ , o p t i o n f i e l d , o p t i o n v a l u e )% Only f o r the ’ Even t s and Jacob ian f i e l d you do must not p r o v i d e the f i l e n ame% ( as i n od e s e t ) , but on l y s e t i t to 1 ’ Even t s/ Jaco iban ’ on or 0 ’ Even t s/ Jaco iban ’% o f f . Depending on the OCOBJ the , e . g . , the even t f i l e i s assumed as% ’ modelnameEvents ’ .%% Other o p t i o n s impor tan t f o r the o d e s o l v e r a r e s e t i n the OC f i e l d . There you can% change the d e f a u l t o d e s o l v e r ’ ode45 ’ to some o th e r s o l v e r s e t t i n g :% opt=s e t o c o p t i o n s ( ’OC’ , ’ ODESolver ’ , o d e s o l v e r )% I f you want to chage the d e f a u l t i n d ex f o r the i n i t i a l p o i n t from 1 to some o th e r% s t a r t i n g i nd ex you can s e t :% opt=s e t o c o p t i o n s ( ’OC’ , ’ S t a r t I n d ex ’ , i d x )

Page 49: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

IVPs

Solve ODEs

Stable path

Manage results

BVPs

Stable path

Store paths

Plotting

Phase space

Time path

2.77

Calculating manifold paths

f u n c t i o n ocAsy = s t a b l e p a t h ( ocObj , dynPrim , t ime l eng th , v a r a r g i n )%% STABLEPATH c a l c u l a t e s the s t a b l e path u s i n g the IVP approach .%% For the IVP a po i n t l y i n g on the s t a b l e e i g e n s p a c e near% the e q u i l i b r i um i s used as an i n i t i a l p o i n t f o r s o l v i n g the% c a n o n i c a l sys tem backwards i n t ime .%% STABLEPATH(OCOBJ,DYNPRIM,T) c a l c u l a t e s the s t a b l e path o f DYNPRIM wi th% t ime l e n g t h abs (T) .%% STABLEPATH(OCOBJ,DYNPRIM,T,OPT) d i s t a n c e o f the i n i t i a l p o i n t from the% equ i l b r i um i s de te rm ined by the op t i on en t r y ’ L i n e a r i z a t i o nD i s t a n c e ’ o f the OC% f i e l d . Thus to change the d e f u a l t d i s t a n c e 1e−3 you can s e t :% opt=s e t o c o p t i o n s ( ’OC’ , ’ L i n e a r i z a t i o nD i s t a n c e ’ , e i g t o l )% To c a l c u l a t e the path i n the op p o s i t e d i r e c t i o n one can s e t% opt=s e t o c o p t i o n s ( ’OC’ , ’ Backward ’ , 1 )%% STABLEPATH(OCOBJ,DYNPRIM,T,OPT,FLAG) i f FLAG i s s e t to ’ s t rong ’ than the% s t r o n g s t a b l e path i s c a l c u l a t e d ( e i g e n v e c t o r c o r r e s p on d i n g to the% e i g e n v a l u e w i th maximum ab s o l u t r e a l v a l u e ) .%% STABLEPATH(OCOBJ,DYNPRIM,T,OPT,FLAG ,W) For s t a b l e man i f o l d s w i th d imens ion% dim>1 the f a c t o r W can be p rov ided , we i gh t i n g the d i f f e r e n t e i g e n v e c t o r s .

f u n c t i o n ocAsy = man i f o l d pa th ( ocObj , dynPrim , t ime l eng th , v a r a r g i n )%% MANIFOLDPATH c a l c u l a t e s the s t a b l e path%% MANIFOLDPATH(OCOBJ,DYNPRIM,T,OPT,FLAG) FLAG can be one o f the e n t r i e s% ’ s ’ : s t a b l e o r ’ s s ’ : s t r o n g s t a b l e o r ’ws ’ : weak s t a b l e% ’u ’ : u n s t a b l e o r ’ su ’ : s t r o n g u n s t ab l e o r ’wu ’ : weak u n s t ab l e% ’ c ’ : c e n t e r% For the o th e r e n t r i e s and op t i o n s s e e ’ s t a b l e p a t h ’

Page 50: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

IVPs

Solve ODEs

Stable path

Manage results

BVPs

Stable path

Store paths

Plotting

Phase space

Time path

2.78

Storing results

f u n c t i o n [ newocObj , v a r a r g ou t ]= s t o r e ( ocObj , v a r a r g i n )%% STORE r e s u l t s%% OCOBJ=STORE(OCOBJ, ’CONTINUATION ’ ) s t o r e r e s u l t s o f the l a s t% c o n t i n u a t i o n p r o c e s s i n the r e s u l t s o f the oc model OCOBJ . The i d e n t i f i e r% ’CONTINUATION’ can a l s o be omi t t ed .%% OCOBJ=STORE(OCOBJ, ’ DATAFILE .MAT’ ) s t o r e data g i v en by the MAT f i l e DATAFILE .MAT as% a r e s u l t o f a c o n t i n u a t i o n p r o c e s s .%% OCOBJ=STORE(OCOBJ,OCELEMENT) s t o r e s the ob j e c t OCELEMENT i n the r e s u l t s o f the oc% ob j e c t OCOBJ (OCELEMENT can be a c e l l a ray o f o b j e c t s ) . P o s s i b l e c l a s s e s f o r% OCELEMENT are% dy n p r im i t i v e : e q u i l i b r i um or l i m i t c y c l e% oca s ymp to t i c : a s ymp to t i c s o l u t i o n s%% The d e f a u l t f i e l d n ame s o f the o cRe s u l t s a r e% ocRe s u l t s . E q u i l i b r i um : f o r a d y n p r im i t i v e b e i n g an e q u i l i b r i um% ocRe s u l t s . L im i tCy c l e : f o r a d y n p r i m i t i v e b e i n g a l i m i t c y c l e% ocRe s u l t s . E x t r ema l S o l u t i o n : f o r an oca s ymp to t i c% ocRe s u l t s . NonAdmi s s i b l eSo l u t i on : f o r an oca s ymp to t i c v i o l a t i n g the% models c o n s t r a i n t s%% OCOBJ=STORE ( . . . , ’ REPLACE’ ,VAL) f o r c e s to r e p l a c e an a l r e a d y c a l c u l a t e d% ob j e c t by the new one i f VAL=’on ’ ( d e f a u l t i t i s s e t ’ o f f ’ ) . I f t h e% ob j e c t does not a l r e a d y e x i s t i t i s appended .%% OCOBJ=STORE ( . . . , ’ APPEND’ ,VAL) f o r c e s to append the ob j e c t to an a l r e a d y% e x i s t i n g c e l l a r r a y o f o b j e c t s o r c r e a t e s a new c e l l a r r a y .%% OCOBJ=STORE ( . . . , ’ FIELDNAME’ ,VAL) f o r c e s to s t o r e the ob j e c t a t the% ocRe s u l t s under the f i e l d n ame VAL, i f the ob j e c t i s a c e l l a r ray , VAL i s% e i t h e r a s t r i n g o r a c e l l a r r a y o f s t r i n g s o f the same l e n g t h as VAL .%% [OCOBJ,OCELEMENT]=STORE(OCOBJ , . . . ) r e t u r n s the s t o r e d e l ement s OCELEMENT.%% [OCOBJ,OCELEMENT,FIELDNAMES]=STORE(OCOBJ , . . . ) r e t u r n s c o r r e s p on d i n g% f i e l d n ame s .

Page 51: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

IVPs

Solve ODEs

Stable path

Manage results

BVPs

Stable path

Store paths

Plotting

Phase space

Time path

2.79

Removing results

Remove specific results

f u n c t i o n ocObj=d e l e t e ( ocObj , v a r a r g i n )%% DELETE d e l e t e s s p e c i f i c r e s u l t s s t o r e d i n OCOBJ .%% OCOBJ=DELETE(OCOBJ,FIELDNAME, INDEX) d e l e t e s the r e s u l t s s t o r e d under FIELDNAME i n the% ocResu l t s , where INDEX i s an a r r a y s p e c i f y i n g the i nd ex o f the r e s u l t s to be d e l e t e d .%% OCOBJ=DELETE(OCOBJ, FIELDNAME1, INDEX1 , FIELDNAME2, INDEX2 , . . . )

Remove entire field

f u n c t i o n newocObj=remove ( ocObj , f i e l dname , v a r a r g i n )%% REMOVE removes a l l r e s u l t s from a o cRe s u l t s f i e l d .%% OCOBJ=REMOVE(OCOBJ,FIELDNAME) removes the f i e l d FIELDNAME from the% ocRe s u l t s .%% OCOBJ=REMOVE(OCOBJ,FIELDNAME, ’FORCE’ ) f o r c e s to remove the f i e l d w i thou t% a warn ing .%% OCOBJ=REMOVE(OCOBJ,FIELDNAME, INDEX) remove o b j e c t s w i th i n d ex INDEX from% the f i e l d FIELDNAME o f the o cRe s u l t s .

Page 52: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

IVPs

Solve ODEs

Stable path

Manage results

BVPs

Stable path

Store paths

Plotting

Phase space

Time path

2.80

Retrieving stored results

Remark

To retrieve the elements of the fields Equilibrium andExtremalSolution in the ocResults specific commands aredefined, see below.

Predefined commands

>> ocEP=e q u i l i b r i um (m)ocEP =

[1 x1 d y n p r im i t i v e ] [ 1 x1 d y n p r im i t i v e ] [ 1 x1 d y n p r im i t i v e ]>> ocEx=e x t r ema l s o l (m)ocEx =

[1 x1 o c t r a j e c t o r y ] [ 1 x1 o c t r a j e c t o r y ] [ 1 x1 o c t r a j e c t o r y ]

Retrieve elements of other (user defined) fields

>> ocAsym=m. o cRe s u l t s . S tab l ePathocAsym =

[1 x1 oca s ymp to t i c ] [ 1 x1 oca s ymp to t i c ] [ 1 x1 oca s ymp to t i c ]>> ocEP=m. o cRe s u l t s . E q u i l i b r i umocEP =

[1 x1 d y n p r im i t i v e ] [ 1 x1 d y n p r im i t i v e ] [ 1 x1 d y n p r im i t i v e ]

Page 53: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

IVPs

Solve ODEs

Stable path

Manage results

BVPs

Stable path

Store paths

Plotting

Phase space

Time path

2.81

Initialization file for the BVP ansatz

f u n c t i o n i n i t S t r u c t=i n i t o c c o n t ( contarg , ocObj , va r t ype , t a r g e t c o o r d i n a t e , t a r g e t v a l u e ,i n i t t r j , v a r a r g i n )

%% INITOCCONT r e t u r n s the i n i t i a l i z i n g s t r u c t u r e f o r the c o n t i n u a t i o n% p r o c e s s%% INITSTRUCT=INITOCCONT(CONTARG,OCOBJ,VARTYPE,TCOORD,TVAL, INITTRJ , . . . )% r e t u r n s the i n i t i a l i z i n g s t r u c t u r e INITSTRUCT f o r the c o n t i n u a t i o n type% s p e c i f i e d i n CONTARG, where CONTARG i s% ’ ex t r ema l ’ . . . i f an ex t r ema l s o l u t i o n i s con t i n u ed% ’ boundary ’ . . . i f t h e boundary o f a c o n s t r a i n t i s con t i n u ed% ’ dnss ’ . . . i f a DNSS po i n t i s con t i n u ed% INITTRJ i s an i n i t i a l s o l u t i o n f o r the ana l y z ed BVP and i s o f c l a s s% ’ d y n p r im i t i v e ’ , ’ ocasymptot i c ’ o r ’ o c t r a j e c t o r y ’ .% The arguments VARTYPE, TCOORD and TVAL a r e e x p l a i n e d nex t :%% INITSTRUCT=INITOCCONT(CONTARG,OCOBJ, ’ INITPOINT ’ , INITPTIDX , INITPTVAL , . . . )% INITPTIDX denotes the c o o r d i n a t e ( s ) o f the i n i t i a l s t a t e s to which the% s o l u t i o n i s con t i n u ed . I f INITPTIDX i s empty the e n t i r e c o o r d i n a t e s o f% the s t a t e space a r e assumed .%% INITSTRUCT=INITOCCONT(CONTARG,OCOBJ, ’PARAMERTER’ , PARIDX ,PARVAL , . . . ) w i th% PARIDX the i nd ex o f the v a r i e d paramete r i s s p e c i f i e d and PARVAL denotes% the paramete r v a l u e to which the s o l u t i o n i s con t i n u ed .%% INITSTRUCT=INITOCCONT ( . . . , ’ I n t eg r a t i onT ime ’ ,T) f o r an i n f i n i t e t ime% ho r i z o n model t h i s denotes the t r u n c a t i o n time , o t h e rw i s e i t i s s e t to% the t ime h o r i z o n .%% INITSTRUCT=INITOCCONT ( . . . , ’ Cont inuat ionType ’ ,CT) p o s s i b l e v a l u e s f o r CT% are% ’ f ’ . . . f i x s t ep method% ’ a l ’ . . . a d ap t i v e s t ep method wi th l i n e a r e x t r a p o l a t i o n% ’ aq ’ . . . a d ap t i v e s t ep method wi th q u ad r a t i c e x t r a p o l a t i o n%% INITSTRUCT=INITOCCONT ( . . . , ’ PathType ’ ,PT) f o r an i n f i n i t e t ime h o r i z o n% model the path type PT i s e i t h e r ’ s ’ f o r a s t a b l e , ’ u ’ f o r an un s t ab l e ,% ’ ss ’ f o r the s t r o n g s t a b l e , and ’ uu ’ f o r the s t r o n g u n s t ab l e path .

Page 54: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

IVPs

Solve ODEs

Stable path

Manage results

BVPs

Stable path

Store paths

Plotting

Phase space

Time path

2.82

Calculating manifold paths using the BVP ansatz

f u n c t i o n v a r a r g ou t=occon t ( ocObj , I n i tBVSt ru c t , v a r a r g i n )%% OCCONT s o l v e s and con t i n u e s a BVP%% OCXP=OCCONT(OCOBJ, INITSTRUCT) c o n t i n u a t i o n o f a s o l u t i o n s p e c i f i e d i n% INITSTRUCT o f the oc model OCOBJ . OCXP i s the l a s t a dm i s s i b l e s o l u t i o n o f% the c o n t i n u a t i o n p r o c c e s s . INITSTRUCT i s a s t r u c t u r r e t u r n e d by INITOCCONT ( . . . ) .%% OCXP=OCCONT(CONTARG,OCOBJ, INITSTRUCT ,OPT) OPT p r o v i d e s o c op t i o n s f o r the% c o n t i n u a t i o n p r o c e s s . Impoerant o p t i o n s f o r the c o n t i n u a t i o n p r o c e s s a r e :% MaxCont inuat i onStep s : maximum number o f c o n t i n u a t i o n s t e p s [ I n f ]% Check : ch eck i n g a d m i s s i b i l i t y o f the s o l u t i o n ’ on ’% Backward : con t i n u e fo rward [ 0 ] o r backward [ 1 ]% In i t S t epWid th : i n i t i a l s t ep width [ 1 e−4]% MaxStepWidth : maximum s t ep width [ 0 . 5 ]% Mean I t e r a t i on : used as a ‘ ‘ measure ’ ’ f o r i n c r e a s i n g the s t ep width [ 1 5 ]% MinStepWidth : min imal s t ep width [ 1 e−8] , belwo tha t the c o n t i n u a t i o n% p r o c e s s i s i n t e r r u p t e d% Admi s s i b l eTo l : t o l e r a n c e to dete rm ine s a dm i s s i b l i t y o f a s o l u t i o n [ 1 e−6]% Sav e I n t e rmed i a t e : s t o r i n g a l s o i n t e rm e d i a t e s o l u t i o n s du r i n g c o n t i n a u t i o n% [ ’ o f f ’ ] . I f s e t to ’ on ’ the i n t e rm e d i a t e s o l u t i n o s

can% be added to the o cRe s u l t s u s i n g m=s t o r e (m)% StepFacto r : f a c t o r f o r i n c r e a s i n g the s t ep width [ 5 ]% MaxGr idPoints : p r o v i d e s an upper l i m i t f o r the t ime g r i d o f the s o l u t i o n .% I f t h i s i s exceeed the s t ep width i s r educed [ 5 0 0 ] .%% These op t i o n s can be s e t u s i n g : opt=s e t o c o p t i o n s ( ’OCCONT’ , optionname , o p t i o n v a l u e )%% To change the d e f a u l t BVP s o l v e r ’ bvp4c ’ use :% opt=s e t o c o p t i o n s ( ’OC’ , ’ BVPSolver ’ , b v p s o l v e r )%% The op t i o n s f o r the BVP s o l v e r can be s e t u s i n g :% opt=s e t o c o p t i o n s ( ’BVP’ , optionname , o p t i o n v a l u e )% f o r t h e s e op t i o n s s e e MATLAB r e f e r e n c e f o r ’ bvp4c ’ .

Page 55: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

IVPs

Solve ODEs

Stable path

Manage results

BVPs

Stable path

Store paths

Plotting

Phase space

Time path

2.83

Usage of the store command after continuation

Remark

During continuation the results are stored in the mat-file

ContinuationData.mat

in the data directory of the model. Using the store command thisfile is loaded and the data is added to the ocResults.

Example

>> m=ocmodel ( ’mom’ )>> m. o cRe s u l t sans =1x1 s t r u c t a r r a y w i th no f i e l d s .>> ocEP=ca l c e p (m) ; b=i s a dm i s s i b l e (m, ocEP) ; ocEP(˜ b ) = [ ] ;>> i n i t S t r u c t=i n i t o c c o n t ( ’ e x t r ema l ’ ,m, ’ i n i t p o i n t ’ , 1 , 5 , ocEP{1}, ’ I n t e g r a t i o nT ime ’ , 500)>> s o l=occon t (m, i n i t S t r u c t , opt ) ;...

>> m=s t o r e (m) ;>> m. o cRe s u l t sans =

Ex t r ema l S o l u t i o n : {[1 x1 oca s ymp to t i c ]}

generates or adds results to m.ocResults.ExtremalSolution.

Page 56: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

IVPs

Solve ODEs

Stable path

Manage results

BVPs

Stable path

Store paths

Plotting

Phase space

Time path

2.84

Plotting in the phase space

f u n c t i o n v a r a r g ou t = p l o t p h a s e ( v a r a r g i n )%% PLOTPHASE p l o t s the phase p o r t r a i t f o r a s o l u t i o n o f an ocmodel .%% PLOTPHASE(OCOBJ,OCELEMENT) p l o t s the f i r s t two c o o r d i n a t e s o f the OCELEMENT, where% OCELEMENT can be e i t h e r a d y n p r im i t i v e , occurve , o c t r a j e c t o r y o r o ca s ymp to t i c .%% PLOTPHASE(OCOBJ,OCTRJ,VARTYPE) wi th VARTYPE one can s p e c i f y the type% o f v a r i a b l e s to be p l o t t ed , p o s s i b l e v a l u e s a r e :% ’ s t a t e ’ . . . s t a t e v a l u e% ’ co s t a t e ’ . . . c o s t a t e v a l u e% ’ c on t r o l ’ . . . c o n t r o l v a l u e% ’ ham i l t on i an ’ . . . v a l u e o f the Hami l ton ian% ’ l m u l t i p l i e r ’ . . . v a l u e o f the Lagrang ian m u l t i p l i e r% ’ u s e r f un c ’ . . . v a l u e o f u s e r d e f i n e d f u n c t i o n s%% PLOTPHASE(OCOBJ,OCTRJ,VARTYPE,XCOORD) XCOORD s p e c i f i e s the s t a t e c o o r d i n a t e% XCOORD. I f i t i s empty the f i r s t s t a t e c o o r d i n a t e i s p l o t t e d .%% PLOTPHASE(OCOBJ,OCTRJ,VARTYPE,XCOORD,YCOORD) YCOORD s p e c i f i e s the c o o r d i n a t e% o f term g i v en by VARTYPE. I f i t i s empty the f i r s t c o o r d i n a t e o f VARTYPE i s p l o t t e d .%% PLOTPHASE(OCOBJ,OCTRJ,VARTYPE,XCOORD,YCOORD,PROPERTY,PROPERTYVAL) f o r p o s s i b l e% p r o p e r t i e s s e e MATLAB r e f e r e n c e o f p l o t .%% Add i t o n a l l y the f o l l o w i n g p r o p e r t i e s ( depend ing on the type o f OCELEMENT) can be s e t :% o c t r a j e c o t r y :% ’ con t inuous ’ . . . i f t h e o c t r a j e c t o r y c o n s i s t s o f d i f f e r e n t a r c s t h e s e can be% p l o t t e d as a cu r v e ’ c o n t i n u o s l y ’ connected at the sw i t c h i n g p o i n t s ’ on ’ . Or% each a r c i s p l o t t e d s e p a r a t e l y ( u s i n g a l s o d i f f e r e n t c o l o r s ) ’ o f f ’ .% occu r v e :% ’ a s s o c i a t e d s o l ’ . . . i f , e . g . , the occu r v e i s a boundary cu r v e c a l c a u l t e d by a% co n t i n u a t i o n p r o c e s s w i th ’ on ’ the a s s o c i a t e d ( ’ l a s t ’ ) s o l u t i o n o f t h i s% c o n t i n u a t i o n p r o c e s s i s p l o t t ed , d e f a u l t i s i s s e t to ’ o f f ’ .% oca s ymp to t i c :% ’ l i m i t s e t ’ . . . i f s e t to ’ on ’ the c o r r e s p on d i n g l i m i t s e t i s a l s o p l o t t ed , by% d e f a u l t t h i s i s s e t ’ o f f ’ .%% P r o p e r t i e s o f l i m i t s e t ( a s s i c a t e d s o l ) can be s e t u s ing , e . g . , ’ l im i t s e tMa r k e r ’ , ’ o ’

Page 57: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

IVPs

Solve ODEs

Stable path

Manage results

BVPs

Stable path

Store paths

Plotting

Phase space

Time path

2.85

Plotting ocResults in the phase space

f u n c t i o n v a r a r g ou t = p l o t p h a s e o c r e s u l t ( v a r a r g i n )%% PLOTPHASEOCRESULT p l o t s the o cRe s u l t s o f an oc ob j e c t .%% PLOTPHASEOCRESULT(OCOBJ,VARTYPE,XCOORD,YCOORD) f o r VARTYPE,XCOORD and YCOORD% see the PLOTPHASE command .%% PLOTPHASEOCRESULT(AXH,OCOBJ,VARTYPE,XCOORD,YCOORD) wi th AXH you can s p e c i f y a f i g u r e /

a x i s% hand l e where the r e s u l t s s hou l d be p l o t t e d .%% PLOTPHASEOCRESULT(H,OCOBJ,VARTYPE,XCOORD,YCOORD, ’ONLY’ ,CELLNAME) wi th CELLNAME you

can% p r o v i d e a c e l l a r r a y o f s t r i n g s c o n s i t i n g o f f i l e d names o f the o cRe s u l t s . Then on l y% th e s e r e s u l t s a r e p l o t t e d . Even f o r one f i e l d you have to s p e c i f y i t as a c e l l .%% PLOTPHASEOCRESULT( . . . , ’ ONLY’ ,CELLNAME, CELLIDX) CELLIDX i s a% c e l l a r r a y o f the same s i z e as CELLNAME and p r o v i e d the i nd ex o f s p e c i f i c e l ement s o f% the CELLNAME f i e l d s , e . g .% p l o t p h a s e o c r e s u l t (m, ’ s t a t e ’ , 1 , 2 , ’ on ly ’ ,{ ’ E x t r ema lSo l u t i on ’} , ’ on l y i n d ex ’ ,{ [ 2 3]})% on l y p l o t s the 2 and 3 ob j e c t s t o r e d i n o cRe s u l t s . E x t r ema l S o l u t i o n . With { [0]} no% e lement s i s p l o t t e d and wi th { [ ]} a l l e l emnt s a r e p l o t t e d .%% PLOTPHASEOCRESULT( . . . , ’ ONLY’ ,PROPERTY,PROPERTYVAL) wi th PROPERTY and PROPERTYVAL% p r o p e r t i e s can be p rov ided , s e e PLOTPHASE.%% H=PLOTPHASEOCRESULT ( . . . ) r e t u r n s the l i n e hand l e s

Page 58: Dieter Graß Lecture2: Basicnumericalmethods · Solving BVPs Continuation Stable paths Definitions IVP approach BVP approach BVP Implementation Initialization Dynamics Boundary conditions

Dieter Graß

IVPs

Solve ODEs

Stable path

Manage results

BVPs

Stable path

Store paths

Plotting

Phase space

Time path

2.86

plottimepath

f u n c t i o n v a r a r g ou t = p l o t t imep a t h ( v a r a r g i n )%% PLOTTIMEPATH p l o t s the t ime path f o r the o c t r a j e c t o r y o f an ocmodel .%% PLOTTIMEPATH(OCOBJ,OCTRJ,VARTYPE) p l o t s the t ime path s o f the OCTRJ v a r i a b l e s ,% where each c oo r d i n a t e i s d i s p l a y e d as a s e p e r a t e l i n e . w i th VARTYPE one can s p e c i f y

the type% o f v a r i a b l e s to be p l o t t ed , p o s s i b l e v a l u e s a r e :% ’ s t a t e ’ . . . s t a t e v a l u e% ’ co s t a t e ’ . . . c o s t a t e v a l u e% ’ c on t r o l ’ . . . c o n t r o l v a l u e% ’ ham i l t on i an ’ . . . v a l u e o f the Hami l ton ian% ’ l m u l t i p l i e r ’ . . . v a l u e o f the Lagrang ian m u l t i p l i e r% ’ u s e r f un c ’ . . . v a l u e o f u s e r d e f i n e d f u n c t i o n s%% PLOTTIMEPATH(OCOBJ,OCTRJ,VARTYPE,COORD) s p e c i f y the c o o r d i n a t e s COORD to% be p l o t t e d . I f i t i s empty a l l c o o r d i n a t e s a r e d i s p l a y e d .%% PLOTTIMEPATH(OCOBJ,OCTRJ,VARTYPE,COORD,PROPERTY,PROPERTYVAL) he r e% PROPERTY can e i t h e r be a u s u a l p r o p e r t y o f the MATLAB p l o t command or% one o f the p r o p e r t i e s ot the ’ p l o t pha s e ’ command .%% PLOTTIMEPATH(AXH , . . . ) the r e s u l t i s p l o t t e d to the axes w i th hand l e AXH.%


Recommended