ControlSystem - lsim wrong usage

using ControlSystems, DataInterpolations, Plots


s = tf(“s”)
sys = Kiw0^2/(s^3+2Dw0s^2+w0^2s+Kiw0^2)
tsim = 0:0.005:50;
tval() = rand(range(0, stop=50, length=10000))
vec = [tval() for i in 1:10000]
ramp(x,t) = vec(t)
res = lsim(sys, ramp, tsim)

How to correct it?

It looks like you might have assigned a vector to lsim at one point? Try res = ControlSystems.lsim(sys, ramp, tsim)

You seem to create a variable called vec, and then calling vec on something. I would guess you are trying to call that vector, maybe just change that variable name?

1 Like

A few suggestions… First: (1) bracket code chunks between triple back-ticks. This will give a “computer code” syntax. If you follow the opening three back-ticks by “julia”, I assume this will give possible Julia specific syntax. (2) bracket in-line code by single back-tick, without keyword “julia”.

Your line:

tval() = rand(range(0, stop=50, length=10000))

[notice typesetting of code chunk as described in point (1) above]

This function draws a random element from the list of time values [0,…,50]. OK.

Next, you create vector vec (notice typesetting as described in point (2) above) by drawing 10_000 times from this function [syntax for doing this is comprehension]. So vec contains 10_000 time points (?) in the interval (0,50), in artibrary order.

  • Is this really what you want, or
  • should the elements of vec be sorted?

If you mean that vec should be time points when some ramping takes place, then it would seem more natural to sort the elements of vec??

Your next code is:

ramp(x,t) = vec(t)

This won’t work. vec is a vector, and not a function. The correct syntax to pick out elements from vec is, e.g., vec[1] to pick out the first element, etc.

[Perhaps you have seen some Julia code where you can choose between using vector syntax and function syntax, e.g., in solutions of differential equations, where you actually can write both sol[1] and sol(1.5). But that possibility is created in a very specific way, and is not allowed in general. The way to create this dual vector and function syntax is to create your own data structure where sol is a vector (or more generally: an array). Then, in that data structure, you can create a “functor” which is a function with the same name as the data structure. In DifferentialEquations, this functor sets up a data interpolation of the elements in the solution array.

But you have not created such a data structure, and you can therefore not use function syntax on your array.]

If you instead want to convert vec to a function, you would need to create a data interpolation function from vec.

Anyways, it would be simpler to answer your questions if you explain what you want vec to produce, and what ramp should produce.

1 Like

Thanks for the thorough and helpful answer @BLI !

I was also struggling to figure out exactly what the intention of the original code was, I came up with this code that runs, but whether or not it produces the intended result is not clear to me. Since the input function is called rand, I believe there is some call to cumsum missing on the randomly sampled vector, or some other operation that will cause the input to look like some form of jittered ramp.

using ControlSystems, Plots

w0 = 2 * pi
D = 0.01
Ki = 0.12

s = tf('s')
sys = (Ki*w0)^2 / (s^3 + 2D*w0*s^2 + w0^2*s + (Ki*w0)^2)
Ts = 0.005
tsim = 0:Ts:50;
tval() = rand(range(0, stop = 50, length = 10000))
tvec = [tval() for i = 1:length(tsim)]
ramp(x, t) = tvec[floor(Int, t*Ts + 1)]
res = lsim(c2d(sys, Ts), ramp, tsim)
1 Like