3
$\begingroup$

I use Mathematica 8 and am working with DynamicModule. I'm trying to enter a function in an InputField then later apply it to two values entered in subsequent Input Fields. The function just does not seem to be dynamically applying to the new values I input. The issue probably lies in my definition of the function.

     DynamicModule[{dydx = Null, xi = Null, yi = Null}, 
      Deploy[
       Panel[
        Grid[Transpose[{{"Differential equation: (dy/dx)=", "x:", "y:", 
        "slope:"}, {InputField[Dynamic[dydx]], InputField[Dynamic[xi]],
        InputField[Dynamic[yi]], 
        InputField[
          Dynamic[Function[{x, y}, Dynamic[dydx]][Dynamic[xi], 
           Dynamic[yi]]], Enabled -> False]}}]]]]]
$\endgroup$

1 Answer 1

4
$\begingroup$

There are a number of issues here:

  1. There is no need to wrap Dynamic around the inner variables of the final input field expression. Indeed, it is harmful as Dynamic is purely a user interface element and acts as a holding wrapper in any other context (like an arithmetic expression).
  2. The use of Function in the output expression will cause the variables x and y to be localized. However, an use of those variables in the equation field will refer to the symbols in the surrounding context (probably Global`).

We can fix both of these problems thus:

DynamicModule[{dydx = Null, xi = Null, yi = Null}
, Deploy @ Panel @ Grid @ Transpose @
    { { "Differential equation: (dy/dx)=", "x:", "y:", "slope:"}
    , { InputField[Dynamic[dydx]]
      , InputField[Dynamic[xi]]
      , InputField[Dynamic[yi]]
      , InputField[Dynamic[dydx /. {x -> xi, y -> yi}], Enabled -> False]
      }
    }
]

Note how the role of Function has been taken over by a replacement expression. We could have still used Function by "hiding" it from the variable-renaming machinery, e.g.

Dynamic[(Function@@{{x, y}, dydx})[xi, yi]]

But I prefer the simpler replacement expression shown above.

Another possible issue concerns the expressions entered by the user. The user's input is changed ("simplified") when focus leaves each input field. This may or may not be what you want. To disable it, change the expression type for the field to be Boxes. Any use of the field's value must interpret that value using ToExpression. For example, applying this adjustment to the equation field:

DynamicModule[{dydx = Null, xi = Null, yi = Null}
, Deploy @ Panel @ Grid @ Transpose @
    { { "Differential equation: (dy/dx)=", "x:", "y:", "slope:"}
    , { InputField[Dynamic[dydx], Boxes]
      , InputField[Dynamic[xi]]
      , InputField[Dynamic[yi]]
      , InputField[Dynamic[ToExpression@dydx /. {x -> xi, y -> yi}], Enabled -> False]
      }
    }
]

screenshot of the panel

Without the use of Boxes, the user's expression Sin[x] / Cos[y] gets changed to Sec[y] Sin[x] as soon as the cursor leaves the field.

$\endgroup$
1
  • $\begingroup$ Excellent! The substitution worked well. New problems have arisen though $\endgroup$
    – Shaggy1135
    Apr 18, 2014 at 20:35

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.