From: Richardson, A. <ar...@ev...> - 2005-07-14 15:32:27
|
I've written two simple m-files (to_p.m and to_r.m) that convert=20 complex numbers to/from standard form from/to the polar form that it is often used in electrical engineering. Please add them to the forge. Usage notes and examples are included as comments in the code. Thanks Tony Richardson ## Copyright (C) 2005 Anthony M. Richardson ## ## This program is free software; you can redistribute it and/or ## modify it under the terms of the GNU General Public License ## as published by the Free Software Foundation; either version 2 ## of the License, or (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, but ## WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ## Author: Tony Richardson <ric...@ev...> ## Created: July 2005 function [R, Theta] =3D to_p(A,opt) % [R, Theta] =3D to_p(A, opt) - Convert from rectangular to polar form. % With single return argument: % If opt =3D=3D 1 (default) % Returns magnitudes and phases in alternate columns % If opt =3D=3D 2 % Returns magnitudes % If opt =3D=3D 3 % Returns phase angles % With two return arguments: % Returns magnitudes and phase angle (in degrees) matrices. % (opt is ignored) % % Notes: % 1) to_p() displays complex numbers in the polar form that is often % used in electrical engineering. Results from to_p() are typically % not used for additional computation. % 2) to_r() is the corresponding inverse function. % % Examples: % > % Convert a complex matrix to polar form % > R =3D to_p([i 3+4*i; 0 10; 2+2*i 5]) % R =3D % % 1.00000 90.00000 5.00000 53.13010 % 0.00000 0.00000 10.00000 0.00000 % 2.82843 45.00000 5.00000 0.00000 % % > % Generate random voltage and impedance matrices % > V =3D 10*rand(3,1)+i*10*rand(3,1); % > Z =3D 1000*rand(3,3)+i*1000*rand(3,3); % > I =3D Z\V % I =3D % % 0.0069266 + 0.0067151i % 0.0053507 - 0.0158189i % 0.0095161 + 0.0070036i % % > % Display currents in polar form % > to_p(I) % ans =3D % % 9.6473e-03 4.4112e+01 % 1.6699e-02 -7.1312e+01 % 1.1816e-02 3.6352e+01 % % > % In the last example, we find I1 =3D 9.65 mA /_ 44.1 degrees, I2 = =3D % > % 16.7 mA /_ -71.3 degress and I3 =3D 11.8 mA /_ 36.4 degrees. if (nargout <=3D 1)=20 % Only a single return argument. if nargin =3D=3D 1=20 % Return the mag and phase in adjacent columns. nc =3D 2*size(A,2); R =3D zeros(size(A,1),nc); R(:,1:2:nc) =3D abs(A); Theta =3D arg(A)*180./pi; R(:,2:2:nc) =3D Theta; else if opt =3D=3D 1=20 % Return the mag and phase in adjacent columns. nc =3D 2*size(A,2); R =3D zeros(size(A,1),nc); R(:,1:2:nc) =3D abs(A); Theta =3D arg(A)*180./pi; R(:,2:2:nc) =3D Theta; elseif opt =3D=3D 2=20 % Return only the mag R =3D abs(A); elseif opt =3D=3D 3=20 % Return only the phase (in degrees) R =3D arg(A)*180./pi; else error("invalid second argument"); end end elseif nargout =3D=3D 2=20 % With two return arguments, the mag and phase are in stored in % separate matrices R =3D abs(A); Theta =3D arg(A)*180./pi; else error("invalid number of return arguments"); end endfunction ## Copyright (C) 2005 Anthony M. Richardson ## ## This program is free software; you can redistribute it and/or ## modify it under the terms of the GNU General Public License ## as published by the Free Software Foundation; either version 2 ## of the License, or (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, but ## WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ## Author: Tony Richardson <ric...@ev...> ## Created: July 2005 function [A] =3D to_r(R,Theta) % A =3D to_r(R,Theta) - Convert from polar to rectangular form. % With single argument R: % R must have an even number of columns. Alternate columns contain the % magnitudes and phase angles (in degrees). % With two arguments R and Theta: % R is a matrix containing the magnitudes % Theta is a matrix containing the phase angles (in degrees). % % Notes: % 1) to_r() converts complex numbers from the polar form that is % common in electrical engineering to standard rectangular form. % When performing calculations, all complex matrices should be % in standard (rectangular) form. There are not octave routines % that work directly with "polar form" complex matrices. % 2) to_p() is the corresponding inverse function. % % Examples: % > % Assume we have a four impedances equal to 100/_ 45, 50+50*i, 200, % > % 80 /_ 90. We can easily create a corresponding impedance matrix % > % in standard form as: % > Z =3D [to_r(100, 45) 50+50*i; 200 to_r(80, 90)] % Z =3D % % 70.71068 + 70.71068i 50.00000 + 50.00000i % 200.00000 + 0.00000i 0.00000 + 80.00000i % % > % Or, if we have two voltages equal to (polar form) 200 /_45, and 100. % > % We can create the corresponding complex vector as: % > V =3D to_r([200 45; 100 0]) % V =3D % % 141.42 + 141.42i % 100.00 + 0.00i % % > % The corresponding currents can be determined using: % > I =3D Z\V % I =3D % % 0.86364 - 0.64282i % 1.60706 + 0.90909i % % > % The currents can be displayed in polar form with to_p(): % > to_p(I) % ans =3D % % 1.0766 -36.6612 % 1.8464 29.4962 if nargin > 2=20 error("incorrect number of arguments."); end if nargin =3D=3D 1=20 % If there is a single argument, the mag and phase are % in adjacent columns. nc =3D size(R,2); if (nc - fix(nc/2)*2) =3D=3D 1=20 error("single argument must have have an even number of columns"); end A =3D R(:,1:2:nc).*exp(i*pi*R(:,2:2:nc)/180.); elseif nargin =3D=3D 2=20 % With two arguments, the mag and phase are in separate matrices if ~all(size(R) =3D=3D size(Theta))=20 error("arguments must be of the same dimension."); end A =3D R.*exp(i*pi*Theta/180.); end endfunction |