3

I'm new to R and I need to plot the quadratic matrix equation:

x^T A x + b^T x + c = 0

in R^2, with A being a 2x2, b a 2x1, and c a constant. The equation is for a boundary that defines classes of points. I need to plot that boundary for x0 = -6...6, x1 = -4...6. My first thought was generate a bunch of points and see where they are zero, but it depends on the increment between the numbers (most likely I'm not going guess what points are zero).

Is there a better way than just generating a bunch of points and seeing where it is zero or multiplying it out? Any help would be much appreciated,

Thank you.

Stephen
  • 2,365
  • 17
  • 21
  • I'm pretty sure you have to generate a sample set to plot. Can you give any more information on your inputs and what your expected plot should look like? – N8TRO Feb 21 '13 at 01:50

1 Answers1

5

Assuming you have a symmetric matrix A,

eg

 # A = | a    b/2 |
 #     | b/2  c   |

and your equation represents a conic section, you can use the conics package

What you need is a vector of coefficients c(a,b,c,d,e,f) representing

a.x^2 + b*x*y + c*y^2 + d*x + e*y + f

In your case, say you have

 A <- matrix(c(2,1,1,2))

 B <- c(-20,-28)
 C <- 10


# create the vector
v <- append(c(diag(A),B,C),A[lower.tri(A)]*2), 1)



 conicPlot(v)

enter image description here

You could easily wrap the multiplication out into a simple function

# note this does no checking for symmetry or validity of arguments

expand.conic <- function(A, B, C){
 append(c(diag(A),B,C),A[lower.tri(A)]*2), 1)
}
mnel
  • 113,303
  • 27
  • 265
  • 254
  • Perfect! exactly what I needed. would you have any idea how to make it into a ggplot object? – Stephen Feb 21 '13 at 02:13
  • Not without rewriting conicPlot and `conics:::plotEllipse`. You could look how these work, and write your own wrapper to create a data.frame that you could use with ggplot. – mnel Feb 21 '13 at 02:19
  • 1
    You could also see http://stackoverflow.com/questions/12922740/drawing-ellipses-hyperbolas-in-r for a `ggplot2` approach – mnel Feb 21 '13 at 02:23
  • I'm just marveling at the beauty of this answer. Wish I could pass rep over to your bucket. – IRTFM Jun 13 '14 at 21:09