# Model a first order system with input delay - Padé approximant?

**URL:** <https://scilab.discourse.group/t/model-a-first-order-system-with-input-delay-pade-approximant/388>\
**Category:** Modeling and simulation\
**Created:** [February 7, 2024, 10:36am UTC](https://scilab.discourse.group/t/model-a-first-order-system-with-input-delay-pade-approximant/388 "2024-02-07T10:36:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![scelles](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@scelles](https://scilab.discourse.group/u/scelles)\
**Post date:** [February 7, 2024, 10:36am UTC](https://scilab.discourse.group/t/model-a-first-order-system-with-input-delay-pade-approximant/388/1 "2024-02-07T10:36:32Z")

</div>

Hello,

I would like to model a 1st order system with delay in Scilab (not XCos).

It was previously (in old versions of Scilab) possible to do

```auto
s = poly(0, 's');
H = syslin('c', 1*exp(-1.5*s) / (1+4*s))
time = linspace(0, 20, 51);
rep = csim('step', time, H);
plot(time, rep);

```

but it doesn’t seem to work anymore because of the delay `exp(-1.5*s)`

How to model such a system in Scilab?

Is the Padé approximant the most practical solution?

How to implement it in Scilab? Is there built-in function for that purpose?

Kind regards

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [February 7, 2024, 11:12am UTC](https://scilab.discourse.group/t/model-a-first-order-system-with-input-delay-pade-approximant/388/2 "2024-02-07T11:12:53Z")

</div>

Hello Sebastien,

That’s good to hear from you again, welcome to this new great Scilab place!

Scilab itself does no handle pure delays in transfer functions but there a toolbox that should at least partly implement them (I did not tested it yet) with several overloadings:

> **[Scilab Module : iodelay toolbox](https://atoms.scilab.org/toolboxes/iodelay)**
>
> manipulation and frequency analysis of linear dynamical systems with input or output delays

A priori, this toolbox uses Padé approximants, but this has to be checked.

S.

---

<div class="post-metadata">

**Author:** ![scelles](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@scelles](https://scilab.discourse.group/u/scelles)\
**Post date:** [February 7, 2024, 11:42am UTC](https://scilab.discourse.group/t/model-a-first-order-system-with-input-delay-pade-approximant/388/3 "2024-02-07T11:42:28Z")

</div>

Thanks Stephane for this prompt answer

ATOMS doesn’t seems to provide a search function. That’s sad 😉  
(I mean inside Scilab… not [https://atoms.scilab.org/](https://atoms.scilab.org/) )

This code works

```auto
s = poly(0, 's');

// 1st order system
Gs = 1 // static gain
tau = 4 // time constant
H = syslin('c', Gs / (1 + tau * s))

// Delay
Tr = 1.5
// R = syslin('c', (1 - Tr / 2 * s) / (1 + Tr / 2 * s)) // approximation du retard pur par approximation de Padé à l'ordre 1
// R = syslin('c', (1 - Tr / 2 * s + Tr^2 * s^2 / 12) / (1 + Tr / 2 * s + Tr^2 * s^2 / 12)) // approximation du retard pur par approximation de Padé à l'ordre 2
R = syslin('c', (1 - Tr / 2 * s + Tr^2 * s^2 / 10 - Tr^3 * s^3 / 120) / (1 + Tr / 2 * s + Tr^2 * s^2 / 10 + Tr^3 * s^3 / 120)) // approximation du retard pur par approximation de Padé à l'ordre 3

T = H * R

// Heaviside step response
temps = linspace(0, 20, 51);
rep = csim('step', temps, T);
plot(temps, rep);

```

but not this one

```auto
s = poly(0, 's');

// 1st order system
Gs = 1 // static gain
tau = 4 // time constant
H = syslin('c', Gs / (1 + tau * s))

// Delay
Tr = 1.5

T = iodelay(H, Tr)

// Heaviside step response
temps = linspace(0, 20, 51);
rep = csim('step', temps, T);
plot(temps, rep);

```

Any idea what is wrong ?

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [February 7, 2024, 12:01pm UTC](https://scilab.discourse.group/t/model-a-first-order-system-with-input-delay-pade-approximant/388/4 "2024-02-07T12:01:36Z")

</div>

> [@scelles](#):
>
> Any idea what is wrong ?

Yes, this toolbox overloads simple algebraic operations and only some macros, namely

```auto
bode, black, nyquist

```

No state-space simulation is supported, that’s why `csim` does not work. In fact no rational approximation of delays is implemented.

S.

---

<div class="post-metadata">

**Author:** ![scelles](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@scelles](https://scilab.discourse.group/u/scelles)\
**Post date:** [March 5, 2024, 4:57pm UTC](https://scilab.discourse.group/t/model-a-first-order-system-with-input-delay-pade-approximant/388/5 "2024-03-05T16:57:23Z")

</div>

I noticed in a Julia library some code for pade approximant

> <https://github.com/JuliaControl/ControlSystems.jl/blob/0d825400f1fd0126a4ccaa63408ba2b38351d47a/lib/ControlSystemsBase/src/delay_systems.jl#L96-L154>

Maybe it could be worth to implement a pade function in Scilab.

Here is code

```scilab
function [coeffs]=pade_coeffs(n)
    coeffs = []
    for i = 0:n
        coeffs(i+1) = nchoosek(n, i) * prod(n+1:2*n-i)
    end
endfunction

function [coeffs_scaled]=_linscale_coeffs(coeffs, a)
    a_pow = 1
    coeffs_scaled(1) = coeffs(1)
    for k = 2:length(coeffs)
        a_pow = a * a_pow
        coeffs_scaled(k) = coeffs(k) * a_pow
    end
endfunction

function [T]=pade(tau, n)
    coeffs = pade_coeffs(n)
    coeffs_scaled = _linscale_coeffs(coeffs, tau)
    num = poly(_linscale_coeffs(coeffs, -tau), "s", "coeff")
    den = poly(_linscale_coeffs(coeffs, tau), "s", "coeff")    
    T = syslin("c", num, den)
endfunction

//p = poly(coeffs, "x", "coeff")

s = poly(0, 's')
Gs = 1 // static gain
tau = 4 // time constant
H = syslin('c', Gs / (1 + tau * s))

Td = 2.0 // delay
Hd = pade(Td, 10)

T = H * Hd

time = linspace(0, 20, 51);
rep = csim('step', time, T);
plot(time, rep);

```

![image](https://global.discourse-cdn.com/free1/uploads/utc/original/1X/b81559391190d9425baebd99b029cd90cbeff25e.png)

What about integrating it into Scilab?

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [March 6, 2024, 8:31am UTC](https://scilab.discourse.group/t/model-a-first-order-system-with-input-delay-pade-approximant/388/6 "2024-03-06T08:31:35Z")

</div>

> [@scelles](#):
>
> What about integrating it into Scilab?

Yes, e.g. in the cacsd module, but a help page is also needed (in the spirit of [Padé approximation of models with time delay - MATLAB pade - MathWorks France](https://fr.mathworks.com/help/control/ref/dynamicsystem.pade.html)). In the source of pade() you should control the type of input arguments by using the new mechanism (see [https://scilab.discourse.group/t/new-language-feature-arguments-introduced-in-scilab-2024-0-0](https://scilab.discourse.group/t/new-language-feature-arguments-introduced-in-scilab-2024-0-0)). When you have all these elements working in your fork you can create a merge request.

S.

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [March 6, 2024, 8:50am UTC](https://scilab.discourse.group/t/model-a-first-order-system-with-input-delay-pade-approximant/388/7 "2024-03-06T08:50:42Z")

</div>

Αnd if you think that iodelay toolbox is actually used, you could take into account the syntax

```auto
sysp = pade(sys,...)

```

when `sys` has delays.

S.

---

<div class="post-metadata">

**Author:** ![scelles](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@scelles](https://scilab.discourse.group/u/scelles)\
**Post date:** [March 6, 2024, 9:09am UTC](https://scilab.discourse.group/t/model-a-first-order-system-with-input-delay-pade-approximant/388/8 "2024-03-06T09:09:49Z")

</div>

> [@mottelet](#):
>
> Padé approximation of models with time delay - MATLAB pade - MathWorks France

Sorry but I can’t find repository of iodelay - see [ATOMS : iodelay toolbox details](https://atoms.scilab.org/toolboxes/iodelay#comment3574)

I looked at cacsd module [scilab/modules/cacsd · minor · scilab / scilab · GitLab](https://gitlab.com/scilab/scilab/-/tree/minor/scilab/modules/cacsd/)

There is so many things! not sure I can handle that.

I wonder if you @mottelet or other contributors can handle that ? Pinging @vincent.couvert @davidcl @adeline.carnis @cedric.delamarre @antoine.elias (seeing latest contributions in [code](https://gitlab.com/scilab/scilab))

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [March 6, 2024, 9:40am UTC](https://scilab.discourse.group/t/model-a-first-order-system-with-input-delay-pade-approximant/388/9 "2024-03-06T09:40:27Z")

</div>

> [@scelles](#):
>
> There is so many things! not sure I can handle that.

You are contributor to other open source projects (Python, Julia), I am sure you can do it 🙂

S.
