I hope this title isn't misleading, but I am creating 2 programs that ask a user a series of questions in regards to derivatives and plots and I will explain as thorough as possible. I don't have a simple example to demonstrate this so here is what I am creating and where I'm stuck:

1st program: This program asks a user to enter a variable of choice, such as x, y or z (whatever single variable you will want to derive). The next question will ask the user to enter some function, and from the function given, a plot will be created of the original function along with both derivatives.

  Clear[var, expr, derivatives, blah, color]; 
  var = Input["Type a variable, such as x, in this window."];
  expr = Input["Type an expression to be differentiated in the variable you just typed in the last window."];
  g[blah_] = expr /. {var -> blah};
  derivatives := {g[var], g'[var], g''[var]}
  Plot[{Evaluate@derivatives}, {x, -5, 5}, PlotLegends -> "Expressions"]

Unfortunately I don't know how to include an image of what the plot looks like, but basically it shows the graph of the original function and 2 derivatives, each labeled by a certain color and there is an included legend that corresponds each function to that color.

Now, for my second program I want to ask a user which color corresponds to which function in the plot that is generated. This program should check whether the user's answer is correct and respond accordingly, such as "Nice job" or "Try again", and asks the question again if the user is wrong. I have been given hints to use the command SameQ but I'm mainly confused about the "color" aspect of this task. The following code I supply below is completely wrong, but I am just trying things. For example:

  blue = Input["What is the color of the original function?"];
  If[derivatives[[1]] === blue, Print["Great Job"], 
  While[derivatives[[1]] != blue, blue = Input["Try again; what is the color of the original function?"]]]

I have absolutely no idea how set a color equal to something. Is it even possible?

Any help or tips is much appreciated. If you need clarification I will try my best to do so.

  • 2
    $\begingroup$ Thanks to @halirutan and others, we have a lovely package that makes including images from MMA in your SE posts a snap. $\endgroup$ Nov 8, 2016 at 1:56

1 Answer 1


First, with

plot = Plot[{Evaluate @ derivatives}, {x, -5, 5}, PlotLegends -> "Expressions"]

the RGB colors are

Cases[plot, RGBColor[x_, y_, z_] -> {x, y, z}, Infinity][[1 ;; 3]]

{{0.368417, 0.506779, 0.709798}, {0.880722, 0.611041, 0.142051}, {0.560181, 0.691569, 0.194885}}

i.e., not red, green, or blue. It would be less ambiguous for the user if

plot = Plot[{Evaluate @ derivatives}, {x, -5, 5}, 
  PlotStyle -> {Blue, Red, Green}, PlotLegends -> "Expressions"]

In such a case,

Cases[plot, RGBColor[x_, y_, z_] -> {x, y, z}, Infinity][[1 ;; 3]]

{{0, 0, 1}, {1, 0, 0}, {0, 1, 0}}


color = Input[
   "What is the color of the original function: red, green or blue?"];

If[color === blue, Print["Great Job"], 
 While[color =!= blue, 
  color = Input[
    "Try again; what is the color of the original function: red, green or blue?"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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