Dynamics Definition

Table of Contents

  1. ode. A object of class ode is a structure that contains a all information of a ordinary differential equation. This have a dynamic equation, initial condition, time span, etc.
  2. Methods

  3. pde1d. A object of class pde1d is a structure that contains a all information of a partial differential equation in one dimension. This have a dynamic equation, initial condition, time span, space span etc.
  4. Methods

  5. pde2d. A object of class pde2d is a structure that contains a all information of a partial differential equation in two dimension. This have a dynamic equation, initial condition, time span, space span etc.
  6. Methods

  7. pdefem. A object of class pdefem is a structure that contains a all information of a partial differential equation in two dimension in finite elements. This have a dynamic equation, initial condition, time span, space discretization, nodes and elements, etc.
  8. Methods

  9. linearode.
  10. Methods

  11. linearpde.
  12. Methods


Class:: ode

A object of class ode is a structure that contains a all information of a ordinary differential equation. This have a dynamic equation, initial condition, time span, etc.

Method::ode of Class::ode

Syntax

  • iode = ode(Fs,Xs,Us,tspan)

Interface

INPUTS
Name Class Description
Fs CasADi Function

This is a function $f:R^+\times\mathcal{X}\times\mathcal{U}$ that define the dynamics.

Xs SX.sym

It is a symbolical state vector, $x \in \mathcal{X}$.

Us SX.sym

it is a symbolical control vector, $u \in \mathcal{U}$

tspan double

The ode is a object that can be solved with the command “solve” but this need a time span to build a answer. The number of points in time is denote by $N_t$. It is $N_t = length(tspan)$

OUTPUTS
Name Class Description
iode ode ode object that contain all information of ode.

Example

Create a symbolical variable

clear
import casadi.*
xs = SX.sym('x',2,1);
us = SX.sym('u',2,1);
ts = SX.sym('t');

Define a dynamic as casadi Function

Fs = casadi.Function('F',{ts,xs,us},{ - xs(1) + us(1);
                                      - xs(2) + us(2)});
tspan = linspace(0,1,100);

And create a object ode

iode = ode(Fs,xs,us,tspan);
iode

iode = 

  ode with properties:

               tspan: [1x100 double]
                  ts: [1x1 casadi.SX]
          MassMatrix: [2x2 double]
              method: []
              solver: []
          DynamicFcn: [1x1 casadi.Function]
               State: [1x1 fun]
             Control: [1x1 fun]
    InitialCondition: [2x1 casadi.DM]
                  Nt: 100


Method::SetIntegrator of Class::ode

Syntax

  • SetIntegrator(iode,method)

Interface

INPUTS
Name Class Description
idyn ode

dynamic object

method string

It is a string that can be {BackwardEuler,RK4,RK8,casadi}. This parameter define the numerical schema that it will use to solve the problem

Example

Create a symbolical variable

clear
import casadi.*
xs = SX.sym('x',2,1);
us = SX.sym('u',2,1);
ts = SX.sym('t');

Define a dynamic as casadi Function

Fs = casadi.Function('F',{ts,xs,us},{ [- 2*xs(1) + us(1) ;
                                      - xs(2) + us(2)]    });
tspan = linspace(0,1.5,7);

And create a object ode

iode = ode(Fs,xs,us,tspan);
iode.InitialCondition = [1;1];
u0 = ZerosControl(iode);
iode.tspan = linspace(0,0.5,7);
SetIntegrator(iode,'RK4')
xt_RK4 = solve(iode,u0)

xt_RK4 = 

@1=1, 
[[@1, 0.897602, 0.805689, 0.723187, 0.649134, 0.582664, 0.523], 
 [@1, 0.946689, 0.89622, 0.848441, 0.80321, 0.76039, 0.719853]]

iode.tspan = linspace(0,0.5,7);
SetIntegrator(iode,'RK8')
xt_RK8 = solve(iode,u0)

xt_RK8 = 

@1=1, 
[[@1, 0.846482, 0.716531, 0.606531, 0.513417, 0.434598, 0.367879], 
 [@1, 0.920044, 0.846482, 0.778801, 0.716531, 0.659241, 0.606531]]

iode.tspan = linspace(0,0.5,7);
SetIntegrator(iode,'BackwardEuler')
xt_BE  = solve(iode,u0)

xt_BE = 

@1=1, 
[[@1, 0.833333, 0.694444, 0.578704, 0.482253, 0.401878, 0.334898], 
 [@1, 0.916667, 0.840278, 0.770255, 0.706067, 0.647228, 0.593292]]

Method::solve of Class::ode

Syntax

  • xt = solve(iode,u0)

Interface

INPUTS
Name Class Description
idyn ode

dynamic object

u0 double or casadi.DM

control initial guess

OUTPUTS
Name Class Description
xt double or casadi.DM dynamic solution in tspan

Example

Create a symbolical variable

clear
import casadi.*
xs = SX.sym('x',2,1);
us = SX.sym('u',2,1);
ts = SX.sym('t');

Define a dynamic as casadi Function

Fs = casadi.Function('F',{ts,xs,us},{ [- 2*xs(1) + us(1) ;
                                      - xs(2) + us(2)]    });
tspan = linspace(0,1,10);

And create a object ode

iode = ode(Fs,xs,us,tspan);
SetIntegrator(iode,'RK4')
iode.InitialCondition = [1;1];
u0 = ZerosControl(iode);
xt = solve(iode,u0);
xt

xt = 


[[1, 0.867043, 0.751763, 0.65181, 0.565147, 0.490007, 0.424857, 0.368369, 0.319392, 0.276926], 
 [1, 0.929876, 0.864669, 0.804035, 0.747653, 0.695225, 0.646473, 0.60114, 0.558985, 0.519787]]

class(xt)

ans =

    'casadi.DM'


Method::ZerosControl of Class::ode

Syntax

  • u0 = ZerosControl(iode)

Interface

INPUTS
Name Class Description
idyn ode

dynamic object

OUTPUTS
Name Class Description
u0 casadi.DM zero control

Example

Create a symbolical variable

clear
import casadi.*
xs = SX.sym('x',2,1);
us = SX.sym('u',2,1);
ts = SX.sym('t');

Define a dynamic as casadi Function

Fs = casadi.Function('F',{ts,xs,us},{ [- 2*xs(1) + us(1) ;
                                      - xs(2) + us(2)]    });
tspan = linspace(0,1,10);

And create a object ode

iode = ode(Fs,xs,us,tspan);
SetIntegrator(iode,'RK4')
u0 = ZerosControl(iode);
u0

u0 = 

@1=0, 
[[@1, @1, @1, @1, @1, @1, @1, @1, @1, @1], 
 [@1, @1, @1, @1, @1, @1, @1, @1, @1, @1]]

class(u0)

ans =

    'casadi.SX'




Class:: pde1d

A object of class pde1d is a structure that contains a all information of a partial differential equation in one dimension. This have a dynamic equation, initial condition, time span, space span etc.

Method::pde1d of Class::pde1d

Syntax

  • ipde1d = pde1d(Fs,Xs,Us,tspan,xline)

Interface

INPUTS
Name Class Description
Fs CasADi Function

This is a function $f:R^+\times\mathcal{X}\times\mathcal{U}$ that define the dynamics.

Xs SX.sym

It is a symbolical state vector, $x \in \mathcal{X}$.

Us SX.sym

it is a symbolical control vector, $u \in \mathcal{U}$

tspan double

The ode is a object that can be solved with the command “solve” but this need a time span to build a answer. The number of points in time is denote by $N_t$. It is $N_t = length(tspan)$

xline double

The number of points in time is denote by $N_x$. It is $N_x = length(xline)$.

OUTPUTS
Name Class Description
ipde1d pde1d ode object that contain all information of pde 1d.

Example

Create a symbolical variable

clear
import casadi.*
xs = SX.sym('x',2,1);
us = SX.sym('u',2,1);
ts = SX.sym('t');

Define a dynamic as casadi Function

Fs = casadi.Function('F',{ts,xs,us},{ - xs(1) + us(1);
                                      - xs(2) + us(2)});
tspan = linspace(0,1,100);
xline = linspace(0,1,100);

And create a object ode

ipde1d = pde1d(Fs,xs,us,tspan,xline);
ipde1d

ipde1d = 

  pde1d with properties:

               xline: [1x100 double]
               tspan: [1x100 double]
                  ts: [1x1 casadi.SX]
          MassMatrix: [2x2 double]
              method: []
              solver: []
          DynamicFcn: [1x1 casadi.Function]
               State: [1x1 fun]
             Control: [1x1 fun]
    InitialCondition: [2x1 casadi.DM]
                  Nt: 100




Class:: pde2d

A object of class pde2d is a structure that contains a all information of a partial differential equation in two dimension. This have a dynamic equation, initial condition, time span, space span etc.

Method::pde2d of Class::pde2d

Syntax

  • ipde1d = pde1d(Fs,Xs,Us,tspan,xline)

Interface

INPUTS
Name Class Description
Fs CasADi Function

This is a function $f:R^+\times\mathcal{X}\times\mathcal{U}$ that define the dynamics.

Xs SX.sym

It is a symbolical state vector, $x \in \mathcal{X}$.

Us SX.sym

it is a symbolical control vector, $u \in \mathcal{U}$

tspan double

The ode is a object that can be solved with the command “solve” but this need a time span to build a answer. The number of points in time is denote by $N_t$. It is $N_t = length(tspan)$

xline double

The number of points in time is denote by $N_x$. It is $N_x = length(xline)$.

yline double

The number of points in time is denote by $N_y$. It is $N_y = length(yline)$.

OUTPUTS
Name Class Description
ipde2d pde2d ode object that contain all information of pde 2d.

Example

Create a symbolical variable

clear
import casadi.*
us = SX.sym('u',2,1);
vs = SX.sym('v',2,1);
ts = SX.sym('t');

Define a dynamic as casadi Function

Fs = casadi.Function('F',{ts,us,vs},{ - us(1) + vs(1);
                                      - us(2) + vs(2)});
tspan = linspace(0,1,100);
xline = linspace(0,1,100);
yline = linspace(0,1,100);

And create a object ode

ipde2d = pde2d(Fs,us,vs,tspan,xline,yline);
ipde2d

ipde2d = 

  pde2d with properties:

               xline: [1x100 double]
               yline: [1x100 double]
                 xms: [100x100 double]
                 yms: [100x100 double]
               tspan: [1x100 double]
                  ts: [1x1 casadi.SX]
          MassMatrix: [2x2 double]
              method: []
              solver: []
          DynamicFcn: [1x1 casadi.Function]
               State: [1x1 fun]
             Control: [1x1 fun]
    InitialCondition: [2x1 casadi.DM]
                  Nt: 100




Class:: pdefem

A object of class pdefem is a structure that contains a all information of a partial differential equation in two dimension in finite elements. This have a dynamic equation, initial condition, time span, space discretization, nodes and elements, etc.

Method::pdefem of Class::pdefem

Syntax

  • ipde1d = pde1d(Fs,Xs,Us,Nodes,Elements)

Interface

INPUTS
Name Class Description
Fs CasADi Function

This is a function $f:R^+\times\mathcal{X}\times\mathcal{U}$ that define the dynamics.

Xs SX.sym

It is a symbolical state vector, $x \in \mathcal{X}$.

Us SX.sym

it is a symbolical control vector, $u \in \mathcal{U}$

tspan double

The ode is a object that can be solved with the command “solve” but this need a time span to build a answer. The number of points in time is denote by $N_t$. It is $N_t = length(tspan)$

Nodes double

Mesh nodes, specified as a matrix of real numbers. The matrix size is 2-by-Nnodes for a 2-D case and 3-by-Nnodes for a 3-D case. Nnodes is the number of nodes in the mesh. Node j has x, y, and z coordinates in column j of nodes.

Elements double

Mesh elements, specified as an integer matrix with 3, and Nelements columns, where Nelements is the number of elements in the mesh. Linear planar mesh or linear mesh on the geometry surface has size 3-by-Nelements. Each column of elements contains the indices of the triangle corner nodes for a surface element. In this case, the resulting geometry does not contain a full mesh. Create the mesh using the generateMesh function.

OUTPUTS
Name Class Description
ipdefem pdefem ode object that contain all information of pde in finite elements.

Example

Create a points an elements

t = linspace(pi/30,2*pi,30);
pgon = polyshape({[0.5*sin(t)]},{[0.5*cos(t)]});

tr = triangulation(pgon);
%
tnodes    = tr.Points';
telements = tr.ConnectivityList';

With these we can create a FEM matrices with help to MATLAB PDE Toolbox

model = createpde();
geometryFromMesh(model,tnodes,telements);

Define Equation

applyBoundaryCondition(model,'neumann','Edge',1:model.Geometry.NumEdges,'g',0);
specifyCoefficients(model,'m',0,'d',0,'c',1,'a',0,'f',0);

and generate mesh

hmax = 0.05;
generateMesh(model,'Hmax',hmax,'GeometricOrder','linear','Hgrad',2);

Get a Finite elements Matrices

FEM = assembleFEMatrices(model,'stiff-spring');
Ns = length(FEM.Fs);
import casadi.*
%
Us = SX.sym('w',Ns,1);
Vs = SX.sym('v',Ns,1);
ts = SX.sym('t');

Define the dynamic

Fs = casadi.Function('f',{ts,Us,Vs},{ FEM.Fs + FEM.Ks*Us + Vs });
%
tspan = linspace(0,2,50);
%
idyn = pdefem(Fs,Us,Vs,tspan,tnodes,tnodes);
SetIntegrator(idyn,'RK4')

Initial Condition

xms = Nodes(1,:)' ;yms = Nodes(2,:)' ;
% radial coordinates
rms  = sqrt(xms.^2+yms.^2);
thms = atan2(yms,xms);

U0 =  exp((-xms.^2-yms.^2)/0.25^2);
idyn.InitialCondition = U0(:);
%
Vt = ZerosControl(idyn);
Wt = solve(idyn,Vt);
%
Wt

Wt = 


[[0.0183156, 0.0179932, 0.0176703, ..., -0.0567949, -0.0654909, -0.0753068], 
 [0.0182246, 0.0178072, 0.0173858, ..., -0.0430701, -0.0483894, -0.0542836], 
 [0.0185783, 0.018069, 0.0175525, ..., -0.0577603, -0.0643154, -0.0715892], 
 ...,
 [0.141174, 0.140772, 0.140377, ..., 0.447323, 0.49929, 0.559693], 
 [0.122455, 0.122056, 0.12166, ..., 0.203015, 0.218401, 0.236377], 
 [0.102459, 0.102, 0.101541, ..., 0.139265, 0.149296, 0.161212]]



Class:: linearode



Class:: linearpde



Table of contents