View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

4.9 | 49 ratings Rate this file 131 Downloads (last 30 days) File Size: 111 KB File ID: #4705 Version: 1.0
image thumbnail



AJ Johnson (view profile)


01 Apr 2004 (Updated )

Plot an error ellipse depicting confidence interval given a covariance matrix.

| Watch this File

File Information

% ERROR_ELLIPSE - plot an error ellipse, or ellipsoid, defining
% confidence region
% ERROR_ELLIPSE(C22) - Given a 2x2 covariance matrix, plot the
% associated error ellipse, at the origin. It returns a graphics handle
% of the ellipse that was drawn.
% ERROR_ELLIPSE(C33) - Given a 3x3 covariance matrix, plot the
% associated error ellipsoid, at the origin, as well as its projections
% onto the three axes. Returns a vector of 4 graphics handles, for the
% three ellipses (in the X-Y, Y-Z, and Z-X planes, respectively) and for
% the ellipsoid.
% ERROR_ELLIPSE(C,MU) - Plot the ellipse, or ellipsoid, centered at % MU, a vector whose length should match that of C (which is 2x2
% or 3x3).
% ERROR_ELLIPSE(...,'Property1',Value1,'Name2',Value2,...) sets
% the values of specified properties, including:
% 'C' - Alternate method of specifying the covariance matrix
% 'mu' - Alternate method of specifying the ellipse (-oid) center
% 'conf' - A value betwen 0 and 1 specifying the confidence interval.
% the default is 0.5 which is the 50% error ellipse.
% 'scale' - Allow the plot the be scaled to difference units.
% 'style' - A plotting style used to format ellipses.
% 'clip' - specifies a clipping radius. Portions of the ellipse, -oid,
% outside the radius will not be shown.
% NOTES: C must be positive definite for this function to work
% properly.


This file inspired Gaussian Mixture Probability Hypothesis Density Filter (Gm Phd).

MATLAB release MATLAB 6.1 (R12.1)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (64)
22 Sep 2016 M.B

M.B (view profile)

Nice piece of code.

03 May 2016 Aurican

18 Apr 2016 Ke Jing

Thank you, good for learning

Comment only
03 Dec 2015 Alceu Costa

Alceu Costa (view profile)

23 Jul 2015 Ahmed Fasih

Thanks AJ! Delicious BSD license :)

23 Jul 2015 Ahmed Fasih

What's the license on this submission, anyone know?

Comment only
04 Jun 2014 Jonathan Currie

Should the second argument to qchisq not be the degrees of freedom, rather than the number of parameters?

Comment only
11 Dec 2013 Peter Farkas

This was excellent for learning about Kalman filters.

14 Aug 2013 Serena

Serena (view profile)

Thank you,it was useful!
I have a question about the k value since I'm not familiar with statistics: why do you get it from a k square distribution and not from a normal distribution?

Comment only
11 Jun 2013 Christian

08 Feb 2013 Charles Nelatury

02 May 2012 Burcu

Burcu (view profile)

Really very helpful. But did not understand how to calculate k value from conf "k = sqrt(qchisq(conf,r))" Does anybody know how this relation holds? Thanks..

24 Apr 2012 Mohammad Ehsanul

Thanks. Saved a lot of my time.

Comment only
26 Oct 2011 Takuma

Takuma (view profile)

29 Sep 2011 Anthony

Very helpful and easy to use.

09 Apr 2011 gunes bay1r

thanks for conf. interval comment phil fox

21 Mar 2011 Claus

Claus (view profile)

Ooops, there's an obvious mistake in my area/volume calculation from above. I forgot to factor in the quantile for the desired percentile, i.e., you have to multiply the area/volume with the quantile of the desired confidence interval (conf). You can use the embedded functions from error_ellipse to calculate Pconf (for the default of conf=0.5, Pconf(2D)=1.1774/Pconf(3D)=1.5382). The formular for area (volume accordingly) should read:

Comment only
18 Mar 2011 Claus

Claus (view profile)

@Jenn Risk & @me: Finally got a solution for area/volume. It's all based on the eigenvalues of the CovMatrix (the prerequisite for error_ellipse).
Area (2D): area=prod(sqrt(eig(C)))*pi
Volume (3D): volume=prod(sqrt(eig(C)))*4/3*pi

Hope that helps. Cheers!

Comment only
17 Mar 2011 Claus

Claus (view profile)

Worked just great for me and saved a LOT of time writing my own code. Thanks!

I'm currently working on a area/volume calculation. @Ahmed Fasih: det(covMatrix) may definately be used as a measure, however, it's not the actual area/volume.

27 Dec 2010 Zohar Bar-Yehuda

01 Sep 2010 Ahmed Fasih

Jenn Risk: just use det(CovMatrix) for area/volume.

M ChC, Volkan Ozturk:
figure; handle(1)=error_ellipse(A); hold on; handle(2)=error_ellipse(B); handle(3)=error_ellipse(C); set(handle(2), 'color','r', 'linestyle',':'); set(handle(3), 'color','k', 'linestyle','--');

19 Aug 2010 Marcel

Marcel (view profile)

Perfect piece of code! I am using it alot. Had to change the API though to make it a bit more flexible in terms of plot properties (e.g. passing it a parent axes handle).

22 Jun 2010 Ipsita

Ipsita (view profile)

??? function h = error_ellipse(varargin)
Error: Function definitions are not permitted at the prompt or in scripts.

I get the above error, can anyone help ? I am using MATLAB 7.6.0(2008a). Thanks

Comment only
19 Apr 2010 Jenn Risk

Very helpful optically - but can it calculate the area of the error ellipse? I couldn't easily get it to adapt to this. Any help would be great!

Comment only
18 Nov 2009 David

David (view profile)

Sorry, but my previous comment contained some errors:
- C = [1 -2; -2; 4]. This is not a valid covariance matrix.
- "positive semi-definite symmetric (that is, they may include negative values off the diagonal)." This is not true.

What is true, is that C must be a positive semi-definite matrix. But that is just a minor comment, because the implementation allows for that.

This Matlab function works. Thanks!

Comment only
17 Nov 2009 David

David (view profile)

% NOTES: C must be positive definite for this function to
% work properly.

This is quite a strange restriction, because covariance matrices are positive semi-definite symmetric (that is, they may include negative values off the diagonal). For example, it is not possible to visualize the following very simple, and valid, covariance matrix: C = [1 -2; -2; 4].

Does anybody know of a Matlab function which is capable of visualizing all kinds of covariance matrices (both 2D and 3D)? Thanks!

Comment only
16 Nov 2009 Pearl

Pearl (view profile)

16 Nov 2009 Benjamin Pavie

01 Sep 2009 Miguel Torres

01 Jul 2009 Nora

Nora (view profile)

02 Dec 2008 John

John (view profile)

function works great and is easy to use. thank you

12 Nov 2008 Dan Stowell

Delightful tool, easy to use and worked straight away

17 Sep 2008 R Lewis

Did the job flawlessly.

06 Aug 2008 Alex Yakubovich

re: change color, multiple ellipses on same plot

To have multiple ellipses, just type 'hold on' in the command window before calling error_ellipse. You can change the color once you've plotted the ellipse in the property editor (open pe in view>property editor)

25 Jul 2008 Adam Attarian

precisely what i needed! hooray.

21 Jun 2008 M ChC

how do I change the color of the ellipse?

04 Jun 2008 Dagmar Schoch

15 May 2008 Trevor Wood

excellent, thanks.

14 May 2008 Siladitya Dey

26 Apr 2008 Marino Pagan

Thank you very much!!! Excellent!

11 Apr 2008 Matthew Nelson

very useful. thank you

21 Feb 2008 phil fox

Be aware that the default confidence interval is 0.5 rather than more standard values such as 0.683, 0.9 etc.. Use the 'conf' parameter to change.

Comment only
21 Nov 2007 Volkan Ozturk

How can i see two plots on the same graphics. I have to do some comparison.


Comment only
03 Jul 2007 kevin chen


25 Jun 2007 Feng Rice


10 Jan 2007 mohan palani

02 Nov 2006 Sujai Kumar

14 Jul 2006 John Magirk

perfect! works great AND easy to use.

05 Jul 2006 Chris Willy

Great code, very useful. Saved me lots of time.

16 May 2006 Matt Irwin

Great code, thanks.

10 May 2006 Damien Dusha

You just saved me a day and most of my hair :)

04 May 2006 Valentin Kuklin

Amazing. Good work! Thanks a lot!

09 Apr 2006 M Fellows

Very Useful!

27 Feb 2006 ryan jessup

worked perfectly! Thanks!

06 Feb 2006 oxana govokhina


24 Jan 2006 Laurent Nguyen

Works perfectly

14 Oct 2005 Juan Pablo Nieto

It works excellent

21 Mar 2005 Paul Thompson

There is NOT an error at line 117.

05 Jan 2005 Kamini Singha

works fabulously.

02 Dec 2004 Niklaus Hugi

29 Jul 2004 Gordon Dryden

There is an error at line 117. The line reads: XYZ = [X(:),Y(:),Z(:)]*sqrt(eigval)*eigvec'; but should be: XYZ = [X(:),Y(:),Z(:)]*eigvec*sqrt(eigval)*eigvec';

13 Apr 2004 Mark Brown

Doesn't run. I get an error...
??? Undefined command/function 'getopt'.
Error in ==> error_ellipse at 61
prop = getopt(default_properties, varargin{:});

Comment only
13 Apr 2004 Una Kusan

Now it's working fine.
Thanks again!

07 Apr 2004 Una Kusan

It doesn't work on my matlab platform (6.1).

Comment only
23 Jul 2015 1.0

Updated license.
Added white paper describing the math behind the confidence region computations.

Contact us