{"id":2799,"date":"2011-01-17T11:36:39","date_gmt":"2011-01-17T10:36:39","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=2799"},"modified":"2011-01-17T11:36:39","modified_gmt":"2011-01-17T10:36:39","slug":"zero-pole-gain-models-in-mathematica-8","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=2799","title":{"rendered":"Zero-Pole-Gain models in Mathematica 8"},"content":{"rendered":"<p>MATLAB&#8217;s control system toolbox has functions for converting <a href=\"http:\/\/en.wikipedia.org\/wiki\/Transfer_function\">transfer functions<\/a> to either <a href=\"http:\/\/en.wikipedia.org\/wiki\/State_space_%28controls%29\">state space representations<\/a> or zero-pole-gain models as follows<\/p>\n<pre>%Create a simple transfer function in MATLAB\r\nnum=[2 3];\r\nden = [1 4 0 5];\r\nmytf=tf(num,den);\r\n\r\n%convert this to State Space form\r\ntf2ss(num,den)\r\nans =\r\n\r\n0   1   0\r\n0   0   1\r\n-5  -0  -4\r\n\r\n%Convert to zero-pole-gain form\r\n[z,p,k]=tf2zp(num,den)\r\nz = -1.5000\r\np =\r\n\r\n-4.27375 + 0.00000i\r\n0.13687 + 1.07294i\r\n0.13687 - 1.07294i\r\n\r\nk =  2<\/pre>\n<p>Now that Mathematica 8 is available I wondered what the Mathematica equivalent to the above would look like.  The conversion to a StateSpace model is easy:<\/p>\n<pre>mytf=TransferFunctionModel[(2 s + 3)\/(s^3 + 4 s^2 + 5), s]\r\nStateSpaceModel[mytf]<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.walkingrandomly.com\/images\/mathematica8\/tf.png\" alt=\"Transfer Function\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.walkingrandomly.com\/images\/mathematica8\/state_space.png\" alt=\"State Space\" \/><\/p>\n<p>but I couldn&#8217;t find an equivalent to MATLAB&#8217;s <a href=\"http:\/\/www.mathworks.com\/help\/toolbox\/signal\/tf2zp.html\">tf2zp<\/a> function.<\/p>\n<p>I can get the poles and zeros easily enough (as an aside it&#8217;s nice that Mathematica can do this exactly)<\/p>\n<pre>TransferFunctionPoles[mytf]\r\nTransferFunctionZeros[mytf]<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.walkingrandomly.com\/images\/mathematica8\/poles_zeros.png\" alt=\"State Space\" \/><br \/>\nbut what about gain?\u00a0 There is no TransferFunctionGain[] function as you might expect. There is a TransferFunctionFactor[] function but that doesn&#8217;t do what I want either.<\/p>\n<p>It turns out that there is a function that will do what I want but it is hidden from the user a little in an internal function<\/p>\n<pre>Control`ZeroPoleGainModel[mytf]\r\n\r\nControl`ZeroPoleGainModel[{{{{-(3\/2)}}}, {1\/\r\n     3 (-4 - 16 (2\/(263 - 3 Sqrt[5865]))^(\r\n        1\/3) - (1\/2 (263 - 3 Sqrt[5865]))^(1\/3)), -(4\/3) +\r\n     8\/3 (1 + I Sqrt[3]) (2\/(263 - 3 Sqrt[5865]))^(1\/3) +\r\n     1\/6 (1 - I Sqrt[3]) (1\/2 (263 - 3 Sqrt[5865]))^(1\/3), -(4\/3) +\r\n     8\/3 (1 - I Sqrt[3]) (2\/(263 - 3 Sqrt[5865]))^(1\/3) +\r\n     1\/6 (1 + I Sqrt[3]) (1\/2 (263 - 3 Sqrt[5865]))^(1\/3)}, {{2}}}, s]<\/pre>\n<p>That&#8217;s not as user friendly as it could be though.  How about this?<\/p>\n<pre>tf2zpk[x_] := Module[{z, p, k,zpkmodel},\r\n  zpkmodel = Control`ZeroPoleGainModel[x];\r\n  z = zpkmodel[[1, 1, 1, 1]];\r\n  p = zpkmodel[[1, 2]];\r\n  k = zpkmodel[[1, 3, 1]];\r\n  {z, p, k}\r\n  ]<\/pre>\n<p>Now we can get very similar behavior to MATLAB:<\/p>\n<pre>In[10]:= tf2zpk[mytf] \/\/ N\r\n\r\nOut[10]= {{-1.5}, {-4.27375, 0.136874 + 1.07294 I,\r\n  0.136874 - 1.07294 I}, {2.}}<\/pre>\n<p>Hope this helps someone out there.\u00a0 Thanks to my contact at Wolfram who told me about the Control`ZeroPoleGainModel function.<\/p>\n<p><strong>Other posts you may be interested in<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=2933\">Mathematica 8 &#8211; Control Theory Sneak Peek<\/a><\/li>\n<li><a href=\"https:\/\/www.walkingrandomly.com\/?p=2985\">5 new plot commands in Mathematica 8<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>MATLAB&#8217;s control system toolbox has functions for converting transfer functions to either state space representations or zero-pole-gain models as follows %Create a simple transfer function in MATLAB num=[2 3]; den = [1 4 0 5]; mytf=tf(num,den); %convert this to State Space form tf2ss(num,den) ans = 0 1 0 0 0 1 -5 -0 -4 %Convert [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[8,11],"tags":[],"class_list":["post-2799","post","type-post","status-publish","format-standard","hentry","category-mathematica","category-matlab"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-J9","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2799"}],"version-history":[{"count":22,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2799\/revisions"}],"predecessor-version":[{"id":3167,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/2799\/revisions\/3167"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}