0
$\begingroup$

I'm trying to sketch level curve for z=k for the specified values of k={-2,-1,2} of z= 4x^2 - 4x + y^2 + 2y

So I changed this(by hand) to ellipse of the form 4(x - (1/2))^2 + (y + 1)^2 = k + 2 then I put in the following code

ContourPlot[z + 2 == 4 (x - (1/2))^2 + (y + 1)^2 , {z = -1.99999}, {z = -1}, {z =
    1}, {x, -5, 5}, {y, -5, 5}, ContourStyle -> Black]

but when I try to evaluate cell I get

ContourPlot::nonopt: Options expected (instead of {y,-5,5}) beyond position 3 in ContourPlot[z+2==4 (x-Times[<<2>>])^2+(y+1)^2,{z=-1.99999},{z=-1},{z=1},{x,-5,5},{y,-5,5},ContourStyle->Black]. An option must be a rule or a list of rules. >>
ContourPlot[z + 2 == 4 (x - 1/2)^2 + (y + 1)^2, {z = -1.99999}, {z = -1}, {z =  1}, {x, -5, 5}, {y, -5, 5}, ContourStyle -> Black]

Anyone can tell me what wrong with this code? Also when its says "beyond position 3" do I count 1 for each comma ? How can I get it to sketch this ellipse for the 3 different values of k? thought maybe its the -2 so changed it to -1.999 . I'm sorry if this is really obvious but it's only my 2nd time using Mathematica. Any help is much appreciated.

$\endgroup$

closed as off-topic by Daniel Lichtblau, bbgodfrey, Karsten 7., Mr.Wizard Feb 5 '15 at 7:00

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Daniel Lichtblau, bbgodfrey, Karsten 7., Mr.Wizard
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ Wrong syntax for ContourPlot. You'll want the Contours option. Also you don't actually need to recast in that form (it shouldn't hurt or help, that is). $\endgroup$ – Daniel Lichtblau Feb 4 '15 at 20:29
  • $\begingroup$ Contours[3 == 4 (-(1/2) + x)^2 + (1 + y)^2, {-1.99999}, {-1}, {1}, {x, -5, 5}, {y, -5, 5}, ContourStyle -> GrayLevel[0]] is what it returns when I use Contours $\endgroup$ – user24907 Feb 4 '15 at 20:30
  • $\begingroup$ Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. $\endgroup$ – bbgodfrey Feb 4 '15 at 20:39
  • $\begingroup$ Somebody please improve my answer, utter neophyte when it comes to putting MMA images in answer $\endgroup$ – Manuel --Moe-- G Feb 4 '15 at 20:48
  • $\begingroup$ See Help > Documentation Center, enter ContourPlot, then navigate to Options > Contours and look at example "Use specific contours:". $\endgroup$ – Daniel Lichtblau Feb 4 '15 at 21:56
2
$\begingroup$

Evaluate inside of ContourPlot is how I would do it...

kVals = {-1.95, -1.9, -1.5, -1, 0, 1, 2};

levelCurve = (4 x^2 - 4 x + y^2 + 2 y)

-4 x + 4 x^2 + 2 y + y^2

 levelCurve2 = (4 (x - 1/2)^2 + (y + 1)^2 - 2)

-2 + 4 (-(1/2) + x)^2 + (1 + y)^2

Simplify[levelCurve2 == levelCurve]

True

ContourPlot[
 Evaluate[Table[levelCurve2 == k, {k, kVals}]], {x, -1, 2}, {y, -4, 2}]

ContourPlot with Evaluate

$\endgroup$
  • $\begingroup$ Thank you, but if I only want k= -2, -1, 2 this is all I should need to change in your code? $\endgroup$ – user24907 Feb 4 '15 at 20:51
  • $\begingroup$ what do you expect to see from k=-2? -- I doubt ContourPlot will give you a single point $\endgroup$ – Manuel --Moe-- G Feb 4 '15 at 20:54
  • $\begingroup$ Yes k=-2 was part of question not my choice but I just put in -1.9999 and it has given what looks like a point. $\endgroup$ – user24907 Feb 4 '15 at 20:59
  • 1
    $\begingroup$ Try -1.999 and PlotPoints -> 200 to get inner point (almost). $\endgroup$ – bbgodfrey Feb 4 '15 at 21:00
  • 1
    $\begingroup$ {z = -1.99999} etc, is not a legal argument for ContourPlot, which is why it failed. Note also that you need to delete z from your equation inside ContourPlot, because it is an undefined (and unnecessary) variable. Contours is an option inside ContourPlot. For instance, you could have used Contours->{-1.9999,-1,1}. See the Details and Options section of the ContourPlot documentation for more information. $\endgroup$ – bbgodfrey Feb 4 '15 at 21:21
2
$\begingroup$

To make clear the explanation in my Comments, your curves can be plotted with

ContourPlot[4 (x - (1/2))^2 + (y + 1)^2 - 2, {x, -5, 5}, {y, -5, 5},  ContourStyle -> Black, 
  Contours -> {-1.999, -1, 1}, PlotPoints -> 200, ColorFunction -> (White &)]

contours

It differs from your original expression as follows:

  • The equation involving z is replaced by an expression that defines z
  • Input parameters {z = -1.99999}, etc not recognized by ContourPlot are eliminated. These are what caused the error message.
  • Contours -> {-1.999, -1, 1} specifies the contours desired. This undoubtedly is what Daniel Lichtblau had in mind.
  • ColorFunction -> (White &) specifies that the space between contours is white.
  • PlotPoints -> 200 tells ContourPlot to begin looking for contours at 200 locations. With fewer points it is unable to find the innermost contour. Warning: too large a number of points can slow your code greatly!
$\endgroup$
  • 1
    $\begingroup$ Thanks, sorry for late reply I left computers in college. Will try again with this new knowledge you and Manuel have given me in the morning. Your help and time are much appreciated. $\endgroup$ – user24907 Feb 4 '15 at 23:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.