Skip to content

Commit c85c50f

Browse files
authored
Merge pull request #2 from tamaskis/normalization
added normalization + better input checking, updated examples
2 parents b7c89ce + 173da42 commit c85c50f

File tree

5 files changed

+38
-17
lines changed

5 files changed

+38
-17
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
.DS_Store
2-
local/
1+
.DS_Store
Binary file not shown.

EXAMPLES.mlx

1.08 KB
Binary file not shown.

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@
33
Transforms a continuous transfer function to a discrete transfer function using the forward and backward Euler methods.
44

55

6-
76
## Syntax
87

9-
`Hz = c2d_euler(Hs,T,'forward')` \
10-
`Hz = c2d_euler(Hs,T,'backward')`
8+
`Hz = c2d_euler(Hs,T,type)` \
9+
`Hz = c2d_euler(Hs,T,type,output,normalize)`
10+
1111

12+
## Inputs
1213

13-
## Description
14+
- `Hs` (1×1 `'tf'` or `'zpk'`): continuous transfer function
15+
- `T` (1×1 double): sampling period
16+
- `type` (`char` array): `'forward'` or `'backward`
17+
- `output` (OPTIONAL) (`char` array): specifies output type (`'tf'` or `'zpk'`) (defaults to `'tf'`)
18+
- `normalize` (OPTIONAL) (1×1 `logical`) `true` if transfer function should be normalized, `false` otherwise (defaults to `false`)
1419

15-
`Hz = c2d_euler(Hs,T,'forward')` returns the discrete transfer function `Hz` obtained by applying the forward Euler (i.e. forward difference) transformation to a continuous transfer function `Hs`, where `T` is the sampling period.
20+
## Outputs
1621

17-
`Hz = c2d_euler(Hs,T,'backward')` returns the discrete transfer function `Hz` obtained by applying the backward Euler (i.e. backward difference) transformation to a continuous transfer function `Hs`, where `T` is the sampling period.
22+
- `Hz` (1×1 `tf` or `zpk`): discrete transfer function
1823

1924

2025
## Examples and Additional Documentation

c2d_euler.m

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
% c2d_euler Transforms a continuous transfer function to a discrete
44
% transfer function using the forward and backward Euler methods.
55
%
6-
% Hz = c2d_euler(Hs,T,'forward')
7-
% Hz = c2d_euler(Hs,T,'backward')
6+
% Hz = c2d_euler(Hs,T,type)
7+
% Hz = c2d_euler(Hs,T,type,output,normalize)
88
%
99
% See also c2d.
1010
%
1111
% Copyright © 2021 Tamas Kis
12-
% Last Update: 2022-09-26
12+
% Last Update: 2023-09-17
1313
% Website: https://tamaskis.github.io
1414
% Contact: tamas.a.kis@outlook.com
1515
%
@@ -25,32 +25,42 @@
2525
% ------
2626
% INPUT:
2727
% ------
28-
% Hs - (1×1 tf or zpk) continous transfer function
29-
% T - (1×1 double) sampling period
30-
% type - (char) 'forward' or 'backward'
31-
% output - (OPTIONAL) (char) specifies output type ('tf' or 'zpk')
28+
% Hs - (1×1 tf or zpk) continous transfer function
29+
% T - (1×1 double) sampling period
30+
% type - (char array) 'forward' or 'backward'
31+
% output - (OPTIONAL) (char array) specifies output type ('tf' or
32+
% 'zpk') (defaults to 'tf')
33+
% normalize - (OPTIONAL) (1×1 logical) true if transfer function should
34+
% be normalized, false otherwise (defaults to false)
3235
%
3336
% -------
3437
% OUTPUT:
3538
% -------
3639
% Hz - (1×1 tf or zpk) discrete transfer function
3740
%
3841
%==========================================================================
39-
function Hz = c2d_euler(Hs,T,type,output)
42+
function Hz = c2d_euler(Hs,T,type,output,normalize)
4043

4144
% defaults "output" to 'tf' if not input
4245
if (nargin < 4) || isempty(output)
4346
output = 'tf';
4447
end
4548

49+
% defaults "normalize" to false if not input
50+
if (nargin < 5) || isempty(normalize)
51+
normalize = false;
52+
end
53+
4654
% symbolic variable for z;
4755
z = sym('z');
4856

4957
% specified Euler approximation of s
5058
if strcmpi(type,'backward')
5159
s = (z-1)/(T*z);
52-
else
60+
elseif strcmpi(type,'forward')
5361
s = (z-1)/T;
62+
else
63+
error("'type' must be input as 'backward' or 'forward'")
5464
end
5565

5666
% converts transfer function object to symbolic function object
@@ -66,6 +76,13 @@
6676
num = sym2poly(sym_num);
6777
den = sym2poly(sym_den);
6878

79+
% normalizes coefficients w.r.t. coefficient on largest power of z in
80+
% denominator
81+
if normalize
82+
num = num/den(1);
83+
den = den/den(1);
84+
end
85+
6986
% creates discrete transfer function model
7087
Hz = tf(num,den,T);
7188

0 commit comments

Comments
 (0)