1+ % c2d_euler Transforms a continuous transfer function to a discrete
2+ % transfer function using the forward and backward Euler methods.
3+ %
4+ % Hz = c2d_euler(Hs,T,'forward') returns the discrete transfer function
5+ % "Hz" obtained by applying the forward Euler (i.e. forward difference)
6+ % transformation to a continuous transfer function "Hs", where "T" is the
7+ % sampling period.
8+ %
9+ % Hz = c2d_euler(Hs,T,'backward') returns the discrete transfer function
10+ % "Hz" obtained by applying the backward Euler (i.e. backward difference)
11+ % transformation to a continuous transfer function "Hs", where "T" is the
12+ % sampling period.
13+ %
14+ % See also c2d
15+ %
16+ % MATLAB Central File Exchange:
17+ % GitHub: https://github.com/tamaskis/c2d_euler-MATLAB
18+ %
19+ % See "DOCUMENTATION.pdf" for additional documentation and examples.
20+ % Examples can also be found in EXAMPLES.m. Both of these files are
21+ % included with the download.
22+ %
23+ % Copyright (c) 2021 Tamas Kis
24+ % Last Update: 2021-04-10
25+
26+
27+
28+ %% FUNCTION
29+
30+ % INPUT: Hs - continuous transfer function
31+ % T - sampling period
32+ % type - 'forward' or 'backward'
33+ % OUTPUT: Hz - discrete transfer function
34+ function Hz = c2d_euler(Hs ,T ,type )
35+
36+ % symbolic variable for z;
37+ z = sym(' z' );
38+
39+ % specified Euler approximation of s
40+ if strcmp(type ,' backward' )
41+ s = (z - 1 )/(T * z );
42+ else
43+ s = (z - 1 )/T ;
44+ end
45+
46+ % converts transfer function object to symbolic function object
47+ [num ,den ] = tfdata(Hs );
48+ Hz = poly2sym(cell2mat(num ),z )/poly2sym(cell2mat(den ),z );
49+
50+ % performs Euler transformation
51+ Hz = simplify(subs(Hz ,s ));
52+
53+ % obtains numerator and denominator of symbolic expression in MATLAB's
54+ % "polynomial form"
55+ [sym_num ,sym_den ] = numden(Hz );
56+ num = sym2poly(sym_num );
57+ den = sym2poly(sym_den );
58+
59+ % creates discrete transfer function object
60+ Hz = tf(num ,den ,T );
61+
62+ end
0 commit comments