Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General...

51
Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic objects Cells Structures Classes and objects Initialize model ’MoM’ The canonical system Basic operations Starting the analysis Calculating equilibria Properties of equilibria Solution classes octrajectory dynprimitive ocasymptotic occurve References OCMat functions 1.1 Lecture 1 : First steps: Implementation and a simple model How to start? OCMat: A MATLAB package for the analysis of optimal control problems Dieter Graß: [email protected] Operations Research and Control Systems (ORCOS)

Transcript of Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General...

Page 1: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.1

Lecture 1:

First steps: Implementation and a

simple modelHow to start?

OCMat: A MATLAB package for the analysis ofoptimal control problems

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

Page 2: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.2

General remarks

• Actual OCMat version only works with MATLAB 7.5.0(R2007b) and lower versions.

• MATLAB changed Symbolic Toolbox engine, from MAPLEkernel to MuPad.

• Automatic file generation needs Symbolic toolbox.

• Nonlinear equation solver needs Optimization Toolbox.

• Files bvp4c.m and bvp5c.m need to be adapted.

• Totally revised version of OCMat within one year.• Compatibility with higher MATLAB versions.• More problem classes, e.g., multi-stage models, differential

games, ...• Improved interlocking of BVP solver and continuation.

(BVPSUITE1.1, Ewa Weinmuller)

• OCMat Homepage:http://orcos.tuwien.ac.at/research/ocmat_software

Page 3: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.3

Outlook: Analyze and animate

Initialization file

% An Opt ima l l y Managed Co ra l Reefs t a t ed ynam i c s=sym( ’ [ ( s igma∗x1∗(1−x1/x2 )−gamma/( x3+tau )∗(x1ˆ th e t a /(1+x1ˆ th e t a ) )−e ta∗u1∗

x1 ) / e p s i l o n ; ( n−d∗x2−e∗x2∗x1 ) / e p s i l o n ; p s i∗x3 /( kappa∗x3+s∗x2+w)−l∗x3 ] ’ ) ;c o n t r o l c o n s t r a i n t=sym( ’ [ u1−ulow ] ’ ) ;

o b j e c t i v e f u n c t i o n=sym( ’ p∗u1∗x1−ph i∗u1ˆ2+mu∗x3 ’ ) ;

% Gene r a ln=30;d=0.05;e =2.2 ;e p s i l o n=1e−1;p s i =1;kappa=1;s=1;w=1;l =0.05;p=1;mu=93;r =0.02;ulow=0;tau=1e−5;s igma =0.825;gamma=1;ph i=1;e ta =1;t h e t a =1;

Page 4: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.3

Outlook: Analyze and animate

Page 5: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.4

An introductory example

Model formulation (Model of moderation)

maxν(·)

∫ ∞

0

e−rt(

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

dt

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

Page 6: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.4

An introductory example

Model formulation (Model of moderation)

maxν(·)

∫ ∞

0

e−rt(

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

dt

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

Necessary conditions

H(x , ν, λ) := x2 + cν2 + λ(

x − x3 + ν)

ν◦(t) = maxν

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

2cλ(t)

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

1− 3x(t)2)

Page 7: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.4

An introductory example

Initialization file (mom.m)

% Model o f moderat ion (MOM)s t a t ed ynam i c s=sym( ’ x1−x1ˆ3+u1 ’ ) ;o b j e c t i v e f u n c t i o n=sym( ’ x1ˆ2+c∗u1ˆ2 ’ ) ;

r =0.5 ;c=1;

Necessary conditions

H(x , ν, λ) := x2 + cν2 + λ(

x − x3 + ν)

ν◦(t) = maxν

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

2cλ(t)

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

1− 3x(t)2)

Page 8: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.4

An introductory example

Initialization file (mom.m)

% Model o f moderat ion (MOM)s t a t ed ynam i c s=sym( ’ x1−x1ˆ3+u1 ’ ) ;o b j e c t i v e f u n c t i o n=sym( ’ x1ˆ2+c∗u1ˆ2 ’ ) ;

r =0.5 ;c=1;

Generating files

>> [ ocPro , fonCon , mode l I n f o ]= i n i t o cma t ( ’mom ’ ) ;>> m=ocmodel ( ’mom’ ) ;>> f i l e s 4m o d e l (m) ;>> moveocmat f i l e s (m) ;

Page 9: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.5

Numeric types

Constructing numeric arrays

>> v = [12 −8 22 ]

v =

12 −8 22

>> M=[1 2 8 ;4 −5 6 ;0 4 4 ]

M =

1 2 84 −5 60 4 4

Accessing numeric arrays

>> v ( [ 1 end ] )ans =

12 22

>> M( 3 , : )ans =

0 4 4

>> M( [ 1 3 ] , 2 )ans =

24

Multiplication operator

>> M∗M

ans =

9 24 −12−16 57 −2216 −36 40

Pointwise operator

>> M.∗M

ans =

1 4 6416 25 360 16 16

Page 10: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.6

Strings and characters

Constructing string arrays

>> s t r f 1=’ d∗x1ˆ2∗u1ˆd−ph i∗x2 ’ ;>> s t r f 2=’ e∗x1∗u2ˆ sigma−p s i ∗x2 ’

;

>> s t r=s t r v c a t ( s t r f 1 , s t r f 2 )

s t r =

d∗x1ˆ2∗u1ˆd−ph i∗x2e∗x1∗u2ˆ s i g−p s i ∗x2

Accessing string arrays

>> s t r ( 2 , : )

ans =

e∗x1∗u2ˆ s i g−p s i ∗x2>> s t r ( 1 , : ) =[ ]

s t r =

e∗x1∗u2ˆ s i g−p s i ∗x2

Page 11: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.7

Symbolic objects

Constructing symbolic objects

>> equ=sym( ’ a∗xˆ2+b∗x+c ’ )

equ =

a∗xˆ2+b∗x+c>> symf (1 )=sym ( s t r ( 1 , : ) ) ; symf (2 )=sym ( s t r ( 2 , : ) )

symf =

[ d∗x1ˆ2∗u1ˆd−ph i∗x2 , e∗x1∗u2ˆ sigma−p s i ∗x2 ]

Operators on symbolic objects

>> d i f f ( equ , ’ x ’ )

ans =

2∗a∗x+b

>> s o l=s o l v e ( symf (1 ) , symf (2 ) , ’ x1 ’ , ’ x2 ’ )s o l =

x1 : [ 2 x1 sym ]x2 : [ 2 x1 sym ]

Page 12: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.8

Cells

Cells

>> C{1}=[1 2 8 ;4 −5 6 ;0 4 −4];>> C{2}=s t r v c a t ( s t r f 1 , s t r f 2 ) ;>> C{3}=symf

C =

[3 x3 doub le ] [ 2 x20 char ] [ 1 x2 sym ]

Accessing

>> C{ [1 3 ]}ans =

1 2 84 −5 60 4 −4

ans =

[ d∗x1ˆ2∗u1ˆd−ph i∗x2 , e∗x1∗u2ˆ sigma−p s i ∗x2 ]

>> C(2)ans =

[2 x20 char ]

Page 13: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.9

Structures

Structure

>> S . F1=[1 2 8 ; 4 −5 6 ; 0 4 −4] ; S . F2=s t rS =

F1 : [ 3 x3 doub le ]F2 : [ 2 x20 char ]

>> s o ls o l =

x1 : [ 2 x1 sym ]x2 : [ 2 x1 sym ]

Accessing

>> s o l . x1

ans =

0ph i∗e∗u2ˆ sigma /d∗u1ˆ(−d ) / p s i

>> S . F2 ( 1 , : )ans =

d∗x1ˆ2∗u1ˆd−ph i∗x2

Page 14: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.10

Classes and objects

• Introduces new data type building up on structures

• Allows to redefine (overload) operators, e.g., plottingcommands

• You create an object by calling the class constructor

• The M-files defining the methods for a class are collected in aclass directory (e.g., @ocmodel)

ocmodel object

>> m=ocmodel ( )

m =

empty ocmodel>> s t r u c t (m)

ans =

o c I n s t a n t : [ 1 x1 s t r u c t ]o c I n f o s : [ 1 x1 s t r u c t ]

ocProblem : [ 1 x1 s t r u c t ]o cRe s u l t s : [ 1 x1 s t r u c t ]

Page 15: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.11

Folder structure ’ocmat’

Page 16: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.12

Initialization process: first step

Store properties and necessary conditions in structures

>> [ ocPro , fonCon ]= i n i t o cma t ( ’mom ’ )ocPro =

Sta t e : [ 1 x1 s t r u c t ]Con t r o l : [ 1 x1 s t r u c t ]

Ob j e c t i v e : [ 1 x1 s t r u c t ]C o n s t r a i n t : [ 1 x1 s t r u c t ]Parameter : [ 1 x1 s t r u c t ]

Autonomous : 1E x p l i c i t C o n t r o l : 1

fonCon =

Ac t i v eCo n s t r a i n t : [ 1 x1 s t r u c t ]Ad jo in tDynamics : [ 1 x1 sym ]

A r c I d e n t i f i e r : [ 1 x1 s t r u c t ]E x p l i c i t S o l u t i o n : { [1 x1 s t r u c t ]}

F i r s tOrde rDynamic s : [ 1 x1 s t r u c t ]Func t i on : [ 1 x1 s t r u c t ]

T r a n s v e r s a l i t y C o n d i t i o n : [ 1 x1 sym ]

>> fonCon . E x p l i c i t S o l u t i o n {1} . Con t r o l . Valueans =

−1/2∗ lambda1 /c

Page 17: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.13

Function ’initocmat’

Initialization

f u n c t i o n [ ocPro , fonCon , mode l I n f o ]= i n i t o cma t (modelname )%% INITOCMAT d e r i v e s and s t o r e s i n f o rma t i o n from the ocmodel%b a s e d a t a d i r=getocmatpath ( ’ data ’ ) ;d a t a d i r= f u l l f i l e ( b a s eda t ad i r , modelname ) ;

%% Command \ ocb lo ck%

% s t o r e model i n f o rma t i o n i n model data d i r e c t o r ysave ( f u l l f i l e ( d a t ad i r , [ modelname ’ Data . mat ’ ] ) , ’ ocPro ’ , ’ fonCon ’

, ’ mode l I n f o ’ )

Page 18: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.14

Data generated and stored during initialization

Page 19: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.15

Initialization process: second step

Construct class ’ocmodel’ and generate M-files

>> m=ocmodel ( ’mom’ ) ,m. o c I n f o s

m =

mom: ocmodel o b j e c t

ans =

Name : ’mom ’Opt ima lCont ro lType : ’ s tdmode l ’

Opt imalCont ro lSubType : ’ ’Pa r ame t e rVa r i ab l e : [ 1 x3 sym ]

StageNum : 1Autonomous : 1IndexTab l e : [ 1 0 1 ]

I m p l i c i t C o n t r o l : 0

>> f i l e s 4m o d e l (m)

Page 20: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.16

Initialization process: third step

Page 21: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.16

Initialization process: third step

Move generated M-files

>> moveocmat f i l e s (m)>> t ype momCanonicalSystem .m

f u n c t i o n out = momCanonicalSystem ( t , dynVar , para rg , a r c a r g )%% r e t u r n s the c a n o n i c a l system o f mom model

% Parameter Valuer=pa r a r g (1 ) ;c=pa r a r g (2 ) ;T=pa ra r g (3 ) ;% Func t i on s

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

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

Page 22: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.16

Initialization process: third step

Page 23: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.17

Representations of the canonical system

Manual derivation

ν◦(t) = maxν

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

2cλ(t)

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

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

1− 3x(t)2)

Page 24: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.17

Representations of the canonical system

Symbolic derivation using MATLAB

>> c anon i c a l s y s t em (m)

ans =

dynVar1−dynVar1ˆ3−1/2∗dynVar2 /cr∗dynVar2−2∗dynVar1−dynVar2∗(1−3∗dynVar1 ˆ2)

Remark

In the simplest case the variable dynVar=[dynVar1 dynVar2]

represents the state-costate vector (x , λ)

Page 25: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.17

Representations of the canonical system

M-file representation

>> t ype momCanonicalSystem .m

f u n c t i o n out = momCanonicalSystem ( t , dynVar , para rg , a r c a r g )%% r e t u r n s the c a n o n i c a l system o f mom model

% Parameter Valuer=pa r a r g (1 ) ;c=pa r a r g (2 ) ;T=pa ra r g (3 ) ;% Func t i on s

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

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

Page 26: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.18

Basic operations: Saving and loading

Save and reload

>> save (m)>> l o ad ( ’ mom p1 2 p2 1 p3 In f .

mat ’ )>> m=ocObj ;

User specified name

>> save (m, [ ] , [ ] , [ ] , ’ F i r s tMode l ’ )

Showing parameter values

>> par=showparameter (m)r : 2c : 1T : I n f

par =

2 1 I n f

Changing parameter values

>> m=changeparamete r (m, ’ r , c ’, [ 0 . 1 0 . 5 ] ) ;

>> par =[0.2 1 ] ;>> m=changeparamete r (m, par ) ;

>> m=changeparamete r (m, ’ r ’ , 1 ) ;

Page 27: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.19

Basic operations: symbolic expressions

Operating with symbolic expressions

>> dxdt=canon i c a l s y s t em (m)

dxdt =

dynVar1−dynVar1ˆ3−1/2∗dynVar2 /cr∗dynVar2−2∗dynVar1−dynVar2∗(1−3∗dynVar1 ˆ2)

>> u=c o n t r o l (m)

u =

−1/2∗dynVar2 /c

>> dxd t s=s u b s p a r v a l (m, dxdt )

dxd t s =

dynVar1−dynVar1ˆ3−1/2∗dynVar22∗dynVar2−2∗dynVar1−dynVar2∗(1−3∗dynVar1 ˆ2)

Page 28: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.20

Starting the analysis

Standing assumptions

1 An optimal solution exists for all initial states in the region ofinterest.

2 The optimal paths converge to a steady state or limit cycle.

3 The problem is normal λ0 = 1.

4 An optimal steady state corresponds to a steady state of thecanonical system.

Remark

Beside the proof of existence, all the other assumptions can berelaxed and diverging solutions can be analyzed as well asabnormal problems λ0 = 0.

Page 29: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.21

First Step: Calculating equilibria

Using the symbolic toolbox

>> ocEP=ca l c e p (m) ; b=i s a dm i s s i b l e (m, ocEP ) , ocEP (˜b ) =[ ]

Search f o r z e r o sb =

1 0 0 0 0ocEP =

[1 x1 d y n p r im i t i v e ]>> ocEP{1}ans =

d y n p r im i t i v e o b j e c t :Coo rd i n a t e s :

00

Arc i d e n t i f i e r : 1L i n e a r i z a t i o n : [ 2 x2 doub le ]

>> m=s t o r e (m, ocEP ) ;>> m. o cRe su l t sans =

Equ i l i b r i um : { [1 x1 d y n p r im i t i v e ]}>> ocEP=e q u i l i b r i um (m) ;

Page 30: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.22

Determine properties of equilibria

Jacobian

>> J=ocEP{1} . l i n e a r i z a t i o nJ =

1.0000 −0.5000−2.0000 0

Eigenvalues

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

1 .6180−0.6180

Evaluate: Control

>> u=c o n t r o l (m, ocEP{1})u =

0 0

Evaluate: Hamiltonian

>> u=ham i l t o n i a n (m, ocEP{1})u =

0 0

Coordinates

>> ocEP{1} . dynVarans =

0 00 0

Structure

>> s t r u c t ( ocEP{1})ans =

pe r i o d : 0l i n e a r i z a t i o n : [ 2 x2 doub le ]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 ]

Page 31: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.23

The class octrajectory

Remark

The class octrajectory represents a time path of the canonicalsystem.

Constructor

>> ocTr j=o c t r a j e c t o r y ; s t r u c t ( ocTr j )ans =

dynVar : [ ]t : [ ]

t i m e i n t e r v a l s : [ ]a r c i d : [ ]

Basic fields

• Vector of solution points, ocTrj .dynVar

• Time grid normalized to [0, 1], ocTrj . t

• Not normalized timeinterval, ocTrj . timeintervals

• Arc Identifier characterizing active constraints, ocTrj . arcid

Page 32: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.24

The class dynprimitive

Remark

The class dynprimitive represents the “primitive” solutions,equilibria or limit cycles, of the canonical system.

Constructor

>> ocDyn=dy n p r im i t i v e ( ) ; s t r u c t ( ocDyn )ans =

pe r i o d : [ ]l i n e a r i z a t i o n : [ ]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 ]

Basic fields

• period, which is zero for equilibria, ocDyn.period

• Jacobian (Monodromy) matrix, ocDyn. linearization

• the solution path, ocDyn.octrajectory

Remark

Since the time grid of an octrajectory consists at least of 0 and 1,the coordinate values of an equilibrium, are doubled.

Page 33: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.25

The class ocasymptotic

Remark

The class ocasymptotic represents a solution converging to anequilibrium (limit cycle).

Constructor

>> ocAsym=ocasympto t i c ( ) ; s t r u c t ( ocAsym )ans =

l i m i t s e t : [ 1 x1 d y n p r im i t i v e ]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 ]

Basic fields

• the equilibrium or limit cycle is stored in limitset,ocAsym.limitset

• the solution path, ocAsym.octrajectory

Page 34: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.26

The class occurve

Remark

The class occurve represents a set of points in the state-costatetogether with an identifier for the active constraints.

Constructor

>> ocCur=occu rve ( ) ; s t r u c t ( ocCur )ans =

dynVar : [ ]a r c i d : [ ]

Basic fields

• the points of the state-costate are stored in, ocCur.dynVar

• the identifier for the active constraint is stored in, ocCur.arcid

Page 35: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Introduction

General remarks

Outlook

Example: Mom

Matlab: Data types

Numeric types

Strings and characters

Symbolic objects

Cells

Structures

Classes and objects

Initialize model ’MoM’

The canonical system

Basic operations

Starting the analysis

Calculating equilibria

Properties ofequilibria

Solution classes

octrajectory

dynprimitive

ocasymptotic

occurve

References

OCMat functions

1.27

References

G. Feichtinger and R.F. Hartl. Optimale Kontrolle okonomischerProzesse: Anwendungen des Maximumprinzips in denWirtschaftswissenschaften. Walter de Gruyter, Berlin, 1986. URLhttp://www.univie.ac.at/bwl/prod/hp/hartl/FeiHaBook/OCOP.pdf

D. Grass, J.P. Caulkins, G. Feichtinger, G. Tragler, and D.A. Behrens.Optimal Control of Nonlinear Processes: With Applications in Drugs,Corruption, and Terror. Springer Verlag, Berlin, 2008

A. Seierstad and K. Sydsaeter. Optimal Control Theory with EconomicApplications. North-Holland, Amsterdam, 1986

J.P. Caulkins, G. Feichtinger, D. Grass, and G. Tragler. A model ofmoderation: Finding Skiba points on a slippery slope. Central EuropeanJournal of Operations Research, 45–64(1):13, 2005

J.P. Caulkins, G. Feichtinger, D. Grass, and G. Tragler. Bifurcating DNSthresholds in a model of organizational bridge building. Journal ofOptimization Theory and Applications, 133(1):19–35, 2007

MATLAB documentation:

http://www.mathworks.com/help/techdoc/index.html

Page 36: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Initialization

initocmat

Constructor

Generating files

Basic operations

Saving/loading amodel

Parameter operations

Options structure

Setting options

Default options

Numerical analysis

Equilibria

Storing results

Handle results

Term evaluation

Control values

Other evaluations

BVP solver

1.28

Initialization

Initialization function

f u n c t i o n [ ocPro , fonCon , mode l I n f o ]= i n i t o cma t (modelname )%% INITOCMAT d e r i v e s and s t o r e s impor tan t i n f o rma t i o n from the oc model%% INITOCMAT(NAME) d e r i v e s i n f o rma t i o n from the model NAME, where NAME i s% a l s o the models name . I t i s r e q u i r e d th a t a MATLAB f i l e NAME( .M) e x i s t s , which% i n c l u d e s the d e f i n i n g p r o p e r t i e s o f the model . The d e f a u l t d i r e c t o r y f o r the% i n i t i a l i z a t i o n f i l e i s .\ocmat\model\ i n i t f i l e s%% INITOCMAT(NAME,OPT) wi th OPT s p c i f i c o p t i o n s du r i gn the i n i t i a l i z a t i o n can be% prov ided , e . g . , opt=s e t o c o p t i o n s ( ’ INIT ’ , ’ S imp l i f y ’ , ’ a ’ ) , s e e the ’ op t i on ’ s e c t i o n .%% [OCPRO,FONCON,MODELINFO]=INITOCMAT(NAME, . . . ) the output arguments OCPRO,FONCON,% MODELINFO ar e s t r u c t u r e s , where OCPRO summar izes the i n f o rma t i o n from the% i n i t i a l i z a t i o n f i l e . FONCON con t a i n s the s ymbo l i c e x p r e s s i o n s d e r i v e d from the% f i r s t o r d e r o p t im a l i t y c o n d i t i o n s . And the s t r u c t u r e MODELINFO con t a i n s g e n e r a l% i n f o rma t i o n about the model .

% These s t r u c t u r e s a r e s t o r e d i n a MAT−f i l e a t the d e f a u l t models data d i r e c t o r y ,% e . g . , .\ocmat\data\mom, named by the models ’ name and an appended Data , e . g . ,% momData . mat . These s t r u c t u r e s a r e ma in l y used du r i n g the au tomat i c f i l e g e n e r a t i o n% p r o c e s s and f o r t h e c o n s t r u c t o r method o f the model o b j e c t .

Basic structure of initialization file

s t a t ed ynam i c s=sym( ’ x1−x1ˆ3+u1 ’ ) ; % mandatory s t a t e v a r i a b l e : x1 , x2 , . . .o b j e c t i v e f u n c t i o n=sym( ’ x1ˆ2+u1ˆ2 ’ ) ; % mandatory c o n t r o l v a r i a b l e : u1 , u2 , . . .% Gene r a lr =1; % mandatory d i s c o u n t r a t e : r

Page 37: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Initialization

initocmat

Constructor

Generating files

Basic operations

Saving/loading amodel

Parameter operations

Options structure

Setting options

Default options

Numerical analysis

Equilibria

Storing results

Handle results

Term evaluation

Control values

Other evaluations

BVP solver

1.29

Object constructor

Constructor

f u n c t i o n ocObj = ocmodel ( v a r a r g i n )%% OCMODEL c o n s t r u c t o r f o r an ocmodel%% OCMODEL() r e t u r n s an empty model%% OCMODEL(NAME) c o n s t r u c t s an ocmodel f o r model NAME. The c o n s t r u c t o r l o ad s the data% p r e v i o u s l y s t o r e d du r i n g the i n i t i a l i z a t i o n p r o c e s s ’ i n i t o cmat ’ .%% OCOBJ=OCMODEL(NAME) the ob j e c t i s r e t u r n e d to OCOBJ, which i s now an OCMODEL c l a s s .% The c l a s s f u n c t i o n s a r e s t o r e d i n the d i r e c t o r y .\ocmat\ c l a s s e s\mode lC l a s s e s\@ocmodel

.% This c o n s t r u c t i o n a l l ow s to o v e r l o ad MATLAB func t i on , e . g . the p l o t t i n g commands p l o t

.% This means t h a t f u n c t i o n s w i th the same MATLAB name and an ocmodel as argument a r e% f i r s t s e a r ch ed i n the c l a s s d i r e c t o r y .% The c l a s s OCOBJ a r e ma in l y d e f i n e d as a s t r u c t u r e c o n s i s t i n g o f f o u r f i e l d s , which% ar e a l s o s t r u c t u r e s . The main f i e l d s a r e :% o c I n s t a n t . ParameterValue . . . c o n t a i n i n g the a c t u a l v a l u e s o f the model ,% when the paramete r v a l u e s a r e changed , t h i s f i e l d i s changed

i n f a c t% o c I n s t a n t . TimeHorizon . . . which i s s e t to i n f by d e f a u l t% o c I n f o s : c o n t a i n s the s t r u c t u r e ’ mode l In fo ’ r e t u r n e d by i n i t o cma t% ocPro : c o n t a i n s the s t r u c t u r e ’ mode l In fo ’ r e t u r n e d by i n i t o cma t% ocRe s u l t s : i s empty a f t e r i n i t i a l i z a t i o n and a l l c a l c u a l a t i o n s can be s t o r e d i n% t h i s s t r u c t u r e u s i n g the command STORE, s e e d e s c r i p t i o n .

Page 38: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Initialization

initocmat

Constructor

Generating files

Basic operations

Saving/loading amodel

Parameter operations

Options structure

Setting options

Default options

Numerical analysis

Equilibria

Storing results

Handle results

Term evaluation

Control values

Other evaluations

BVP solver

1.30

Automatic file generation

Generating files

f u n c t i o n f i l e s 4m o d e l ( ocObj , v a r a r g i n )%% FILES4MODEL c r e a t e f i l e s f o r nume r i c a l a n a l y s i s%% FILES4MODEL(OCOBJ) g en e r a t e s the MATLAB f i l e s f o r the nume r i c a l a n a l y s i s o f the model

.% The gen e r a t ed f i l e s a r e s t o r e d i n the d i r e c t o r y .\ocmat\model\out .%% FILES4MODEL(OCOBJ,OPT) wi th OPT some d e f a u l t s e t t i n g s can be chaged , s e e s e c t i o n

OPTIONS .

Moving files

f u n c t i o n moveocmat f i l e s ( ocObj , v a r a r g i n )%% MOVEOCMATFILES moves the f i l e s to a d i r e c t o r y%% MOVEOCFILES(MODELNAME) moves the model f i l e s from the s t anda rd output% d i r e c t o r y to the s t anda rd model d i r e c t o r y .%% MOVEOCFILES(MODELNAME,FORCE) I f FORCE=1 f i l e s i n the t a r g e t d i r e c t o r y% ar e f o r c e d to be o v e rw r i t t e n . De f au l t i s 0 .% I f the t a r g e t d i r e c t o r y does not e x i s t i t w i l l be c r e a t e d and i s added to% the MATLAB Path .%% [STATUS ,RESULT] = MOVEOCFILES(MODELNAME,FORCE) The r e s u l t i n g s t a t u s% and s t anda rd output from the op e r a t i n g system ar e r e t u r n e d .

Page 39: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Initialization

initocmat

Constructor

Generating files

Basic operations

Saving/loading amodel

Parameter operations

Options structure

Setting options

Default options

Numerical analysis

Equilibria

Storing results

Handle results

Term evaluation

Control values

Other evaluations

BVP solver

1.31

Saving/loading a model

Save a model

f u n c t i o n save ( ocObj , v a r a r g i n )%% SAVE sav e s an op t ima l c o n t r o l model%% SAVE(OCOBJ) s av e s the data o f an op t ima l c o n t r o l model i n the s t anda rd% data d i r e c t o r y w i th the name composed o f the name o f the model and i t s% paramete r v a l u e s .%% SAVE(OCOBJ,FORMAT) u s e s the format s t r i n g FORMAT ( s ee SPRINTF f o r% d e t a i l s ) f o r the paramete r v a l u e s .%% SAVE(OCOBJ,FORMAT,FORCE) FORCE = 1 f o r c e s to o v e rw r i t e an a l r e a d y% e x i s t i n g f i l e . De f au l t v a l u e i s 0 .%% SAVE(OCOBJ,FORMAT,FORCE,FN) FN p r o v i d e s an a l t e r n a t i v e f i l e n ame .%% SAVE(OCOBJ,FORMAT,FORCE , [ ] , IDX) f i l e n ame i s g en e r a t ed o f the model% name and the paramete r v a l u e s f o r the IDX ’ th paramete r v a l u e s .

Load a previously saved model

>> l o ad ( ’ mom p1 1 p2 1 p3 In f . mat ’ )>> whos % shows the v a r i a b l e s i n the work ing space

Name S i z e Bytes C l a s s A t t r i b u t e s

ocObj 1x1 9421 ocmodel

Page 40: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Initialization

initocmat

Constructor

Generating files

Basic operations

Saving/loading amodel

Parameter operations

Options structure

Setting options

Default options

Numerical analysis

Equilibria

Storing results

Handle results

Term evaluation

Control values

Other evaluations

BVP solver

1.32

Showing/changing parameter values

Changing parameter values

f u n c t i o n ocObj=changeparameter ( ocObj , v a r a r g i n )%% CHANGEPARAMETER r e t u r n s oc model w i th new paramete r s%% CHANGEPARAMETER(OCOBJ,PAR) r e t u r n s an op t ima l c o n t r o l model w i th changed% paramete r v a l u e s . I f PAR i s not s e t o r empty , the d e r i v e d c l a s s ocmodel i s% c l e a r e d and the o r i g i n a l pa ramete r v a l u e s a r e s e t .%% CHANGEPARAMETER(OCOBJ, IDX ,VALUE) a s s i g n s the v a l u e VALUE to the paramete r% wi th i nd ex IDX and r e t u r n s the ocmodel w i th t h i s changed paramete r . One% or more paramete r s can be changed , an example f o r chang ing one paramete r% would be ocObj=changeparameter ( ocObj , 1 , 0 . 0 5 ) , f o r chang ing more paramete r s% ocObj=changeparameter ( ocObj , [ 1 2 3 ] , [ 6 5 4 ] ) .%% CHANGEPARAMETER(OCOBJ,NAME,VALUE) a s s i g n s a c e r t a i n v a l u e VALUE to the% paramete r w i th the name NAME and r e t u r n s the oc model w i th t h i s changed% paramete r . One or more paramete r s can be changed , names o f d i f f e r e n t% paramete r s have to be s e p e r a t e d by a comma e . g .% ocObj=changeparameter ( ocObj , ’ a lpha , beta ’ , [ 1 2 ] ) .

Showing parameter values

f u n c t i o n v a r a r g ou t=showparameter ( ocObj , v a r a r g i n )

% SHOWPARAMETER d i s p l a y s paramete r s o f the ob j e c t%% SHOWPARAMETER(OCOBJ)%% SHOWPARAMETER(OCOBJ,FORMAT) d i s p l a y s the pa r ame t e r v a l u e s , u s i n g format% FORMAT ( See f p r i n t f f o r format s t r i n g d e t a i l s . )%% [PARVAL PARARG]=SHOWPARAMETER(OCOBJ , . . . ) a d d i t o n a l l y r e t u r n s the% paramete r v a l u e s and names .

Page 41: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Initialization

initocmat

Constructor

Generating files

Basic operations

Saving/loading amodel

Parameter operations

Options structure

Setting options

Default options

Numerical analysis

Equilibria

Storing results

Handle results

Term evaluation

Control values

Other evaluations

BVP solver

1.33

Setting options

f u n c t i o n op t s=s e t o c o p t i o n s ( v a r a r g i n )

% SETOCOPTIONS l e t s you a d j u s t the op t i o n s o f the OCMAT too l b ox%% OPT=SETOCOPTIONS(CAT1,NAME1,VALUE1 ,CAT2,NAME2,VALUE2 , . . . ) r e p l a c e s the% v a l u e s o f the op t i o n s NAME1, NAME2 , . . . i n the c a t e g o r i e s CAT1,CAT2 , . . o f% the d e f a u l t op t i on s t r u c t u r e w i th new v a l u e s VALUE1 , VALUE2 , . . . and% r e t u r n s an op t i on s t r u c t u r e w i th the changed op t i o n s .%% OPT=SETOCOPTIONS(OPT,CAT1,NAME1,VALUE1 ,CAT2,NAME2,VALUE2 , . . . ) r e p l a c e s the% v a l u e s o f the op t i o n s NAME1, NAME2 , . . . i n the c a t e g o r i e s CAT1,CAT2 , . . o f% the op t i on s t r u c t u r e OPT wi th new v a l u e s VALUE1 , VALUE2 , . . . and% r e t u r n s an op t i on s t r u c t u r e w i th the changed op t i o n s .%% OPT=SETOCOPTIONS(OPT,CAT1,NAME1,VALUE1 ,NAME2,VALUE2 ,CAT2,NAME3,VALUE3 , . . )% r e p l a c e s the v a l u e s o f the op t i o n s NAME1, NAME2 o f the ca t ego r y CAT1% of the op t i on s t r u c t u r e OPT wi th the v a l u e s VALUE1 and VALUE2 and% op t i on NAME3 o f ca t ego r y CAT2 wi th v a l u e VALUE3 e t c . and r e t u r n s the% changed op t i on s t r u c t u r e

Page 42: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Initialization

initocmat

Constructor

Generating files

Basic operations

Saving/loading amodel

Parameter operations

Options structure

Setting options

Default options

Numerical analysis

Equilibria

Storing results

Handle results

Term evaluation

Control values

Other evaluations

BVP solver

1.34

Default options

Option categories and its default values

f u n c t i o n opt = d e f a u l t o c o p t i o n s ( )

% Crea t e s and r e t u r n s a s t r u c t u r e c o n t a i n i n g a l l d e f a u l t o p t i o n s ( i n c l u d i n g% some MATLAB and MATCONT p r e d e f i n e d op t i on s t r u c t u r e s ) f o r the OCMAT too l b ox .%% The i n t e n t i o n o f t h i s s t r u c t u r e i s to a l l ow a compact h an d l i n g o f the% d i f f e r e n t o p t i o n s . I t c o n s i s t s o f s e v e r a l s u b s t r u c t u r e s each c o n t a i n i n g% d e f a u l t o p t i o n s needed f o r d i f f e r e n t computat i on s o f the t oo l b ox .%% The s t r u c t u r e c o n s i s t s o f s e v e r a l s u b s t r u c t u r e s , which can be i d e n t i f i e d% by t h e i r name and which c on t a i n op t i o n s needed f o r d i f f e r e n t a p p l i c a t i o n s% w i t h i n the t oo l b ox :% ’OC’ . . . g e n e r a l OCMat op t i o n s% ’ INIT ’ . . . o p t i o n s f o r model i n i t i a l i z a t i o n% ’EP ’ . . . ( s e e op t ims e t )op t im i z a t i o nToo l b o xF l a g=˜i s empty ( s t r f i n d ( [ ma t l a b v e r s i o n . Name ] , ’ Op t im i za t i on Toolbox ’ ) ) ;%OC De fau l t Opt ions − Needed i n d i f f e r e n t f u n c t i o n s o f the OCMat too l b oxd e f a u l t o c=s t r u c t ( ’ BVPSolver ’ , ’ bvp4c ’ , ’ FSo l v e r ’ , ’ f s o l v e ’ , ’ ODESolver ’ , ’ ode45 ’ , . . .

’ L i n e a r i z a t i o nD i s t a n c e ’ ,1 e−5, ’ A dm i s s i b i l i t y T o l e r a n c e ’ ,1 e−6) ;

%I n i t i a l i z a t i o n Opt ions − Used f o r i n i t i a l i z i n g an ocmat problemd e f a u l t i n i t=s t r u c t ( ’ S imp l i f y ’ , ’ n ’ , ’ Jacob ian ’ , ’ e x p l i c i t ’ , ’ Paramete rJacob ian ’ , ’ e x p l i c i t ’

, ’ S k i p C a l c u l a t i o n s ’ , ’ o f f ’ , ’ Exc ludeCases ’ , ’ o f f ’ ) ;

% Eq u i l i b r i a Opt ions − Used f o r the numer ic c a l c u l a t i o n o f e q u i l i b r i ai f o p t im i z a t i o nToo l b o xF l a g

d e f a u l t e p=op t ims e t ( ’ D i s p l a y ’ , ’ o f f ’ , ’ J acob ian ’ , ’ o f f ’ , ’ MaxFunEvals ’ ,500 , ’ Max I t e r ’, 500) ;

e l s ed e f a u l t e p = [ ] ;

end

Page 43: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Initialization

initocmat

Constructor

Generating files

Basic operations

Saving/loading amodel

Parameter operations

Options structure

Setting options

Default options

Numerical analysis

Equilibria

Storing results

Handle results

Term evaluation

Control values

Other evaluations

BVP solver

1.35

Default options (2)

Option categories and its default values

• OC

BVPSolver: used boundary value solver, [bvp4c,bvp5c]ODESolver: used initial value solver, [ode45,ode23,ode113, ...]

FSolver: used solver for system of equations, e.g., zeros of canonicalsystem, [ fsolve ] (needs the optimization toolbox)

LinearizationDistance: the ε for manifold path calculations using the initial valueapproach, [R+]

• INIT

Simplify: simplifying the symbolic expressions during the initialization

process:

• ’n’ ... no simplifying• ’a’ ... automatic simplifying• ’m’ ... user is asked

Jacobian: calculating the Jacobian

• ’explicit’ ... explicit derivation during initialization• ’implicit’ ... implicit derivation during initialization• ’numeric’ ... numeric approximation

ParameterJacobian: see ’Jacobian’SkipCalculations: important in case of implicitly given control values. During

initialization user is asked if MATLAB shall try to find explicitsolution or not.

• EP: these are described in MATLAB help for optimset.

Page 44: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Initialization

initocmat

Constructor

Generating files

Basic operations

Saving/loading amodel

Parameter operations

Options structure

Setting options

Default options

Numerical analysis

Equilibria

Storing results

Handle results

Term evaluation

Control values

Other evaluations

BVP solver

1.36

Calculating equilibria/Stability properties

Calculation of equilibria

f u n c t i o n [ ocEP , v a r a r g ou t ] = ca l c e p ( ocObj , v a r a r g i n )%% CALCEP c a l c u l a t e s e q u i l i b r i a o f oc model .%% CALCEP(OCOBJ) c a l c u l a t e s the e q u i l i b r i um f o r the c a n o n i c a l sys tem o f the% oc model OCOBJ u s i n g the c a l c t y p e t oo l b ox .%% CALCEP(OCOBJ, X0) c a l c u l a t e s the e q u i l i b r i a n ume r i c a l l y w i th s t a r t i n g% v a l u e s g i v en by X0 . I f X0 i s empty c a l c t y p e c a l c u l a t i o n i s a p p l i e d .%% CALCEP(OCOBJ, X0 ,OPT) change the d e f a u l t o p t i o n s f o r FSOLVE . OPT i s an% ocop t i on s−s t r u c t u r e .%% CALCEP(OCOBJ, X0 ,OPT,ARCID) c a l c u l a t e the e q u i l i b r i a on l y f o r the a r c% g i v en by the i nd ex ARCID .%% OCEP = CALCEP ( . . . ) the argument OCEP i s o f c l a s s ’ d y n p r im i t i v e ’

Eigenvalues/Eigenvectors

f u n c t i o n v a r a r g ou t=e i g ( dynPrim )%% EIG f i n d e i g e n v a l u e s and e i g e n v e c t o r s .%% D = EIG (DYNPRIM) r e t u r n s the e i g e n v a l u e s f o r the Jacob ian /Monodromy% mat r i x o f the d y n p r im i t v e ob j e c t DYNPRIM.%% [D,V] = EIG (DYNPRIM) p roduces ma t r i c e s o f e i g e n v a l u e s (D) and e i g e n v e c t o r s% (V) ( f o r more i n f o rma t i o n h e l p e i g ) .

Page 45: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Initialization

initocmat

Constructor

Generating files

Basic operations

Saving/loading amodel

Parameter operations

Options structure

Setting options

Default options

Numerical analysis

Equilibria

Storing results

Handle results

Term evaluation

Control values

Other evaluations

BVP solver

1.37

Testing for admissibility and saddle property

f u n c t i o n [ b , v a r a r g ou t ]= i s a dm i s s i b l e ( ocObj , v a r a r g i n )%% ISADMISSIBLE t e s t i f e q u i l i b r i um / l i m i t s e t i s a dm i s s i b l e%% ISADMISSIBLE (OCOBJ,DYNPRIM) t e s t s i f th e d y n p r im i t i v e o b j e c t DYNPRIM s a t i s f i e s the

c o n s t r a i n t s% o f the model . I f DYNPRIM i s an e q u i l i b r i um i t i s a l s o checked i f the dynamics i s z e r o

.%% ISADMISSIBLE (OCOBJ,DYNPRIM,OPT) OPT.OC. ADMISSIBILITY p r o v i d e s the t o l e r a n c e f o r% a dm i s s i b i l i t y .%% B = ISADMISSIBLE (OCOBJ,DYNPRIM,OPT) i f the c r i t e r i a a r e s a t i f i e d B=1% o th e rw i s e B=0.%% [B V]= ISADMISSIBLE (OCOBJ,DYNPRIM,OPT) V i s the v e c t o r c o n t a i n i n g the% co r r e s p on d i n g v i o l a t i o n matr ix , where an en t r y 1 s t and s f o r a v i o l a t i o n% o f the c o n s t r a i n t and 0 o t h e rw i s e .%% [B V Z]= ISADMISSIBLE (OCOBJ,DYNPRIMJ ,OPT) i f DYNPRIM i s an e q u i l i b r i um the% va l u e Z r e t u r n s the norm o f the c o r r e s p on d i n g dynamics .%% I f DYNPRIM i s a c e l l o f d y n p r im i t i v e o b j e c t s a d m i s s i b i l i t y i s checked f o r each c e l l ,

and% the output arguments a r e a l s o r e t u r n e d a c e l l v a r i a b l e s .

f u n c t i o n [ b , dim]= i s s a d d l e ( v a r a r g i n )%% ISSADDLE t r u e i f l i m i t s e t i s o f s a d d l e t ype%% B=ISSADDLE(DYNPRIM) r e t u n r s 1 i f d y n p r i m i t i v e o b j e c t DYNPR i s a s ad d l e .%% [B DIM]=ISSADDLE(DYNPRIM) r e t u r n s a l s o the d imens ion DIM.%% [B DIM]=ISSADDLE(DYNPRIM1 , . . . , DYNPRIMN) r e t u r n s v e c t o r s B and DIM, c o n t a i n i n g 1 f o r% s a d d l e s 0 o t h e rw i s e and d imens ion DIM f o r each s ad d l e .

Page 46: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Initialization

initocmat

Constructor

Generating files

Basic operations

Saving/loading amodel

Parameter operations

Options structure

Setting options

Default options

Numerical analysis

Equilibria

Storing results

Handle results

Term evaluation

Control values

Other evaluations

BVP solver

1.38

The store command

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) s t o r e data g i v en by the MAT f i l e DATAFILE 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,OBJ) s t o r e s the ob j e c t OBJ i n the r e s u l t s o f the oc% ob j e c t OCOBJ (OBJ 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% OBJ a r e% 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%%% 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 47: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Initialization

initocmat

Constructor

Generating files

Basic operations

Saving/loading amodel

Parameter operations

Options structure

Setting options

Default options

Numerical analysis

Equilibria

Storing results

Handle results

Term evaluation

Control values

Other evaluations

BVP solver

1.39

Handle stored results

Retrieve equilibria

f u n c t i o n ocEP = e q u i l i b r i um ( ocObj )%% EQUILIBRIUM r e t u r n s the e q u i l i b r i a s t o r e d i n ocObj%% OCEP=EQUILIBRIUM(OCOBJ) r e t u r n s c e l l a r r y o f e q u i l i b r i a s t o r e d i n the o cRe s u l t s .

Delete 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 a field from the ocResultsf 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 o cRe 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 48: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Initialization

initocmat

Constructor

Generating files

Basic operations

Saving/loading amodel

Parameter operations

Options structure

Setting options

Default options

Numerical analysis

Equilibria

Storing results

Handle results

Term evaluation

Control values

Other evaluations

BVP solver

1.40

Evaluating control values

f u n c t i o n out = c o n t r o l ( ocObj , v a r a r g i n )%% CONTROL r e t u r n s the op t ima l c o n t r o l v a l u e / e x p r e s s i o n .%% CONTROL(OCOBJ) r e t u r n s the s ymbo l i c e x p r e s s i o n f o r the op t ima l c o n t r o l% v a l u e f o r the a r c i d e n t i f i e r 1 .%% CONTROL(OCOBJ, ARCID) r e t u r n s the s ymbo l i c e x p r e s s i o n f o r the op t ima l% c o n t r o l v a l u e f o r the a r c i d e n t i f i e r ARCID . I f ARCID i s a v e c t o r a c e l l% a r r a y o f s ymbo l i c e x p r e s s i o n s i s r e t u r n e d f o r each o f the v a l u e s o f% ARCID .%% CONTROL(OCOBJ,X, ARCID) i f X i s numer ic v e c t o r o r matr ix , the u s e r can% p r o v i d e an a r c i d e n t i f i e r ARCID f o r each column , o r a s i n g l e v a l u e i f i t% i s the same f o r each o f the column v e c t o r s .%% CONTROL(OCOBJ,X, ARCID ,COORD)%% CONTROL(OCOBJ,OCEL) r e t u r n s the op t ima l c o n t r o l s f o r the oc e l ement OCEL ,% which can be o f c l a s s ’ d y n p r im i t i v e ’ , ’ o c t r a j e c t o r y ’ , ’ ocasymptot i c ’ ,% ’ occurve ’ , o r an ode s t r u c t u r e .%% CONTROL(OCOBJ,OCEL ,COORD) r e t u r n s the op t ima l c o n t r o l s f o r the oc e l ement OCEL ,%% CTRL = CONTROL(OCOBJ , . . . ) the output argument i s a c e l l a r r a y i n case% tha t the i n p u t argument i s a c e l l a r r a y o f o b j e c t s o t h e rw i s e i t i s an% a r r a y c o n t a i n i g the op t ima l c o n t r o l v a l u e s .

Page 49: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Initialization

initocmat

Constructor

Generating files

Basic operations

Saving/loading amodel

Parameter operations

Options structure

Setting options

Default options

Numerical analysis

Equilibria

Storing results

Handle results

Term evaluation

Control values

Other evaluations

BVP solver

1.41

Other evaluation commands

f u n c t i o n out = can on i c a l s y s t em ( ocObj , v a r a r g i n )%% CANONICALSYSTEM r e t u r n s the v a l u e / e x p r e s s i o n o f the c a n o n i c a l sys tem .%

f u n c t i o n out = c o s t a t e ( ocObj , v a r a r g i n )%% COSTATE r e t u r n s the op t ima l c o s t a t e v a l u e / e x p r e s s i o n .%

f u n c t i o n out = ham i l t o n i a n ( ocObj , v a r a r g i n )%% HAMILTONIAN r e t u r n s the op t ima l Hami l ton ian v a l u e / e x p r e s s i o n .%

Manually adapt and invoke a user defined function

f u n c t i o n out = momUserFunction ( t , dynVar , pararg , a r c a r g )%% r e t u r n s the v a l u e s f o r a u s e r d e f i n e d f u n c t i o n o f mom model

% Parameter Valuer=pa ra r g (1 ) ;c=pa ra r g (2 ) ;T=pa ra r g (3 ) ;% Funct ion v a l u e sout = [ ] ;

f u n c t i o n out = u s e r f u n c ( ocObj , v a r a r g i n )%% USERFUNC r e t u r n s the v a l u e o f u s e r d e f i n e d e x p r e s s i o n s

Page 50: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Initialization

initocmat

Constructor

Generating files

Basic operations

Saving/loading amodel

Parameter operations

Options structure

Setting options

Default options

Numerical analysis

Equilibria

Storing results

Handle results

Term evaluation

Control values

Other evaluations

BVP solver

1.42

Manually adapt BVP solver bvp4c

bvp4c.m

272i f N > Nmax273warn ing ( ’MATLAB: bvp4c : RelTolNotMet ’ , . . .274[ ’ Unable to meet the t o l e r a n c e w i thou t u s i n g more than %d ’ . . .275’mesh p o i n t s . \n The l a s t mesh o f %d p o i n t s and ’ . . .276’ the s o l u t i o n a r e a v a i l a b l e i n the output argument . \n ’ , . . .277’The maximum r e s i d u a l i s %g , wh i l e r e q u e s t e d accu r acy i s %g . ’

] , . . .278Nmax , l e n g t h ( x ) ,max( r e s ) , r t o l ) ;279s o l . x = x ;280s o l . y = y ;281s o l . yp = yp ;282s o l . s o l v e r = so l v e r n ame ;283s o l . b c e v a l s=nBCeval ;284i f unknownPar285s o l . pa ramete r s = Ext raArgs{1};286end287r e t u r n288end289290nN = n∗N;291292needGlobJac = t r u e ;293294end295296end % wh i l e297298% Output299s o l . x = x ;300s o l . y = y ;301s o l . yp = yp ;302s o l . s o l v e r = so l v e r n ame ;303s o l . b c e v a l s=nBCeval ;

Page 51: Dieter Graß Lecture1: Firststeps: Implementationanda ......Dieter Graß Introduction General remarks Outlook Example: Mom Matlab: Data types Numeric types Strings and characters Symbolic

Dieter Graß

Initialization

initocmat

Constructor

Generating files

Basic operations

Saving/loading amodel

Parameter operations

Options structure

Setting options

Default options

Numerical analysis

Equilibria

Storing results

Handle results

Term evaluation

Control values

Other evaluations

BVP solver

1.43

Manually adapt BVP solver bvp5c

bvp5c.m

285% Output s o l u t i o n s t r u c t u r e286s o l . s o l v e r = so l v e r n ame ;287x = re shap e (X , 1 , [ ] ) ;288y = re shap e (Y, neqn , [ ] ) ;289i f nparam > 0290neqn = neqn − nparam ;291paramete r s = y ( neqn+1 : neqn+nparam , 1) ;292s o l . pa ramete r s = paramete r s ;293end294s o l . x = x ( 1 : n s t age s : end ) ;295s o l . y = y ( 1 : neqn , 1 : n s t age s : end ) ;296s o l . i d a t a . ymid = ymid ( 1 : neqn , : ) ;297s o l . i d a t a . yp = yp ( 1 : neqn , : ) ;298s o l . b c e v a l s=nBCeval ;