Home > matGeom > geom2d > ellipsePerimeter.m

ellipsePerimeter

PURPOSE ^

ELLIPSEPERIMETER Perimeter of an ellipse.

SYNOPSIS ^

function perim = ellipsePerimeter(ellipse, varargin)

DESCRIPTION ^

ELLIPSEPERIMETER Perimeter of an ellipse.

   P = ellipsePerimeter(ELLI)
   Computes the perimeter of an ellipse, using numerical integration.
   ELLI is an ellipse, given using one of the following formats:
   * a 1-by-5 row vector containing coordinates of center, length of
       semi-axes, and orientation in degrees
   * a 1-by-2 row vector containing only the lengths of the semi-axes.
   The result

   P = ellipsePerimeter(ELLI, TOL)
   Specify the relative tolerance for numerical integration.


   Example
   P = ellipsePerimeter([30 40 30 10 15])
   P = 
       133.6489 

   See also
     ellipses2d, drawEllipse

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function perim = ellipsePerimeter(ellipse, varargin)
0002 %ELLIPSEPERIMETER Perimeter of an ellipse.
0003 %
0004 %   P = ellipsePerimeter(ELLI)
0005 %   Computes the perimeter of an ellipse, using numerical integration.
0006 %   ELLI is an ellipse, given using one of the following formats:
0007 %   * a 1-by-5 row vector containing coordinates of center, length of
0008 %       semi-axes, and orientation in degrees
0009 %   * a 1-by-2 row vector containing only the lengths of the semi-axes.
0010 %   The result
0011 %
0012 %   P = ellipsePerimeter(ELLI, TOL)
0013 %   Specify the relative tolerance for numerical integration.
0014 %
0015 %
0016 %   Example
0017 %   P = ellipsePerimeter([30 40 30 10 15])
0018 %   P =
0019 %       133.6489
0020 %
0021 %   See also
0022 %     ellipses2d, drawEllipse
0023 %
0024 %
0025 
0026 % ------
0027 % Author: David Legland
0028 % e-mail: david.legland@grignon.inra.fr
0029 % Created: 2012-02-20,    using Matlab 7.9.0.529 (R2009b)
0030 % Copyright 2012 INRA - Cepia Software Platform.
0031 
0032 %% Parse input argument
0033 
0034 if size(ellipse, 2) == 5
0035     ra = ellipse(:, 3);
0036     rb = ellipse(:, 4);
0037     
0038 elseif size(ellipse, 2) == 2
0039     ra = ellipse(:, 1);
0040     rb = ellipse(:, 2);
0041     
0042 elseif size(ellipse, 2) == 1
0043     ra = ellipse;
0044     rb = varargin{1};
0045     varargin(1) = [];
0046     
0047 end
0048 
0049 % relative tolerance
0050 tol = 1e-10;
0051 if ~isempty(varargin)
0052     tol = varargin{1};
0053 end
0054 
0055 
0056 %% Numerical integration
0057 
0058 n = length(ra);
0059 
0060 perim = zeros(n, 1);
0061 
0062 for i = 1:n
0063     % function to integrate
0064     f = @(t) sqrt(ra(i) .^ 2 .* cos(t) .^ 2 + rb(i) .^ 2 .* sin(t) .^ 2) ;
0065 
0066     % absolute tolerance from relative tolerance
0067     eps = tol * max(ra(i), rb(i));
0068     
0069     % integrate on first quadrant
0070     if verLessThan('matlab', '7.14')
0071         perim(i) = 4 * quad(f, 0, pi/2, eps); %#ok<DQUAD>
0072     else
0073         perim(i) = 4 * integral(f, 0, pi/2, 'AbsTol', eps);
0074     end
0075 end
0076

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019