1

I need to draw a 2D ellipse using its general form (x-c)'A(x-c)=1

I would like to know how to do this effectively in MATLAB using ezplot.

4

2 に答える 2

5

This answer is applicable to almost any problem which can be formulated as an implicit surface/curve in MATLAB. I am going to demonstrate it on an ellipse.

Short Version:

A = [5 4; 4 5]    
c = [1; 2]    
elFunc = @(A11,A22,A12,A21,c1,c2,x,y) (c1-x).*(A11*(c1-x)+A21*(c2-y))+(c2-y).*(A12*(c1-x)+A22*(c2-y))-1    
ezplot(@(x,y) elFunc(A(1,1),A(2,2),A(1,2),A(2,1),c(1),c(2),x,y), [0 2 0 4])

Long Version:

An ellipse can be written implicitly in its most general form (for any dimension) as

(x-c)'A(x-c) = 1 or (x-c)'A(x-c)-1 = 0

where x,c are in R^n and A is an nxn matrix.

In order to get this into a form that MATLAB can use we can use the symbolic toolbox. For a 2D ellipse we write:

syms A11 A12 A21 A22 c1 c2 x y real
impl = ([x y]-[c1 c2])*[A11 A12; A21 A22]*([x;y]-[c1;c2])-1

This produces the following output:

(c1 - x)*(A11*(c1 - x) + A21*(c2 - y)) + (c2 - y)*(A12*(c1 - x) + A22*(c2 - y)) - 1

We dont need the symbolic toolbox anymore so we just copy the string, vectorize it by adding the dot operator version and turn it into a function

elFunc = @(A11,A22,A12,A21,c1,c2,x,y) (c1-x).*(A11*(c1-x)+A21*(c2-y))+(c2-y).*(A12*(c1-x)+A22*(c2-y))-1

Now we can use ezplot to draw our curve. ezplot assumes that when you give it a function handle it needs to solve for func = 0 so our curve is already described by elFunc in implicit format. All that is left for us to do is to define the domain over which we want ezplot to try and draw the curve. The following example demonstrates the result:

A = [5 4; 4 5]    
    c = [1; 2]    
    elFunc = @(A11,A22,A12,A21,c1,c2,x,y) (c1-x).*(A11*(c1-x)+A21*(c2-y))+(c2-y).*(A12*(c1-x)+A22*(c2-y))-1    
    ezplot(@(x,y) elFunc(A(1,1),A(2,2),A(1,2),A(2,1),c(1),c(2),x,y), [0 2 0 4])

Ellipse plotted by ezplot in MATLAB

于 2012-08-17T11:53:24.200 に答える
4

この回答は、表記がもう少しMatlabbyであることを除いて、@twerdsterの回答とまったく同じです。

A = [5 4; 4 5];
c = [1 2];

elFunc = @(x,y) sum(([x(:)-c(1) y(:)-c(2)] * A) .* [x(:)-c(1) y(:)-c(2)], 2) - 1;
ezplot(elFunc, [0 2 0 4])

最後の発言として、両方の回答がすでに示しているように、簡単にプロットできるezplotものを対象としています。楕円は、無名関数および.ezplot

ezplot一般的に、それ以上の難易度のものには使用しないことをお勧めしますezplot(@(x)sin(x).*cos(2*x))function練習して流暢に話せるようになることはplot()、はるかに実り多いものsurf()です。

于 2012-08-17T12:37:57.427 に答える