<!-- #region -->
# Time domain model 

## Introduction

This section explains how to perform a time domain simulation starting from a mathematical model consisting of Ordinary Differential Equations (ODE's) over a transfer function to a state space model

## ODE model

Large group of mathematical model described by a set of differential equation can be rewritten as a system of **Ordinary Differential Equations** (ODEs). These ODE's are well-known in literature and can be solved in various software packages.  To be able to apply an ODE the mathematical model should not contain partial derivatives and the model equations should be explicit meaning that you should be able to isolate the highest derivative in every equation. 

An ordinary differential equation or ODE is an equation containing a function of one or multiple independent variable and its first order derivatives. 
The term "ordinary" is used in contrast with the term partial differential equation which may be with respect to more than one independent variable.

In general an ODE is specified as:

```{math}
:label: eq:ODE
\frac{d\mathbf{y(t)}}{dt} = \mathbf{F}(t, \mathbf{y})
```

\begin{equation}
\mathbf{y}(t_0) = \mathbf{y}_0
\end{equation}

where $\mathbf{y}=\left [ \begin{array}{c} y_1(t) \\ \vdots \\ y_n((t) \end{array} \right]$ is a n dimensional vector of variables and $\mathbf{F}=\left [ \begin{array}{c} F_1(t, \mathbf{y})  \\  \vdots  \\ F_n(t, \mathbf{y}) \end{array} \right]$ a vector of n functions. 

The vector of initital values for the variables of the ODE model is specified as $y_0$. 

We can identify 4 different types of ODE's: 

### Linear one-dimensional ODE

An examples of **a linear one-dimensional ODE** is :
\begin{equation}
 \frac{dx(t)}{dt} = 5 x(t) -4 \\
\end{equation}
where $x(t)$ is a time dependent function. 

### Non-linear multi-dimensional ODE

An examples of **a non-linear multi-dimensional ODE** is :
\begin{equation}
 \frac{dx(t)}{dt} = 5 x(t)^2 -4 \\
\end{equation}
where $x(t)$ is a time dependent function. 

### Linear multi-dimensional ODE

An examples of **a non-linear one dimensional ODE** is :
\begin{eqnarray}
 \frac{dx_1(t)}{dt} &=& 5 x_1(t) -4 \\
 \frac{dx_2(t)}{dt} &=& 5 x_2(t) -4x_1(t) -8  \\
\end{eqnarray}
where $x_1(t)$ and $x_2(t)$ are time dependent functions.

### Non-linear multi-dimensional ODE

An examples of **a non-linear multivariant ODE** is :
\begin{eqnarray}
 \frac{dx_1(t)}{dt} &=& 5 \sin(x_1(t)) -4 \\
 \frac{dx_2(t)}{dt} &=& 5 \cos(x_2(t)) -4x_1(t) -8  \\
\end{eqnarray}
where $x_1(t)$ and $x_2(t)$ are time dependent functions.


### Non-ODE systems

An example of a mathematical model differential equation that **cannot** be converted to an ODE is:

\begin{equation}
 (\frac{d^2x(t)}{dt^2})^2+ 5\frac{dx(t)}{dt} -4  =0,
\end{equation}

this type a equation can be solved by a Differential Algebraic Equation (DAE). 

**This is however outside the scope of this course**.

## What to do with higher order differential equations? 

Consider the Euler differential equation: 

```{math}
:label: eq:Euler
t^2 \frac{d^2y(t)}{dt^2}+at\frac{dy(t)}{dt}+ b y(t)=0
```

that is not of the ODE type because of the second order differential. 
We can convert equation {eq}`eq:euler` to an ODE by introducing a new variable $v(t) = \frac{dy(t)}{dt}$ that reduces the second order equation to a set of first order equations: 

\begin{eqnarray}
\frac{dy(t)}{dt} &=& v(t) \\
\frac{dv(t)}{dt} &=& \frac{1}{t^2} \left ( -a t v(t)- b y(t)   \right ).
\end{eqnarray}

The ODE model of {eq}`eq:ODE` then becomes $\mathbf{y}(t)=\left [ \begin{array}{c} y(t) \\ v(t) \end{array} \right]$ and $ \mathbf{F}(t, \mathbf{y}) = \left [ \begin{array}{c} v(t) \\   \frac{1}{t^2}\left ( -a t v(t)- b y(t)   \right ) \end{array} \right]$.

## Block diagram

A block diagram of a dynamic system is a **pictorial representation** of the functions performed by each component of the system and of the flow of signals within the system. Such a diagram depicts the interrelationships that exist among the various components. Differing from a purely abstract mathematical representation, a block diagram has the advantage of indicating the **signal flows** of the actual system more realistically.

In a block diagram, all system variables are linked to each other through functional blocks. The **functional block**, or simply block, is a symbol for the mathematical operation on the input signal to the block that produces the output. The transfer functions of the components are usually entered in the corresponding blocks, which are connected by arrows to indicate the direction of the flow of signals. Note that a signal can pass only in the direction of the arrows. Thus, a block diagram of a dynamic system explicitly shows a **unilateral property**.

In general, the **functional operation of a system can be visualized more readily** by examining a block diagram of the system than by examining the physical system itself. A block diagram contains information concerning dynamic behavior, but **it does not include any information about the physical construction** of the system. Consequently, many dissimilar and unrelated systems can be represented by the same block diagram.
Note that in a block diagram the main source of energy is not explicitly shown and that the block diagram of a given system is not unique. A number of different block diagrams can be drawn for a system, depending on the point of view of the analysis. (See Example {numref}`TF0`)

:::{figure-md} TF0
<img src="figures_src/TF0.png" alt="fishy" height="500px">

In the figure (a), (b), and (c) are block diagrams for the same system. (Many different block diagrams are possible for any given system.)
:::


The **advantages** of the block diagram representation of a system lie
in the fact that it is **easy to form** the overall block diagram for
the entire system merely by connecting the blocks of the components
according to the signal flow and that it is **possible to evaluate the
contribution of each component** to the overall performance of the
system.\
In general, the **functional operation of a system can be visualized
more readily** by examining a block diagram of the system than by
examining the physical system itself. A block diagram contains
information concerning dynamic behavior, but **it does not include any
information about the physical construction** of the system.
Consequently, many dissimilar and unrelated systems can be represented
by the same block diagram. Note that in a block diagram the main source
of energy is not explicitly shown and that the block diagram of a given
system is not unique. A number of different block diagrams can be drawn
for a system, depending on the point of view of the analysis.


:::{figure-md} summation
<img src="figures_src/summation.png" alt="fishy" height="200px">

A summation point.
:::


**Summation point**. {numref}`summation` shows a circle with a cross, the symbol that
stands for a summing operation. The plus or minus sign at each arrowhead
indicates whether the associated signal is to be added or subtracted. It
is important that the quantities being added or subtracted have the same
dimensions and the same units.

**Branch point**. A branch point is a point from which the signal from a block goes concurrently to other blocks or summing points.

**Block diagram of a closed-loop system**. Figure 3 is a block diagram of a closed-loop system. The output C(s) is fed back to the summing point, where it is compared with the input R(s). The closed-loop nature of the system is indicated clearly by the figure. The output X(s) of the block is obtained by multiplying the transfer function (s) by the input to the block, E(s).


:::{figure-md} closed
<img src="figures_src/block.png" alt="fishy" height="200px">

Block diagram of a closed loop system.
:::

Most linear and non-linear systems can be represented by a block diagram consisting of blocks, summing points, and branch points. When the output is fed back to the summing point for comparison with the input, it is necessary to convert the form of the output signal to that of the input signal. This conversion is accomplished by the feedback element whose transfer function is $H(s)$, as shown in Figure 3.


### Block diagram example

Consider a mass-spring-damper system with mass $m$, spring constant $k$ and damping factor $b$. The transfer function of this system is


```{math}
:label: eq:L0
 \frac{X(s)}{F(s)} = \frac{1}{ms^2 + bs + k}
```

A block diagram representation of the system is shown in {numref}`TF0`(a).

Notice that Equation {eq}`eq:L0` can be written as

\begin{equation}
(ms^2 + bs + k)X(s) = F(s)
\end{equation}

Rewriting the latter equation as

\begin{equation}
(ms^2 + bs)X(s) = F(s) - kX(s)
\end{equation}

we can obtain a different block diagram for the same system, as shown in {numref}`TF0`(b).

Equation {eq}`eq:L0` can also be rewritten as

\begin{equation}
F(s) - [kX(s) + bsX(s)] = ms^2X(s)
\end{equation}

or

\begin{equation}
\frac{1}{m}F(s) - \frac{k}{m}X(s) -\frac{b}{m} sX(s) = s^2X(s) \label{eq:2.2.2-1-2}
\end{equation}

we can obtain a different block diagram for the same system, as shown in {numref}`TF0`($c$).
