# Error using ControlSystems' lsim

**URL:** https://discourse.julialang.org/t/error-using-controlsystems-lsim/57898
**Category:** General Usage
**Tags:** question, control
**Created:** [March 24, 2021, 9:50pm UTC](https://discourse.julialang.org/t/error-using-controlsystems-lsim/57898 "2021-03-24T21:50:48Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![cjdakin](https://avatars.discourse-cdn.com/v4/letter/c/3ab097/32.png) [@cjdakin](https://discourse.julialang.org/u/cjdakin)
#### Post date: [March 24, 2021, 9:50pm UTC](https://discourse.julialang.org/t/error-using-controlsystems-lsim/57898/1 "2021-03-24T21:50:48Z")

</div>

Newbie to the ControlSystems toolbox trying to simulate the output of a control signal being passed through a transfer function but am having trouble solving an error that is arising. Is it due to a coding error or the toolbox’s ability to represent the transfer function as written? The code is:

```julia
using ControlSystems, SymPy
@var s
n = 2000
Δt = 1/100
t = 0:Δt:N*Δt - Δt
u = rand(N)
G = 0.25 * tf(60 * ((s^0.06)*(1 + 0.014*s)^2.2), [0.03, 1.0], Δt);
y = lsim(G,u,t)

```

The following error results:

```julia
ERROR: LoadError: PyError ($(Expr(:escape, :(ccall(#= C:\Users\DataCruncher\.julia\packages\PyCall\tqyST\src\pyoperators.jl:12 =# @pysym(:PyNumber_Add), PyPtr, (PyPtr, PyPtr), a, b))))) <class 'TypeError'>
TypeError("unsupported operand type(s) for +: 'NotImplementedType' and 'NotImplementedType'")

Stacktrace:
 [1] pyerr_check at C:\Users\DataCruncher\.julia\packages\PyCall\tqyST\src\exception.jl:62 [inlined]
 [2] pyerr_check at C:\Users\DataCruncher\.julia\packages\PyCall\tqyST\src\exception.jl:66 [inlined]
 [3] _handle_error(::String) at C:\Users\DataCruncher\.julia\packages\PyCall\tqyST\src\exception.jl:83
 [4] macro expansion at C:\Users\DataCruncher\.julia\packages\PyCall\tqyST\src\exception.jl:97 [inlined]
 [5] +(::PyCall.PyObject, ::PyCall.PyObject) at C:\Users\DataCruncher\.julia\packages\PyCall\tqyST\src\pyoperators.jl:11
 [6] generic_matvecmul!(::Array{Sym,1}, ::Char, ::Array{Sym,2}, ::SubArray{Sym,1,Array{Sym,2},Tuple{Base.Slice{Base.OneTo{Int64}},Int64},true}, ::LinearAlgebra.MulAddMul{true,true,Bool,Bool}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\LinearAlgebra\src\matmul.jl:681
 [7] mul! at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\LinearAlgebra\src\matmul.jl:81 [inlined]
 [8] mul! at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\LinearAlgebra\src\matmul.jl:208 [inlined]
 [9] ltitr(::Array{Sym,2}, ::Array{Sym,2}, ::Array{Float64,1}, ::Array{Bool,1}) at C:\Users\DataCruncher\.julia\packages\ControlSystems\D98Nv\src\timeresp.jl:241      
 [10] lsim(::StateSpace{Discrete{Float64},Sym}, ::Array{Float64,1}, ::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}; x0::Array{Bool,1}, method::Symbol) at C:\Users\DataCruncher\.julia\packages\ControlSystems\D98Nv\src\timeresp.jl:149
 [11] lsim(::StateSpace{Discrete{Float64},Sym}, ::Array{Float64,1}, ::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}) at C:\Users\DataCruncher\.julia\packages\ControlSystems\D98Nv\src\timeresp.jl:114
 [12] #lsim#178 at C:\Users\DataCruncher\.julia\packages\ControlSystems\D98Nv\src\timeresp.jl:205 [inlined]
 [13] lsim(::TransferFunction{Discrete{Float64},ControlSystems.SisoRational{Sym}}, ::Array{Float64,1}, ::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}) at C:\Users\DataCruncher\.julia\packages\ControlSystems\D98Nv\src\timeresp.jl:205
 [14] top-level scope at f:\UBC\Sensorimotor Correlation\Julia\Transfer_function_Error_Submission.jl:9
in expression starting at f:\UBC\Sensorimotor Correlation\Julia\Transfer_function_Error_Submission.jl:9

```

Thanks for your help!

---

<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: [March 25, 2021, 4:59am UTC](https://discourse.julialang.org/t/error-using-controlsystems-lsim/57898/2 "2021-03-25T04:59:05Z")

</div>

Hello and welcome to the community!

The problem here is that you are mixing two different ways of creating transfer functions.  
The `tf` constructor takes two vectors of coefficients for the numerator and denominator polynomials. You provide a symbilc expression as the numerator, but a vector of coefficients for the denominator. Furthermore, you use the symbolic variable from SymPy. The Laplace `s` variable in a transfer function form is created by

```julia
s = tf("s") # create Laplace s transfer function

```

Another problem is that you are trying to multiply a continuous-time model (in the Laplace `s` variable), with a discrete-time transfer function (where you supply `Δt`). A continuous-time models must be explicitly discretized using the function `c2d`.

You find the relevant documentation on creating systems here  
[https://juliacontrol.github.io/ControlSystems.jl/dev/man/creating\_systems/](https://juliacontrol.github.io/ControlSystems.jl/dev/man/creating_systems/)  
and discretization here  
[https://juliacontrol.github.io/ControlSystems.jl/dev/lib/constructors/#ControlSystems.c2d](https://juliacontrol.github.io/ControlSystems.jl/dev/lib/constructors/#ControlSystems.c2d)
