# Implementation of matlab function in julia (Root locus plot)

**URL:** https://discourse.julialang.org/t/implementation-of-matlab-function-in-julia-root-locus-plot/91662
**Category:** Modelling & Simulations
**Tags:** function, controlsystems
**Created:** [December 15, 2022, 12:36am UTC](https://discourse.julialang.org/t/implementation-of-matlab-function-in-julia-root-locus-plot/91662 "2022-12-15T00:36:00Z")
**Posts on this page:** 11
**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 15, 2022, 12:36am UTC](https://discourse.julialang.org/t/implementation-of-matlab-function-in-julia-root-locus-plot/91662/1 "2022-12-15T00:36:00Z")

</div>

As the title, I can’t reproduce the functionality of the rlocus function in Matlab.  
i use ControlSystems.jl  
1.  
sys = tf([3 1],[9 7 5 6]);  
[r,k] = rlocus(sys)  
ERROR: syntax: invalid assignment location “[r, k]”  
2.  
sys = tf([0.5 -1],[4 0 3 0 2]);  
k = (1:0.5:5);  
r = rlocus(sys,k);  
gain K in Julia dosen’t work  
3.  
rlocus(sys1,‘b’,sys2,‘k’,sys3,‘r’)  
Only images of sys1 can be displayed

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [December 15, 2022, 6:29am UTC](https://discourse.julialang.org/t/implementation-of-matlab-function-in-julia-root-locus-plot/91662/2 "2022-12-15T06:29:52Z")

</div>

You will probably have to provide more detail to get help. What is `rlocus`? Are you using any packages? What is the schlechtes output?

Your issue in 1. is just a Julia syntax issue, you can’t have `[r, k]` on the left hand side of an assignment operation. If `rlocus` returns two things (or more generally something iterable) you can write `r, k = rlocus(..)`.

---

<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 15, 2022, 1:03pm UTC](https://discourse.julialang.org/t/implementation-of-matlab-function-in-julia-root-locus-plot/91662/3 "2022-12-15T13:03:09Z")

</div>

You are using syntax that is not proper Julia syntax, you’re not allowed to write like this

```julia
[r,k] = fun(x)

```

rather, use

```julia
r,k = fun(x)

```

Also, you are not allowed to pass matrices into `tf`, use vectors like this

```julia
sys = tf([3, 1],[9, 7, 5, 6]);

```

In general, you seem to assume that you can write matlab code in Julia, this is not the case and you will have to consult the documentation of ControlSystems to learn how to use it. In particular, see  
[https://juliacontrol.github.io/ControlSystems.jl/stable/man/differences/](https://juliacontrol.github.io/ControlSystems.jl/stable/man/differences/)  
for a list of noteworthy difference between ControlSystems and other similar tools.

Here’s the complete code for your example

```julia
using ControlSystems
sys = tf([3, 1], [9, 7, 5, 6])
k = 1:0.5:5
r, Z, K = rlocus(sys, K=k)

using Plots
rlocusplot(sys, c=:blue) # colors are specified with the c or color keyword
rlocusplot!(sys, c=:black) # add to an existing plot by appending ! to the plot function name

```

![rl](https://global.discourse-cdn.com/julialang/original/3X/4/6/46318e26aa41f584af57673287f459baaf26bdf7.png)

---

<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 15, 2022, 7:34pm UTC](https://discourse.julialang.org/t/implementation-of-matlab-function-in-julia-root-locus-plot/91662/4 "2022-12-15T19:34:10Z")

</div>

thanks at first.  
I have consulted the documentation of ControlSystems before and made many attempts. But the description for the rlocus function seems to be missing. The only introduction I can find is  
“Computes and plots the root locus of the SISO LTISystem P with a negative feedback loop and feedback gains K, if K is not provided, range(1e-6,stop=500,length=10000) is used. If OrdinaryDiffEq.jl is installed and loaded by the user (using OrdinaryDiffEq), rlocusplot will use an adaptive step-size algorithm to select values of K. A scalar Kmax can then be given as second argument.” For different situations, some attempts have been made, But not satisfied.  
I still have a problem with your first code:  
r, Z, K = rlocus(sys, K=k)

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/f/ef2a6a74d1cda4b2e593d79409aa35d86f235050.png)

---

<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 15, 2022, 8:05pm UTC](https://discourse.julialang.org/t/implementation-of-matlab-function-in-julia-root-locus-plot/91662/5 "2022-12-15T20:05:46Z")

</div>

What version of Controlsystems.jl are you using, the `rlocus` function is rather new, before we used to have the `rlocusplot` only.

---

<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 15, 2022, 8:54pm UTC](https://discourse.julialang.org/t/implementation-of-matlab-function-in-julia-root-locus-plot/91662/6 "2022-12-15T20:54:46Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/1/3/13003ed9735152aa5fa5b31dd7b41b06e8e6af90.png)

---

<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 15, 2022, 9:04pm UTC](https://discourse.julialang.org/t/implementation-of-matlab-function-in-julia-root-locus-plot/91662/7 "2022-12-15T21:04:42Z")

</div>

That is a very old version, I recommend updating to the latest version 😊

The latest version is v1.5.2

> **[Release v1.5.2 · JuliaControl/ControlSystems.jl](https://github.com/JuliaControl/ControlSystems.jl/releases/tag/v1.5.2)**
>
> ControlSystems v1.5.2
> Diff since v1.5.1
> Closed issues:
> 
> Lsim docstring update (#758)
> rlocus method (#764)
> lsim, error message for single input system (#767)
> lsim, error when input is a matrix (#769...

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [December 15, 2022, 9:47pm UTC](https://discourse.julialang.org/t/implementation-of-matlab-function-in-julia-root-locus-plot/91662/8 "2022-12-15T21:47:58Z")

</div>

Use a new project to avoid that other packages are holding you back:  
From the command line:

```bash
mkdir control
cd control
julia --project="."

```

In Julia first enter the package manager by pressing the ] key and then enter:

```julia
(control) pkg> add ControlSystems
(control) pkg> add Plots

```

Double check your package status:

```julia
(control) pkg> st
Status `~/repos/control/Project.toml`
  [a6e380b2] ControlSystems v1.5.2
  [91a5bcdd] Plots v1.37.2

```

Press the key above the ENTER key to go back to Julia.

Now you can execute the example from above:

```julia
using ControlSystems
sys = tf([3, 1], [9, 7, 5, 6])
k = 1:0.5:5
r, Z, K = rlocus(sys, K=k)

using Plots
rlocusplot(sys, c=:blue) # colors are specified with the c or color keyword
rlocusplot!(sys, c=:black)

```

And I do not get the same result as @baggepinnen , I get the warning:

```julia
 Warning: Keyword argument hover not supported with Plots.GRBackend().

```

![Screenshot from 2022-12-15 22-52-13](https://global.discourse-cdn.com/julialang/original/3X/8/0/80b1a9b34e86e5d8e4d7c4a2cf1f7615f76a0bb4.png)

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [December 15, 2022, 10:18pm UTC](https://discourse.julialang.org/t/implementation-of-matlab-function-in-julia-root-locus-plot/91662/9 "2022-12-15T22:18:07Z")

</div>

OK, this code works as expected:

```julia
using ControlSystems
sys1 = tf([3, 1], [9, 7, 5, 6])
k = 1:0.5:5
r1, Z1, K1 = rlocus(sys1, K=k)

sys2 = tf([0.5, -1], [4, 0, 3, 0, 2])
r2, Z2, K2 = rlocus(sys2, K=k)

using Plots
rlocusplot(sys1, c=:blue) # colors are specified with the c or color keyword
rlocusplot!(sys2, c=:black)

```

![Screenshot from 2022-12-15 23-17-48](https://global.discourse-cdn.com/julialang/original/3X/6/2/628fa0d802e309754528db108d82f0f9a8a178d1.png)

And if you have your project with ControlSystems and Plots already, start Julia with:

```julia
cd control
julia --project

```

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [December 15, 2022, 10:32pm UTC](https://discourse.julialang.org/t/implementation-of-matlab-function-in-julia-root-locus-plot/91662/10 "2022-12-15T22:32:49Z")

</div>

Created a bug report for the warning: [rlocusplot shows a very long warning with Plots · Issue #785 · JuliaControl/ControlSystems.jl · GitHub](https://github.com/JuliaControl/ControlSystems.jl/issues/785)

---

<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 16, 2022, 4:50am UTC](https://discourse.julialang.org/t/implementation-of-matlab-function-in-julia-root-locus-plot/91662/11 "2022-12-16T04:50:57Z")

</div>

The warning is due to the plot recipe having hover information, but you’re plotting the GR, a backend that does not support hover information. If you use plotly, you can hover the mouse above the plot to see the gain value at a particular point on the curve.
