[R] Plot 2-d Polynomial without solving it

Duncan Murdoch murdoch at stats.uwo.ca
Sun Jul 5 00:33:42 CEST 2009


On 04/07/2009 3:31 PM, Hao Jiang wrote:
> Hi Duncan,Thanks!
> 
> But I still get a little confused about outer() func. Would give me a simple
> example to contour it? Just like the formula x^2 + y^2 + x + y -5 = 0.
> (Sorry I am a newbie to R, found really hard to use the R manual)

 > x <- seq(-6,6,len=100)
 > y <- seq(-4,4,len=100)
 > z <- outer(x,y, function(x,y) x^2 + y^2 + x + y -5)
 > contour(x,y,z,levels=0)

This will draw a contour of the solutions to the equation.  (I set the 
ranges of x and y differently just so I could be sure I got them in the 
right order in the outer and contour functions.)

Duncan Murdoch

> 
> Thanks,
> Hao
> 
> On Sat, Jul 4, 2009 at 7:10 AM, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
> 
>> Hao Jiang wrote:
>>
>>> Hi,
>>> I want to plot a polynomial in the form like ax^2 + bxy + cy^2 + dx + ey +
>>> f
>>> =0 without solving it(since I may have 3 or 4 dimensional polynomial and
>>> it's really hard to solve). Is there any way  to plot this kind of
>>> polynomial?
>>>
>>> Thanks a lot!
>>>
>> There are lots of ways.  A contour plot is probably most informative, a
>> persp plot is prettier.  In either case you need to evaluate the polynomial
>> on a grid, and pass the matrix of values to the plotting function.
>>
>> The outer() function is handy for doing the grid evaluation.
>>
>> Duncan Murdoch
>>
>




More information about the R-help mailing list