Frontmatter

If you are publishing this notebook on the web, you can set the parameters below to provide HTML metadata. This is useful for search engines and social media.

using Plots, ControlSystemsBase, PlutoUI
4.9 s

Creating transfer functions

In MATLAB, access to the Control Systems Toolbox allows one to create transfer functions, make feedback and system interconnections as well as attempt some block diagram algebra.

The basic command to create a system model is the tf command which stands for transfer function. The signature of the tf command is as follows (click on the link to see documentation):

sys = tf(numerator, denominator)

In the above numerator and denominator are MATLAB vectors that stand for the polynomial coefficients of the numerator and denominator of the transfer function respectively. That is, in other words, a poynomial like $ax^2+bx + c$ is represented as MATLAB vector [a, b, c].

md"""### Creating transfer functions

In MATLAB, access to the Control Systems Toolbox allows one to create transfer functions, make feedback and system interconnections as well as attempt some block diagram algebra.


The basic command to create a system model is the [`tf`](https://www.mathworks.com/help/control/ref/tf.html) command which stands for transfer function. The signature of the `tf` command is as follows (click on the link to see documentation):

```matlab
sys = tf(numerator, denominator)
```

In the above numerator and denominator are MATLAB vectors that stand for the polynomial coefficients of the numerator and denominator of the transfer function respectively. That is, in other words, a poynomial like $ax^2+bx + c$ is represented as MATLAB vector `[a, b, c]`.

"""
263 μs

Exercise 1

Create a transfer function model in MATLAB of:

$$G(s) = \dfrac{s+5}{2s^2+3s+4}$$

md"""#### Exercise 1
Create a transfer function model in MATLAB of:

```math
G(s) = \dfrac{s+5}{2s^2+3s+4}
```

"""
153 μs
Click to reveal

This can be done as follows:

G = tf([1, 5], [2, 3, 4])

Foldable("Click to reveal",md"""
This can be done as follows:

```matlab
G = tf([1, 5], [2, 3, 4])
```
""")
1.4 ms

Questions:

  1. How do you enter a factored polynomial? E.g. $(s+1)^2$?

  2. True or false: The first element in the vector goes with the lowest degree term in the polynomial.

  3. How do you enter a zero coefficient ?

md""" **Questions:**
1. How do you enter a factored polynomial? E.g. $(s+1)^2$?
1. True or false: The first element in the vector goes with the lowest degree term in the polynomial.
1. How do you enter a zero coefficient ?
"""
286 μs
Click to reveal

  1. The polynomials have to be expanded out.

  2. False

  3. Any missing coefficients have to be explicitly zeroed out.

Foldable("Click to reveal", md"""
1. The polynomials have to be expanded out.
2. False
3. Any missing coefficients have to be explicitly zeroed out.

""")
3.4 ms

Block diagram algebra

MATLAB allows three basic types of system interconnections: feedback, parallel and series.

Click on the links to see the documentation.

Exercise 2

Connect $G_1$ and $G_2$ in series:

$$G_1(s) = \dfrac{s}{2 s^2-5 s-3} \qquad G_2(s) = \dfrac{s-3}{s+1}$$

Is that the simplest form of the transfer function possible?

Hint: Check minreal

md"""### Block diagram algebra

MATLAB allows three basic types of system interconnections: [`feedback`](https://www.mathworks.com/help/control/ref/inputoutputmodel.feedback.html), [`parallel`](https://www.mathworks.com/help/control/ref/inputoutputmodel.parallel.html) and [`series`](https://www.mathworks.com/help/control/ref/inputoutputmodel.series.html).

Click on the links to see the documentation.

#### Exercise 2

Connect $G_1$ and $G_2$ in series:
```math
G_1(s) = \dfrac{s}{2 s^2-5 s-3} \qquad G_2(s) = \dfrac{s-3}{s+1}
```

Is that the simplest form of the transfer function possible?

Hint: Check [`minreal`](https://www.mathworks.com/help/control/ref/dynamicsystem.minreal.html)
"""
655 μs
Click to reveal

This can be done as follows:

g1_num = [1, 0]
g1_den = [2, -5, -3]
g1 = tf(g1_num, g1_den )
g2_num = [1, -3]
g2_den = [1, 1]
g2 = tf(g2_num, g2_den)

sys_series = series(g1, g2)

Foldable("Click to reveal", md"""
This can be done as follows:

```matlab
g1_num = [1, 0]
g1_den = [2, -5, -3]
g1 = tf(g1_num, g1_den )
g2_num = [1, -3]
g2_den = [1, 1]
g2 = tf(g2_num, g2_den)

sys_series = series(g1, g2)
```

""")
135 μs

However, that is not the simplest form. We can obtaine the simplest form using

minreal(sys_series)
md"""However, that is not the simplest form. We can obtaine the simplest form using

```matlab
minreal(sys_series)
```
"""
107 μs

Exercise 3

Connect $G_1$ and $G_2$ in parallel and simplify the transfer function as much as possible.

md""" #### Exercise 3

Connect $G_1$ and $G_2$ in parallel and simplify the transfer function as much as possible.
"""
134 μs
Click to reveal

Assuming the started code is already entered as above, we simply add:

sys_parallel = minreal(parallel(g1, g2))

Foldable("Click to reveal", md"""
Assuming the started code is already entered as above, we simply add:

```matlab
sys_parallel = minreal(parallel(g1, g2))
```

""")
119 μs

Generating Bode plots (toolbox)

In MATLAB the transfer function model is useful for constructing Bode plots automatically. The function to use is called bode and takes the signature

bode(sys)

or

[mag,phase,wout] = bode(sys)

The difference is that the former automatically creates a Bode plot for us using the the output variables where as the latter only outputs the variables and plotting is left to us.

Exercise 4

Construct the Bode plot of the feedback interconnection $G_1$ above with $G_2$ in the feedback loop.

Question: How do you make a unity feedback interconnection?

md"""### Generating Bode plots (toolbox)

In MATLAB the transfer function model is useful for constructing Bode plots automatically. The function to use is called `bode` and takes the signature

```matlab
bode(sys)
```
or

```matlab
[mag,phase,wout] = bode(sys)
```

The difference is that the former automatically creates a Bode plot for us using the the output variables where as the latter only outputs the variables and plotting is left to us.

#### Exercise 4

Construct the Bode plot of the feedback interconnection $G_1$ above with $G_2$ in the feedback loop.

**Question: How do you make a unity feedback interconnection?**
"""
300 μs
Click to reveal

Again, assuming g1 and g2 have been already created as transfer function models, this can be done as follows:

bode(minreal(feedback(g1, g2)))

Foldable("Click to reveal", md"""Again, assuming `g1` and `g2` have been already created as transfer function models, this can be done as follows:

```matlab
bode(minreal(feedback(g1, g2)))
```

""")
153 μs

Evaluating transfer functions at frequencies

Let us solve Problem 2 of Homework 7 in MATLAB. Remember that the system diagram we need to reduce is:

md"""
#### Evaluating transfer functions at frequencies

Let us solve Problem 2 of Homework 7 in MATLAB. Remember that the system diagram we need to reduce is:

$(Resource("https://i.imgur.com/iZTzOKB.png"))
"""
71.1 ms

and the input to the system is: $x(t) = 5\cos(5t-\pi/6)$

Helpful functions might include: sym2poly, poly2sym and cell2mat.

md""" and the input to the system is: $x(t) = 5\cos(5t-\pi/6)$


Helpful functions might include: [`sym2poly`](https://www.mathworks.com/help/symbolic/sym.sym2poly.html), [`poly2sym`](https://www.mathworks.com/help/symbolic/sym.poly2sym.html) and [`cell2mat`](https://www.mathworks.com/help/matlab/ref/cell2mat.html).
"""
211 μs
Click to reveal answer - simple way

This method allows you to verify your answers in MATLAB when solving things analytically.

top = tf([1], [1, 0]);
forward = parallel(top, 5);
lower = tf([1, 0], [1]);

sys = feedback(forward, lower);

syms s;
ssym_num = poly2sym(cell2mat(sys.Numerator), s);
ssym_den = poly2sym(cell2mat(sys.Denominator), s);

sys_sym = ssym_num / ssym_den;
sys_at5 = subs(sys_sym, 's', 1j*5);

M = abs(sys_at5);
phi = angle(sys_at5);

Foldable("Click to reveal answer - simple way",md"""
This method allows you to verify your answers in MATLAB when solving things analytically.

```matlab
top = tf([1], [1, 0]);
forward = parallel(top, 5);
lower = tf([1, 0], [1]);

sys = feedback(forward, lower);

syms s;
ssym_num = poly2sym(cell2mat(sys.Numerator), s);
ssym_den = poly2sym(cell2mat(sys.Denominator), s);

sys_sym = ssym_num / ssym_den;
sys_at5 = subs(sys_sym, 's', 1j*5);

M = abs(sys_at5);
phi = angle(sys_at5);
```
""")
121 μs
Click to reveal answer - advanced way

In this method we actually are simulating the system with a real input. The system output gets stored in the variable out.

top = tf([1], [1, 0]);
forward = parallel(top, 5);
lower = tf([1, 0], [1]);

sys = feedback(forward, lower);
t = linspace(0, 2*pi, 100);
x = 5*cos(5*t-pi/6);
out = lsim(sys, x, t)

Foldable("Click to reveal answer - advanced way", md"""
In this method we actually are simulating the system with a real input. The system output gets stored in the variable `out`.

```matlab
top = tf([1], [1, 0]);
forward = parallel(top, 5);
lower = tf([1, 0], [1]);

sys = feedback(forward, lower);
t = linspace(0, 2*pi, 100);
x = 5*cos(5*t-pi/6);
out = lsim(sys, x, t)
```
""")
131 μs
begin
struct Foldable{C}
title::String
content::C
end
function Base.show(io, mime::MIME"text/html", fld::Foldable)
write(io,"<details><summary>$(fld.title)</summary><p>")
show(io, mime, fld.content)
write(io,"</p></details>")
end
end
1.7 ms