-
Notifications
You must be signed in to change notification settings - Fork 0
/
c2d_euler.m
95 lines (84 loc) · 2.76 KB
/
c2d_euler.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
%==========================================================================
%
% c2d_euler Transforms a continuous transfer function to a discrete
% transfer function using the forward and backward Euler methods.
%
% Hz = c2d_euler(Hs,T,type)
% Hz = c2d_euler(Hs,T,type,output,normalize)
%
% See also c2d.
%
% Copyright © 2021 Tamas Kis
% Last Update: 2023-09-17
% Website: https://tamaskis.github.io
% Contact: [email protected]
%
% TECHNICAL DOCUMENTATION:
% https://tamaskis.github.io/files/Continuous_to_Discrete_Transfer_Function_Transformation_Using_the_Euler_Methods.pdf
%
% DEPENDENCIES:
% • Control System Toolbox
% • Symbolic Math Toolbox
%
%--------------------------------------------------------------------------
%
% ------
% INPUT:
% ------
% Hs - (1×1 tf or zpk) continous transfer function
% T - (1×1 double) sampling period
% type - (char array) 'forward' or 'backward'
% output - (OPTIONAL) (char array) specifies output type ('tf' or
% 'zpk') (defaults to 'tf')
% normalize - (OPTIONAL) (1×1 logical) true if transfer function should
% be normalized, false otherwise (defaults to false)
%
% -------
% OUTPUT:
% -------
% Hz - (1×1 tf or zpk) discrete transfer function
%
%==========================================================================
function Hz = c2d_euler(Hs,T,type,output,normalize)
% defaults "output" to 'tf' if not input
if (nargin < 4) || isempty(output)
output = 'tf';
end
% defaults "normalize" to false if not input
if (nargin < 5) || isempty(normalize)
normalize = false;
end
% symbolic variable for z;
z = sym('z');
% specified Euler approximation of s
if strcmpi(type,'backward')
s = (z-1)/(T*z);
elseif strcmpi(type,'forward')
s = (z-1)/T;
else
error("'type' must be input as 'backward' or 'forward'")
end
% converts transfer function object to symbolic function object
[num,den] = tfdata(Hs);
Hz = poly2sym(cell2mat(num),z)/poly2sym(cell2mat(den),z);
% performs Euler transformation
Hz = simplify(subs(Hz,s));
% obtains numerator and denominator of symbolic expression in MATLAB's
% "polynomial form"
[sym_num,sym_den] = numden(Hz);
num = sym2poly(sym_num);
den = sym2poly(sym_den);
% normalizes coefficients w.r.t. coefficient on largest power of z in
% denominator
if normalize
num = num/den(1);
den = den/den(1);
end
% creates discrete transfer function model
Hz = tf(num,den,T);
% converts discrete transfer function model to discrete zero-pole-gain
% model if specified
if strcmpi(output,'zpk')
Hz = zpk(Hz);
end
end