Here we will show all the important transfer function operations in Matlab. So, it will be very easy for you to calculate different kinds of control system problems with the transfer function operations in Matlab.

Table of Contents ;

## Step Response of a Transfer Function

In the control system design, if the transfer function in Matlab is known, some of the test inputs can be applied to see both the transient response and steady-state response of the system. Step input is one of these inputs that is applied to the transfer function.

Matlab provides a very useful command called as ‘step()’ command. You can use the ‘step()’ command to test a function of a control system. Here we show how to use the ‘step()’ command to do it.

For example, we want to see the response of the transfer function in Matlab;

TF = 25/(s^2 + 4s + 25)

To do it, you just need to type the code like this in the Matlab command window;

```
>> num = [0 0 25]; denom = [1 4 25];
step(num,denom)
>>
```

In this code, we just created two vectors that include the coefficients of the numerator and denominator of them which are ‘num’ and ‘denom’ vectors respectively. The output is;

The ‘step()’ command automatically creates a plot that shows the response of the system to the step function. If you right-click on the graph, you can look at the peak response, settling time, rise time, and steady-state response of the system, from the ‘Characteristics’ menu.

You can also plot the response of a system to step input, by defining a time range. Check the code below.

```
>> num = [0 0 25]; denom = [1 4 25];
t=0:0.1:10;
step(num,denom,t)
>>
```

### So…

This is the same one in which there is a slight difference. We defined a time vector ‘t’ and we added this inside the parentheses of the ‘step()’ command. It will give the response graph between this time interval like this;

As you see above, if you do not assign any variables to the ‘step()’ command, it will show the graph automatically. But if you assign variables, you can see the response of the system as the list in Command Window, corresponding to the assigned time interval. Check the code below again;

```
>> num = [0 0 25]; denom = [1 4 25];
t=0:0.1:10;
[a,b]=step(num,denom,t)
a =
0
0.1077
0.3599
0.6582
0.9271
1.1221
1.2281
1.2532
1.2189
1.1517
1.0761
1.0100
0.9637
0.9400
0.9362
0.9465
0.9643
0.9833
0.9995
1.0104
1.0156
1.0160
1.0130
1.0084
1.0036
0.9996
0.9971
0.9959
0.9960
0.9969
0.9981
0.9993
1.0002
1.0008
1.0010
1.0010
1.0008
1.0004
1.0001
0.9999
0.9998
0.9997
0.9998
0.9998
0.9999
1.0000
1.0000
1.0001
1.0001
1.0001
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
b =
[]
>>
```

## Conversion of Transfer Functions to State-Space Form in Matlab

This conversion can be made with the ‘tf2ss()’ command in Matlab. If a transfer function is obtained in a polynomial state, this command creates the state-space model of that transfer function in Matlab of a control system. Here we explain how to obtain state-space models of them in Matlab with the ‘tf2ss()’ command, with a very basic coding example. You can try this code example in your Matlab software.

```
>> nom = 10;
denom = [1 4 3];
[x, y, z, t] = tf2ss(nom,denom)
x =
-4 -3
1 0
y =
1
0
z =
0 10
t =
0
>>
```

The use of the ‘tf2ss()’ command is very simple in Matlab. First of all, you need to create the proper vectors that include the coefficients of the nominator and denominator of the input transfer function in Matlab. In this example, these vectors are ‘nom’ and ‘denom’. Then you need to write these vectors inside the parentheses of the ‘tf2ss()’ command.

After that, you need to assign the ‘tf2ss()’ command to four result variables. These variables are ‘x’, ‘y’, ‘z’, and ‘t’ in this example. Execute the code and see the result in the Matlab command window.

According to this example, the input transfer function in Matlab is;

TF = 10/(s^2 + 4s +3)

The output state-state model of this one is;

[X1;X2] = [-4 -3; 1 0]*[x1(t) x2(t)] + [1;0]*u(t);

y(t) = [0 10]*[x1(t); x2(t)];

If you compare the results with the output space-state model, you will understand which result variable shows what.

## Partial Fractional Representation of Transfer Functions

Matlab provides very useful commands for the designer of control systems. One of these commands is the ‘residue()’ command. With the ‘residue()’ command, you can convert it that is given as polynomials to partial fractional representation. Here we show how to use the ‘residue()’ command in Matlab with a very basic coding example. You can try this code in your Matlab software.

The use of the ‘residue()’ command is very simple. Check the example below.

```
>> nom = [2 5 3 6];
denom = [1 6 11 6];
[x, y, z] = residue(nom,denom)
x =
-6.0000
-4.0000
3.0000
y =
-3.0000
-2.0000
-1.0000
z =
2
>>
```

First of all, you need to create two vectors that include the coefficients of the polynomials of the nominator and the denominator of the transfer function in Matlab. In the example above, these vectors are ‘nom’ and ‘denom’. Then you need to type these vectors inside the parentheses of the ‘residue()’ command.

Then you need to assign the ‘residue()’ command to three result variables which are ‘x’, ‘y’, and ‘z’ above. If you execute the code in the Matlab command window, you will see the result.

The elements of the ‘x’ column vector represent the nominators of partial fractions.

The elements of the ‘y’ vector represent the roots of the denominators.

The ‘z’ is the constant value.

In this example, the input transfer function in Matlab is;

TF = (2s^3 + 5s^2 +3s + 6)/(s^3 + 6s^2 + 11s +6)

The output partial fraction form of it is;

TF = 6/(s+3) + 4/(s+2) + 3/(s+1) +2

You can compare the coefficients of the output with the ‘x’, ‘y’, and ‘z’ vector elements.

## Finding Transfer Function of a System From State-Space Form

You can find the transfer function in Matlab of a system from the state-space form of a system with the ‘ss2tf()’ command in Matlab.

Here, we explain how to use the ‘ss2tf()’ command in Matlab with a very basic example. You can try the example below in your Matlab software.

Consider a situation in that we need to calculate of the state-space model below;

[x1; x2] = [0 1; -25 0-4]*[x1(t); x2(t)] + [1 1; 0 1]*[u1(t);u2(t)];

[y1(t);y2(t)] = [1 0; 0 1][x1(t), x2(t)] + [0 0; 0 0]*[u1(t);u2(t)];

As you understand above, there are two inputs and two outputs of the system. This means that there are 4 transfer functions of this system. The problem can be solved with the code below in the Matlab command window;

```
>> x = [0 1;-25 4];
y = [1 1; 0 1];
z = [1 0; 0 1];
t = [0 0 ;0 0];
[nom1, denum2] = ss2tf(x, y, z, t,1)
[nom2, denum2] = ss2tf(x, y, z, t,2)
nom1 =
0 1 -4
%first output first TF nominator
0 0 -25
%first output second TF nominator
denum2 =
1.0000 -4.0000 25.0000
%common denumerator of the TFs of first output
nom2 =
0 1.0000 -3.0000
%second output first TF nominator
0 1.0000 -25.0000
%second output first TF nominator
denum2 =
1.0000 -4.0000 25.0000
%common denumerator of the TFs of second output
>>
```

First of all, we need to define all the matrices inside the state-space model like above. All of these matrices are defined with ‘x’, ‘y’, ‘z’, and ‘t’ variables respectively.

To find all 4 transfer functions, we used the ‘ss2tf()’ code two times. The first one gives the transfer functions in Matlab of the first input. You can see the numerators and denominators of all of them in the first input.

### So…

The same thing is valid for the second output. Four transfer functions are;

TF1 = (s+4)/(s^2 + 4s + 25);

TF2 = 25/(s^2 +4s +25);

TF3 = (s+5)/(s^2 + 4s +25);

TF4 = 25/(s^2 + 4s + 25);

## Conversion Zero-Pole Gain Transfer Function to Polynomial

With this command, you can easily convert the transfer function in Matlab which is given in the zero-pole gain form, into polynomial form.

Also, there is another command that you can do the reverse of this thing in Matlab.

Here we explain the use of the ‘zp2tf()’ command in Matlab with a very basic code example. You can try this code in your Matlab product.

You know that the transfer functions in Matlab are very important aspects of control system design. These functions can be in different forms and the designer can need them in other forms. If a function is given in the zero-pole gain form, you can convert it to a polynomial form with the ‘zp2tf()’ command. Check the coding example below.

```
>> x = [-6.7016; -0.2984];
y = [-4;-3;-2];
z = [1];
[nom,denom]=zp2tf(x,y,z)
nom =
0 1.0000 7.0000 1.9998
denom =
1 9 26 24
>>
```

As you see above, the use of the ‘zp2tf()’ command is very simple in Matlab. First of all, we need to create the required column vectors that include the roots of the nominator and denominator of the zero-pole gain transfer function in Matlab. In this example, these vectors are ‘x’ and ‘y’. And we need to type these vectors inside the parentheses of the ‘zp2tf()’ command.

We need to equate the ‘zp2tf()’ command to two result variables. These variables are ‘nom’ and ‘denom’ here. Take care of the syntax to work the code properly.

If you look at the result;

The given zero-pole gain transfer function in Matlab is like this;

TF = (s+6.7016)(s+0.298)/(s+2)(s+3)(s+4)

And the converted one which is in polynomial form;

TF = (s^2+7s+2)/(s^3+9s^2+26s+24)

So, the variable ‘nom’ gives the nominator of the polynomial form of the transfer function in Matlab and ‘denom’ gives the denominator of the polynomial form.

## Conversion of a Transfer Function to Zero-Pole Gain Form

You know that in control system design, transfer functions in Matlab are very important. Some mathematical manipulations are required to see them in their intended forms. One of these forms is the zero-pole gain form. The ‘tf2zp()’ command does this thing in Matlab. Take a look at the example below.

```
>> nom = [1 7 2]; denom = [1 9 26 24];
[x,y,z]=tf2zp(nom, denom)
x =
-6.7016
-0.2984
y =
-4.0000
-3.0000
-2.0000
z =
1
>>
```

The use of the ‘tf2zp()’ command is very simple in Matlab. First of all, you need to create two vectors that include the coefficients of the nominator polynomial of the function. In this example, our nominator coefficients are the ‘nom’ vector. Do the same thing for the denominator polynomial of the function. In this example again, it is ‘denom’.

Then type the names of these functions inside the parentheses of the ‘tr2zp()’ command.

You need to equate the ‘tr2zp()’ command to three result variables, which are ‘x’, ‘y’ and ‘z’ here.

In this example the input transfer function in Matlab is;

TF = (s^2+7s+2)/(s^3+9s^2+26s+24)

And the zero-pole gain form of this function is;

TF = (s+6.7016)(s+0.298)/(s+2)(s+3)(s+4)

As you understand that values of ‘x’ gives the roots of the nominator of the zero-pole gain function. And ‘y’ gives the roots of the denominator of the zero-pole transfer function in Matlab.

## Applying Different Inputs to Transfer Functions

In simple, every control system has a specific function. Sometimes, engineers must test the control system by applying different inputs to see the output or response of the system. The standard inputs are ‘unit’, ‘ramp’, and ‘step’. You can also apply different kinds of inputs.

Here we show you how to test a function with different inputs with the ‘lsim()’ command. You can try the code below in your Matlab software.

For example, we want to test a transfer function in Matlab like below with a square wave.

TF = (s+6.7016)(s+0.298)/[(s+2)(s+3)(s+4)];

Take a look at the code below;

```
>> a = [-6.7016; -0.2984];
b = [-4;-3;-2];
c = [1];
sys = zpk(a,b,c)
[x, y] = gensig('square', 5, 30, 0.01);
lsim(sys,x,y)
sys =
(s+6.702) (s+0.2984)
--------------------
(s+4) (s+3) (s+2)
Continuous-time zero/pole/gain model.
>>
```

In this code, we created two column vectors ‘a’ and ‘b’ that includes the roots of the numerator and denominator of the function. To define the transfer function of the control system inside the ‘lsim()’ command, we combined these vectors with the ‘zpk()’ command. And we attached this ‘zpk()’ command to a variable called ‘sys’.

With the ‘gensig()’ command, we created the required test signal to be applied to the input of the transfer function in Matlab.

Finally, in the ‘lsim()’ command, we used the ‘sys’ and the variables of the ‘gensig()’ command. The output response of the transfer function in Matlab to that signal will be given as Amplitude/time chart like below.

## Finding State-Space Model of a Zero-Pole Gain Transfer Function

Consider a situation that we want to find the state-space model of the zero-pole gain function of;

TF = 10/((s+3)(s+1))

The code is;

```
>> x = [];
y = [-3;-1];
z = [10];
[a,b,c,d] = zp2ss(x,y,z)
a =
-4.0000 -1.7321
1.7321 0
b =
1
0
c =
0 5.7735
d =
0
>>
```

As you see above, you need to create the required column vectors to define the input variable of the ‘zp2ss()’ command. In this example, ‘x’ and ‘y’ column vectors include the roots of the function. ‘z’ is the nominator of the transfer function in Matlab which is ’10’. You must define the function like this.

Then you need to type these variables that define the zero-pole gain form of the transfer function, inside the parentheses of the ‘zp2ss()’ command respectively.

And you need to assign four result variables which are ‘a’, ‘b’, ‘c’, and ‘d’. here. According to these results, the state-space form of the example above is like this;

[x1; x2] = [-4 -1.7321; 1,7321 0]*[x1(t); x2(t)] + [0; 1]*u(t);

y(t) = [0 5.7735][x1(t), x2(t)]

You can extract your solution from your ‘zp2ss()’ command with the assistance of this example.

## Finding Zero-Pole Gain Form of Transfer Functions From State-Space Model

For example, you have a state-space model of a transfer function in Matlab like this;

[x1; x2] = [0 1; -3 -4]*[x1(t); x2(t)] + [0; 1]*u(t);

y(t) = [10 0][x1(t), x2(t)] + 0*u(t);

You need to properly define this state-space form to find the zero-pole gain form of the function.

```
>> x = [0 1; -3 -4];
y = [0;1];
z = [10 0];
t = 0;
[a, b, c] = ss2zp(x,y,z,t)
a =
0×1 empty double column vector
b =
-1
-3
c =
10
>>
```

As you see in the example above, we defined all the vectors and matrices in the state-space model respectively. ‘x’, ‘y’, ‘z’, and ‘t’ are these matrices and vectors respectively. We typed these vectors and matrices inside the parentheses of the ‘ss2zp()’ command respectively.

And we assigned the ‘ss2zp()’ command to three result variables which are ‘a, ‘b’, and ‘c’.

As you see in the result at the command window, ‘c’ is the numerator of the zero-pole gain form transfer function and ‘b’ includes the roots of the denominator’s roots. The transfer function in Matlab is like this;

TF = 10/((s+1)(s+3));

## Conclusion on Transfer Functions in Matlab

As you see above, different kinds of transfer function calculations are available in Matlab. So, solving the control system problems will be very easy with Matlab with these different kinds of tools.

These are the general points that we state about the Matlab transfer functions. Finally, do not forget to leave your comments and questions below about the Matlab transfer functions.

Your precious feedbacks are very important to us!

## Leave a Reply