polar_value.m
上传用户:zjhyt3
上传日期:2007-07-03
资源大小:89k
文件大小:1k
源码类别:

matlab例程

开发平台:

Matlab

  1. function [mag, angle] = polar_value(x,y)
  2. %POLAR_VALUE Converts (x,y) to (r,theta)
  3. % Function POLAR_VALUE converts an input (x,y) 
  4. % value into (r,theta), with theta in degrees.
  5. % It illustrates the use of optional arguments.
  6.  
  7. % Define variables:
  8. %   angle    -- Angle in degrees
  9. %   msg      -- Error message
  10. %   mag      -- Magnitude
  11. %   x        -- Input x value
  12. %   y        -- Input y value (optional)
  13. %  Record of revisions:
  14. %      Date       Programmer          Description of change
  15. %      ====       ==========          =====================
  16. %    12/16/98    S. J. Chapman        Original code
  17. % Check for a legal number of input arguments.
  18. msg = nargchk(1,2,nargin);
  19. error(msg);
  20. % If the y argument is missing, set it to 0.
  21. if nargin < 2
  22.    y = 0;
  23. end
  24. % Check for (0,0) input arguments, and print out
  25. % a warning message.
  26. if x == 0 & y == 0
  27.    msg = 'Both x any y are zero: angle is meaningless!';
  28.    warning(msg);
  29. end
  30. % Now calculate the magnitude.
  31. mag = sqrt(x.^2 + y.^2);
  32. % If the second output argument is present, calculate 
  33. % angle in degrees.
  34. if nargout == 2
  35.    angle = atan2(y,x) * 180/pi;
  36. end