Implementation of matlab function in julia (Root locus plot)

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

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(..).

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

[r,k] = fun(x)

rather, use

r,k = fun(x)

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

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/
for a list of noteworthy difference between ControlSystems and other similar tools.

Here’s the complete code for your example

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

6 Likes

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)

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

image

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

The latest version is v1.5.2

1 Like

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

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

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

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

Double check your package status:

(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:

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:

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

Screenshot from 2022-12-15 22-52-13

2 Likes

OK, this code works as expected:

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

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

cd control
julia --project

Created a bug report for the warning: rlocusplot shows a very long warning with Plots · Issue #785 · JuliaControl/ControlSystems.jl · GitHub

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.