# ControlSystem - lsim wrong usage

**URL:** https://discourse.julialang.org/t/controlsystem-lsim-wrong-usage/91207
**Category:** New to Julia
**Tags:** package, controlsystems
**Created:** [December 4, 2022, 4:37am UTC](https://discourse.julialang.org/t/controlsystem-lsim-wrong-usage/91207 "2022-12-04T04:37:46Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![GottDengDirk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gottdengdirk/32/43952_2.png) [@GottDengDirk](https://discourse.julialang.org/u/GottDengDirk)
#### Post date: [December 4, 2022, 4:37am UTC](https://discourse.julialang.org/t/controlsystem-lsim-wrong-usage/91207/1 "2022-12-04T04:37:46Z")

</div>

Hallo，  
using ControlSystems, DataInterpolations, Plots

w0=2\*pi  
D=0.01  
Ki=0.12

s = tf(“s”)  
sys = Ki_w0^2/(s^3+2_D_w0_s^2+w0^2_s+Ki_w0^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?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/6/06b14c2dbf6bf50bdab9610e4be4576bb3ef9b73.png)

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [December 4, 2022, 4:47am UTC](https://discourse.julialang.org/t/controlsystem-lsim-wrong-usage/91207/2 "2022-12-04T04:47:35Z")

</div>

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

---

<div class="post-metadata">

### Author: ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)
#### Post date: [December 4, 2022, 6:46am UTC](https://discourse.julialang.org/t/controlsystem-lsim-wrong-usage/91207/3 "2022-12-04T06:46:10Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [December 4, 2022, 9:51am UTC](https://discourse.julialang.org/t/controlsystem-lsim-wrong-usage/91207/4 "2022-12-04T09:51:34Z")

</div>

> [@GottDengDirk](#):
>
> tval() = rand(range(0, stop=50, length=10000))

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:

```julia
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:

```julia
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.

---

<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: [December 4, 2022, 10:00am UTC](https://discourse.julialang.org/t/controlsystem-lsim-wrong-usage/91207/5 "2022-12-04T10:00:02Z")

</div>

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.

```julia
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)
plot(res)

```
