2
$\begingroup$

I'm currently working on a demonstration and attempting to utilize the Interpretation command based on the example provided in the "Generalized Input" tutorial here.

Regarding the particular example of plotting a function over a specified interval:

Interpretation[
    {f = Sin[x], min = 0, max = 2 Pi},
    Panel[
        Grid[
            {{Style["Plot", Bold], SpanFromLeft},
             {"Function:", InputField[Dynamic[f]]},
             {"Min:", InputField[Dynamic[min]]},
             {"Max:", InputField[Dynamic[max]]}}
             ]
     ],
    Plot[f, {x, min, max}]
 ]

Evaluating this cell sets the sine function as the default plot, but I would like to have the reader put their own in. I've tried setting f=Invisible[1] with the hopes of having anything entered multiplied by an unseen 1, but entering anything becomes invisible. I've also tried using Spacer[1] which works as far as what I want to have, but evaluating the cell generates a template with awkward placement of the "Function:" label. This is fixed once a function is entered.

Is there a way I can make the input boxes blank from the start? Or at least a prettier use of Spacer?

Edit: Another problem with using Spacer here is that plugging in negative numbers for the plot range make it say things like -4+, which prevents an actual plot from being made.

Edit: After some sifting through the documentation, I came across a similar example:

Panel[
    DynamicModule[
        {f = Sin[x]}, 
        Column[{InputField[Dynamic[f]], Dynamic[Plot[f, {x, -5, 5}]]}]
        ]
]

Would it be any easier to adapt the blank-input idea to this code?

$\endgroup$
1
  • $\begingroup$ I think I finally worked out a useful answer to your original question. $\endgroup$
    – m_goldberg
    Commented May 14, 2015 at 18:40

1 Answer 1

2
$\begingroup$

I really don't have any experience with Interpretation, but it seems that requesting a function name or pure function rather than a function form, and initializing f to Null gives the behavior you ask for.

Interpretation[{f = Null, min = 0, max = 2 Pi}, 
  Panel[
    Grid[{
      {Style["Plot", Bold], SpanFromLeft}, 
      {"Function:", InputField[Dynamic[f], FieldHint -> "Enter a function"]}, 
      {"Min:", InputField[Dynamic[min]]}, 
      {"Max:", InputField[Dynamic[max]]}}]],
  Plot[f[x], {x, min, max}]]

Initial appearance:

initial

With a function name:

cos-panl

cos

With a pure function:

pure-pnl

pure

I don't like the idea of requesting a function form (e.g., Sin[x] rather than Sin) because it couples the demonstration a particular symbol x for the independent variable. I think it would be possible to rewrite this to parse the variable out, but how to do that would be different question.

$\endgroup$
1
  • $\begingroup$ Four letters was all it took! This is exactly what I'm looking for. Thanks! I also like the FieldHint addition. As for your comment regarding the functions - I've tweaked my demo so that it only plots linear functions, so I'm making it ask for slope and intercept. $\endgroup$
    – user170231
    Commented May 14, 2015 at 18:41

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.