# Using ControlSystems; c2d usage

**URL:** <https://discourse.julialang.org/t/using-controlsystems-c2d-usage/50110>\
**Category:** General Usage\
**Tags:** question, control\
**Created:** [November 13, 2020, 7:01pm UTC](https://discourse.julialang.org/t/using-controlsystems-c2d-usage/50110 "2020-11-13T19:01:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![panditabupesh](https://avatars.discourse-cdn.com/v4/letter/p/ecc23a/32.png) [@panditabupesh](https://discourse.julialang.org/u/panditabupesh)\
**Post date:** [November 13, 2020, 7:01pm UTC](https://discourse.julialang.org/t/using-controlsystems-c2d-usage/50110/1 "2020-11-13T19:01:38Z")

</div>

I am new to Julia. I am trying to use ControlSystems.

```julia
using ControlSystems
sys = tf(1,[1,1])
sys_d = c2d(sys,1)

```

How can I pass zoh or foh as input to c2d?  
Thanks,  
Bupesh

c2d(sys,1,:zoh)  
ERROR: MethodError: no method matching c2d(::TransferFunction{Continuous,ControlSystems.SisoRational{Int64}}, ::Int64, ::Symbol)  
Closest candidates are:  
c2d(::StateSpace, ::Real, ::Symbol) at C:\Users\bupandit.julia\packages\ControlSystems\H3Br2\src\discrete.jl:12  
c2d(::TransferFunction, ::Any; kwargs…) at C:\Users\bupandit.julia\packages\ControlSystems\H3Br2\src\discrete.jl:213  
c2d(::DelayLtiSystem, ::Real, ::Any) at C:\Users\bupandit.julia\packages\ControlSystems\H3Br2\src\delay\_systems.jl:58  
…  
Stacktrace:  
[1] top-level scope at REPL[73]:1

---

<div class="post-metadata">

**Author:** ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)\
**Post date:** [November 13, 2020, 8:28pm UTC](https://discourse.julialang.org/t/using-controlsystems-c2d-usage/50110/2 "2020-11-13T20:28:49Z")

</div>

I was able to reproduce this error, and I believe it is a bug (although I am not an expert). As a workaround, it seems like you can do this:  
`sys_d = c2d(ss(sys), 1) # transfer function to state space, then to discrete`

Looking at the [source](https://github.com/JuliaControl/ControlSystems.jl/blob/af81aee13e43fadf727a7c691b7e1ff8c072ee91/src/discrete.jl#L213), it looks like the method for transfer functions erroneously assumes that `method` is a keyword argument:

> ```
> sys = ss(G)
> sysd = c2d(sys, h, kwargs...)[1]
> 
> ```

whereas the signature for state-space treats it as an optional (not a keyword) argument.

Would you be willing to file an issue? Also, your example would be good to build in as a test.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [November 13, 2020, 8:36pm UTC](https://discourse.julialang.org/t/using-controlsystems-c2d-usage/50110/3 "2020-11-13T20:36:51Z")

</div>

Zero order hold is the default so you do not need to supply that. Other discretization methods have not made it into Controlsystems.jl yet, but I have some on my working branch  
[https://github.com/baggepinnen/ControlSystems.jl/blob/ee19974fb7161fc84c92a0c984dfa18965a6a82e/src/discrete.jl#L12](https://github.com/baggepinnen/ControlSystems.jl/blob/ee19974fb7161fc84c92a0c984dfa18965a6a82e/src/discrete.jl#L12)  
that will appear eventually.

---

<div class="post-metadata">

**Author:** ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)\
**Post date:** [November 13, 2020, 8:49pm UTC](https://discourse.julialang.org/t/using-controlsystems-c2d-usage/50110/4 "2020-11-13T20:49:37Z")

</div>

The docs say  
`[sysd, x0map] = c2d(sys, Ts, method=:zoh)`  
with no semi-colon before `method`. It is true that the method can be left out for default `:zoh`. But if you want `:foh` you have to include it as an optional argument, and it doesn’t work with transfer functions:

```julia
julia> c2d(sys, 1, :foh)
ERROR: MethodError: no method matching c2d(::TransferFunction{Continuous,ControlSystems.SisoRational{Int64}}, ::Int64, ::Symbol)
Closest candidates are:
  c2d(::StateSpace, ::Real, ::Symbol) at /Users/artkuo/.julia/packages/ControlSystems/H3Br2/src/discrete.jl:12
  c2d(::TransferFunction, ::Any; kwargs...) at /Users/artkuo/.julia/packages/ControlSystems/H3Br2/src/discrete.jl:213
  c2d(::DelayLtiSystem, ::Real, ::Any) at /Users/artkuo/.julia/packages/ControlSystems/H3Br2/src/delay_systems.jl:58

```

The same does work with state-space.  
EDIT: Forgot to say [docs for v0.5.3](https://juliacontrol.github.io/ControlSystems.jl/stable/lib/constructors/#ControlSystems.c2d)

---

<div class="post-metadata">

**Author:** ![panditabupesh](https://avatars.discourse-cdn.com/v4/letter/p/ecc23a/32.png) [@panditabupesh](https://discourse.julialang.org/u/panditabupesh)\
**Post date:** [November 13, 2020, 8:50pm UTC](https://discourse.julialang.org/t/using-controlsystems-c2d-usage/50110/5 "2020-11-13T20:50:04Z")

</div>

It seems both zoh and foh methods are implemented in the code. But, why does it throw errors if I am writing:

c2d(sys,1,:zoh)

---

<div class="post-metadata">

**Author:** ![mfalt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mfalt/32/12467_2.png) [@mfalt](https://discourse.julialang.org/u/mfalt)\
**Post date:** [November 13, 2020, 10:04pm UTC](https://discourse.julialang.org/t/using-controlsystems-c2d-usage/50110/6 "2020-11-13T22:04:05Z")

</div>

You are right, there is a bug in the code. The function that catches transferfunction inputs belives that the method is submitted as a kwarg, but the statespace version assumes an optional argument.  
[https://github.com/JuliaControl/ControlSystems.jl/blob/af81aee13e43fadf727a7c691b7e1ff8c072ee91/src/discrete.jl#L213](https://github.com/JuliaControl/ControlSystems.jl/blob/af81aee13e43fadf727a7c691b7e1ff8c072ee91/src/discrete.jl#L213)  
~~I will submitt a PR to solve this within the hour.~~ Fixed in PR [https://github.com/JuliaControl/ControlSystems.jl/pull/392](https://github.com/JuliaControl/ControlSystems.jl/pull/392)  
which we can hopefully pull soon.
