There are a number of issues here:
- 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).
- 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]
}
}
]
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.