Error using ControlSystems' lsim

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:

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:

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!

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

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/
and discretization here
https://juliacontrol.github.io/ControlSystems.jl/dev/lib/constructors/#ControlSystems.c2d

2 Likes